using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Infrastructure.Migrations.AppDb
{
    /// <inheritdoc />
    public partial class AddCrypto : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<int>(
                name: "CoinID",
                table: "Board",
                type: "int",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "Coin",
                columns: table => new
                {
                    ID = table.Column<int>(type: "int", nullable: false, comment: "PK")
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Symbol = table.Column<string>(type: "nvarchar(30)", maxLength: 30, nullable: false, comment: "심볼 (BTC, ETH 등)"),
                    Name = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false, comment: "코인 이름"),
                    LogoImage = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true, comment: "로고 이미지"),
                    Description = table.Column<string>(type: "nvarchar(max)", nullable: true, comment: "설명"),
                    ContractAddress = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: true, comment: "컨트랙트 주소"),
                    WebsiteUrl = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true, comment: "홈페이지 URL"),
                    WhitepaperUrl = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true, comment: "백서 URL"),
                    TwitterUrl = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true, comment: "트위터 URL"),
                    TelegramUrl = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true, comment: "텔레그램 URL"),
                    IsActive = table.Column<bool>(type: "bit", nullable: false, comment: "사용 여부"),
                    IsWarning = table.Column<bool>(type: "bit", nullable: false, comment: "위험 경고"),
                    IsNew = table.Column<bool>(type: "bit", nullable: false, comment: "신규 상장"),
                    IsDelisted = table.Column<bool>(type: "bit", nullable: false, comment: "상장 폐지"),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true, comment: "수정 일시"),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, comment: "등록 일시")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Coin", x => x.ID);
                },
                comment: "코인/토큰");

            migrationBuilder.CreateTable(
                name: "CoinCategory",
                columns: table => new
                {
                    ID = table.Column<int>(type: "int", nullable: false, comment: "PK")
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Code = table.Column<string>(type: "nvarchar(30)", maxLength: 30, nullable: false, comment: "카테고리 코드"),
                    Name = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false, comment: "카테고리 이름"),
                    Order = table.Column<short>(type: "smallint", nullable: false, comment: "순서"),
                    IsActive = table.Column<bool>(type: "bit", nullable: false, comment: "사용 여부"),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true, comment: "수정 일시"),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, comment: "등록 일시")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_CoinCategory", x => x.ID);
                },
                comment: "코인 카테고리");

            migrationBuilder.CreateTable(
                name: "CoinCategoryMap",
                columns: table => new
                {
                    ID = table.Column<int>(type: "int", nullable: false, comment: "PK")
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CoinID = table.Column<int>(type: "int", nullable: false, comment: "코인 ID"),
                    CategoryID = table.Column<int>(type: "int", nullable: false, comment: "카테고리 ID")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_CoinCategoryMap", x => x.ID);
                    table.ForeignKey(
                        name: "FK_CoinCategoryMap_CoinCategory_CategoryID",
                        column: x => x.CategoryID,
                        principalTable: "CoinCategory",
                        principalColumn: "ID",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_CoinCategoryMap_Coin_CoinID",
                        column: x => x.CoinID,
                        principalTable: "Coin",
                        principalColumn: "ID",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "코인-카테고리 연결");

            migrationBuilder.CreateIndex(
                name: "IX_Board_CoinID",
                table: "Board",
                column: "CoinID");

            migrationBuilder.CreateIndex(
                name: "IX_Coin_IsActive",
                table: "Coin",
                column: "IsActive");

            migrationBuilder.CreateIndex(
                name: "IX_Coin_IsDelisted",
                table: "Coin",
                column: "IsDelisted");

            migrationBuilder.CreateIndex(
                name: "IX_Coin_IsNew",
                table: "Coin",
                column: "IsNew");

            migrationBuilder.CreateIndex(
                name: "IX_Coin_IsWarning",
                table: "Coin",
                column: "IsWarning");

            migrationBuilder.CreateIndex(
                name: "IX_Coin_Symbol",
                table: "Coin",
                column: "Symbol",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Coin_Symbol_IsActive",
                table: "Coin",
                columns: new[] { "Symbol", "IsActive" });

            migrationBuilder.CreateIndex(
                name: "IX_CoinCategory_Code",
                table: "CoinCategory",
                column: "Code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_CoinCategory_IsActive",
                table: "CoinCategory",
                column: "IsActive");

            migrationBuilder.CreateIndex(
                name: "IX_CoinCategory_Order",
                table: "CoinCategory",
                column: "Order");

            migrationBuilder.CreateIndex(
                name: "IX_CoinCategoryMap_CategoryID",
                table: "CoinCategoryMap",
                column: "CategoryID");

            migrationBuilder.CreateIndex(
                name: "IX_CoinCategoryMap_CoinID_CategoryID",
                table: "CoinCategoryMap",
                columns: new[] { "CoinID", "CategoryID" },
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_Board_Coin_CoinID",
                table: "Board",
                column: "CoinID",
                principalTable: "Coin",
                principalColumn: "ID",
                onDelete: ReferentialAction.SetNull);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Board_Coin_CoinID",
                table: "Board");

            migrationBuilder.DropTable(
                name: "CoinCategoryMap");

            migrationBuilder.DropTable(
                name: "CoinCategory");

            migrationBuilder.DropTable(
                name: "Coin");

            migrationBuilder.DropIndex(
                name: "IX_Board_CoinID",
                table: "Board");

            migrationBuilder.DropColumn(
                name: "CoinID",
                table: "Board");
        }
    }
}
