using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Infrastructure.Migrations.AppDb
{
    /// <inheritdoc />
    public partial class AddRssNewsTables : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "RssFeedSource",
                columns: table => new
                {
                    ID = table.Column<int>(type: "int", nullable: false, comment: "PK")
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false, comment: "소스 이름"),
                    Url = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: false, comment: "RSS 피드 URL"),
                    Description = table.Column<string>(type: "nvarchar(max)", nullable: true, comment: "소스 설명"),
                    IntervalMinutes = table.Column<int>(type: "int", nullable: false, comment: "수집 주기 (분)"),
                    IsActive = table.Column<bool>(type: "bit", nullable: false, comment: "활성화 여부"),
                    LastFetchedAt = table.Column<DateTime>(type: "datetime2", nullable: true, comment: "마지막 수집 일시"),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true, comment: "수정 일시"),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, comment: "등록 일시")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_RssFeedSource", x => x.ID);
                },
                comment: "RSS 피드 소스");

            migrationBuilder.CreateTable(
                name: "RssNewsArticle",
                columns: table => new
                {
                    ID = table.Column<int>(type: "int", nullable: false, comment: "PK")
                        .Annotation("SqlServer:Identity", "1, 1"),
                    RssFeedSourceID = table.Column<int>(type: "int", nullable: false, comment: "RSS 피드 소스 FK"),
                    Title = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false, comment: "기사 제목"),
                    Link = table.Column<string>(type: "nvarchar(2000)", maxLength: 2000, nullable: true, comment: "원본 링크"),
                    Guid = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true, comment: "RSS GUID (중복 방지)"),
                    Author = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true, comment: "작성자"),
                    Description = table.Column<string>(type: "nvarchar(max)", nullable: true, comment: "요약"),
                    Content = table.Column<string>(type: "nvarchar(max)", nullable: true, comment: "본문 (content:encoded)"),
                    ImageUrl = table.Column<string>(type: "nvarchar(2000)", maxLength: 2000, nullable: true, comment: "썸네일 이미지 URL"),
                    SourceName = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true, comment: "출처명"),
                    Categories = table.Column<string>(type: "nvarchar(max)", nullable: true, comment: "카테고리 (JSON 배열)"),
                    CommentCount = table.Column<int>(type: "int", nullable: false, comment: "댓글 수"),
                    PublishedAt = table.Column<DateTime>(type: "datetime2", nullable: true, comment: "발행 일시"),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, comment: "수집 일시")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_RssNewsArticle", x => x.ID);
                    table.ForeignKey(
                        name: "FK_RssNewsArticle_RssFeedSource_RssFeedSourceID",
                        column: x => x.RssFeedSourceID,
                        principalTable: "RssFeedSource",
                        principalColumn: "ID",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "RSS 뉴스 기사");

            migrationBuilder.CreateIndex(
                name: "IX_RssFeedSource_IsActive",
                table: "RssFeedSource",
                column: "IsActive");

            migrationBuilder.CreateIndex(
                name: "IX_RssFeedSource_Url",
                table: "RssFeedSource",
                column: "Url",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_RssNewsArticle_CreatedAt",
                table: "RssNewsArticle",
                column: "CreatedAt");

            migrationBuilder.CreateIndex(
                name: "IX_RssNewsArticle_Guid_RssFeedSourceID",
                table: "RssNewsArticle",
                columns: new[] { "Guid", "RssFeedSourceID" },
                unique: true,
                filter: "[Guid] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_RssNewsArticle_PublishedAt",
                table: "RssNewsArticle",
                column: "PublishedAt");

            migrationBuilder.CreateIndex(
                name: "IX_RssNewsArticle_RssFeedSourceID",
                table: "RssNewsArticle",
                column: "RssFeedSourceID");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "RssNewsArticle");

            migrationBuilder.DropTable(
                name: "RssFeedSource");
        }
    }
}
