using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Baya.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class BnplTransactions : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "BnplTransactions",
                schema: "payments",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    PaymentTransactionId = table.Column<long>(type: "bigint", nullable: false),
                    ProviderCode = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    MerchantOfRecord = table.Column<string>(type: "nvarchar(40)", maxLength: 40, nullable: false),
                    ExternalPaymentToken = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
                    ExternalTransactionId = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
                    EligibilityStatus = table.Column<string>(type: "nvarchar(30)", maxLength: 30, nullable: true),
                    OrderAmountIrr = table.Column<long>(type: "bigint", nullable: false),
                    SettledAmountIrr = table.Column<long>(type: "bigint", nullable: true),
                    BnplCommissionIrr = table.Column<long>(type: "bigint", nullable: true),
                    Currency = table.Column<string>(type: "nvarchar(5)", maxLength: 5, nullable: false),
                    InstallmentCount = table.Column<byte>(type: "tinyint", nullable: false),
                    Status = table.Column<string>(type: "nvarchar(30)", maxLength: 30, nullable: false),
                    SettledAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    RevertTransactionId = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
                    RevertedAmountIrr = table.Column<long>(type: "bigint", nullable: true),
                    RevertedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    ProviderCommissionReversedAmount = table.Column<long>(type: "bigint", nullable: true),
                    RefundChannel = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: true),
                    CallbackPayloadJson = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    DeletedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    ModifiedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedById = table.Column<int>(type: "int", nullable: true),
                    ModifiedById = table.Column<int>(type: "int", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_BnplTransactions", x => x.Id);
                    table.CheckConstraint("CK_BnplTransactions_SettleSplit", "([SettledAmountIrr] IS NULL AND [BnplCommissionIrr] IS NULL) OR ([SettledAmountIrr] = [OrderAmountIrr] - [BnplCommissionIrr] AND [SettledAmountIrr] >= 0 AND [BnplCommissionIrr] >= 0)");
                    table.ForeignKey(
                        name: "FK_BnplTransactions_PaymentTransactions_PaymentTransactionId",
                        column: x => x.PaymentTransactionId,
                        principalSchema: "payments",
                        principalTable: "PaymentTransactions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_BnplTransactions_ExternalPaymentToken",
                schema: "payments",
                table: "BnplTransactions",
                column: "ExternalPaymentToken",
                filter: "[ExternalPaymentToken] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_BnplTransactions_PaymentTransactionId",
                schema: "payments",
                table: "BnplTransactions",
                column: "PaymentTransactionId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_BnplTransactions_Status",
                schema: "payments",
                table: "BnplTransactions",
                column: "Status");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "BnplTransactions",
                schema: "payments");
        }
    }
}
