using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class AddPaymentTables : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "MerchantId",
                table: "PaymentOrder",
                type: "nvarchar(20)",
                maxLength: 20,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "OrderName",
                table: "PaymentOrder",
                type: "nvarchar(100)",
                maxLength: 100,
                nullable: false,
                defaultValue: "");

            migrationBuilder.CreateTable(
                name: "PaymentCancel",
                columns: table => new
                {
                    ID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    MemberID = table.Column<int>(type: "int", nullable: false),
                    PaymentOrderID = table.Column<int>(type: "int", nullable: false),
                    Method = table.Column<int>(type: "int", nullable: false),
                    TransactionId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    OrderId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Amount = table.Column<int>(type: "int", nullable: false),
                    MerchantId = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    CancelType = table.Column<int>(type: "int", nullable: false),
                    CancelRequester = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: true),
                    CancelReason = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: true),
                    ResponseCode = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: true),
                    ResponseMessage = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
                    OriginalTransactionId = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
                    CancelledAmount = table.Column<int>(type: "int", nullable: true),
                    TransDate = table.Column<string>(type: "nvarchar(15)", maxLength: 15, nullable: true),
                    TransTime = table.Column<string>(type: "nvarchar(15)", maxLength: 15, nullable: true),
                    Balance = table.Column<int>(type: "int", nullable: true),
                    RemainedAmount = table.Column<int>(type: "int", nullable: true),
                    ApprovalDateTime = table.Column<string>(type: "nvarchar(15)", maxLength: 15, nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_PaymentCancel", x => x.ID);
                    table.ForeignKey(
                        name: "FK_PaymentCancel_Member_MemberID",
                        column: x => x.MemberID,
                        principalTable: "Member",
                        principalColumn: "ID");
                    table.ForeignKey(
                        name: "FK_PaymentCancel_PaymentOrder_PaymentOrderID",
                        column: x => x.PaymentOrderID,
                        principalTable: "PaymentOrder",
                        principalColumn: "ID");
                },
                comment: "PG 결제 취소 (요청+응답)");

            migrationBuilder.CreateTable(
                name: "PaymentConfirm",
                columns: table => new
                {
                    ID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    MemberID = table.Column<int>(type: "int", nullable: false),
                    PaymentOrderID = table.Column<int>(type: "int", nullable: false),
                    Method = table.Column<int>(type: "int", nullable: false),
                    TransactionId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    OrderId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Amount = table.Column<int>(type: "int", nullable: false),
                    MerchantId = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    CertificateToken = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
                    ResponseCode = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: true),
                    ResponseMessage = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
                    OrderName = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
                    TotalAmount = table.Column<int>(type: "int", nullable: true),
                    DiscountAmount = table.Column<int>(type: "int", nullable: true),
                    UserName = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: true),
                    TransDate = table.Column<string>(type: "nvarchar(8)", maxLength: 8, nullable: true),
                    TransTime = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: true),
                    CardCode = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: true),
                    CardName = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: true),
                    CardNo = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    InstallmentMonths = table.Column<byte>(type: "tinyint", nullable: true),
                    ApproveNo = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
                    ApprovalDateTime = table.Column<string>(type: "nvarchar(15)", maxLength: 15, nullable: true),
                    AuthKey = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: true),
                    AccountNumber = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: true),
                    BankCode = table.Column<string>(type: "nvarchar(3)", maxLength: 3, nullable: true),
                    UserId = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: true),
                    UserEmail = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: true),
                    BankName = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: true),
                    ExpireDate = table.Column<string>(type: "nvarchar(10)", maxLength: 10, nullable: true),
                    ExpireTime = table.Column<string>(type: "nvarchar(15)", maxLength: 15, nullable: true),
                    VirtualAccountNumber = table.Column<string>(type: "nvarchar(30)", maxLength: 30, nullable: true),
                    UseCashReceipt = table.Column<string>(type: "nvarchar(1)", maxLength: 1, nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_PaymentConfirm", x => x.ID);
                    table.ForeignKey(
                        name: "FK_PaymentConfirm_Member_MemberID",
                        column: x => x.MemberID,
                        principalTable: "Member",
                        principalColumn: "ID");
                    table.ForeignKey(
                        name: "FK_PaymentConfirm_PaymentOrder_PaymentOrderID",
                        column: x => x.PaymentOrderID,
                        principalTable: "PaymentOrder",
                        principalColumn: "ID");
                },
                comment: "PG 결제 승인 (요청+응답)");

            migrationBuilder.CreateTable(
                name: "PaymentLog",
                columns: table => new
                {
                    ID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    MemberID = table.Column<int>(type: "int", nullable: false),
                    LogType = table.Column<int>(type: "int", nullable: false),
                    Code = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    Message = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false),
                    TransactionId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    OrderId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    ExtraData = table.Column<string>(type: "nvarchar(4000)", maxLength: 4000, nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_PaymentLog", x => x.ID);
                    table.ForeignKey(
                        name: "FK_PaymentLog_Member_MemberID",
                        column: x => x.MemberID,
                        principalTable: "Member",
                        principalColumn: "ID");
                },
                comment: "PG 결제 에러/실패/웹훅 로그");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentCancel_CancelType",
                table: "PaymentCancel",
                column: "CancelType");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentCancel_MemberID",
                table: "PaymentCancel",
                column: "MemberID");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentCancel_Method",
                table: "PaymentCancel",
                column: "Method");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentCancel_OrderId",
                table: "PaymentCancel",
                column: "OrderId");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentCancel_PaymentOrderID",
                table: "PaymentCancel",
                column: "PaymentOrderID");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentCancel_TransactionId",
                table: "PaymentCancel",
                column: "TransactionId");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentConfirm_MemberID",
                table: "PaymentConfirm",
                column: "MemberID");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentConfirm_Method",
                table: "PaymentConfirm",
                column: "Method");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentConfirm_OrderId",
                table: "PaymentConfirm",
                column: "OrderId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_PaymentConfirm_PaymentOrderID",
                table: "PaymentConfirm",
                column: "PaymentOrderID");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentConfirm_TransactionId",
                table: "PaymentConfirm",
                column: "TransactionId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_PaymentLog_Code",
                table: "PaymentLog",
                column: "Code");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentLog_LogType",
                table: "PaymentLog",
                column: "LogType");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentLog_MemberID",
                table: "PaymentLog",
                column: "MemberID");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentLog_OrderId",
                table: "PaymentLog",
                column: "OrderId");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentLog_TransactionId",
                table: "PaymentLog",
                column: "TransactionId");
        }

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

            migrationBuilder.DropTable(
                name: "PaymentConfirm");

            migrationBuilder.DropTable(
                name: "PaymentLog");

            migrationBuilder.DropColumn(
                name: "MerchantId",
                table: "PaymentOrder");

            migrationBuilder.DropColumn(
                name: "OrderName",
                table: "PaymentOrder");
        }
    }
}
