using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional

namespace Baya.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class NursePayoutEngine : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.EnsureSchema(
                name: "payouts");

            migrationBuilder.CreateTable(
                name: "NursePayoutBatches",
                schema: "payouts",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    PeriodStart = table.Column<DateOnly>(type: "date", nullable: false),
                    PeriodEnd = table.Column<DateOnly>(type: "date", nullable: false),
                    ProcessingDate = table.Column<DateOnly>(type: "date", nullable: false),
                    TotalAmount = table.Column<long>(type: "bigint", nullable: false),
                    PayoutCount = table.Column<int>(type: "int", nullable: false),
                    Status = table.Column<string>(type: "nvarchar(30)", maxLength: 30, nullable: false),
                    InitiatedByAdminId = table.Column<int>(type: "int", nullable: false),
                    ProcessedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    FailureNotes = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, 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_NursePayoutBatches", x => x.Id);
                    table.ForeignKey(
                        name: "FK_NursePayoutBatches_Users_InitiatedByAdminId",
                        column: x => x.InitiatedByAdminId,
                        principalSchema: "usr",
                        principalTable: "Users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "NursePayouts",
                schema: "payouts",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    BatchId = table.Column<long>(type: "bigint", nullable: false),
                    NurseId = table.Column<long>(type: "bigint", nullable: false),
                    BankAccountId = table.Column<long>(type: "bigint", nullable: false),
                    IbanSnapshot = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    GrossEarningsIrr = table.Column<long>(type: "bigint", nullable: false),
                    ClawbackAppliedIrr = table.Column<long>(type: "bigint", nullable: false),
                    NetAmountIrr = table.Column<long>(type: "bigint", nullable: false),
                    Amount = table.Column<long>(type: "bigint", nullable: false),
                    BookingCount = table.Column<int>(type: "int", nullable: false),
                    Status = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    TransferReference = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
                    PaidAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    FailureReason = table.Column<string>(type: "nvarchar(500)", maxLength: 500, 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_NursePayouts", x => x.Id);
                    table.CheckConstraint("CK_NursePayouts_NetSplit", "[NetAmountIrr] = [GrossEarningsIrr] - [ClawbackAppliedIrr] AND [GrossEarningsIrr] >= 0 AND [ClawbackAppliedIrr] >= 0 AND [NetAmountIrr] >= 0 AND [Amount] >= 0");
                    table.ForeignKey(
                        name: "FK_NursePayouts_NurseBankAccounts_BankAccountId",
                        column: x => x.BankAccountId,
                        principalSchema: "usr",
                        principalTable: "NurseBankAccounts",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_NursePayouts_NursePayoutBatches_BatchId",
                        column: x => x.BatchId,
                        principalSchema: "payouts",
                        principalTable: "NursePayoutBatches",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_NursePayouts_NurseProfiles_NurseId",
                        column: x => x.NurseId,
                        principalSchema: "usr",
                        principalTable: "NurseProfiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "NursePayoutBookingLinks",
                schema: "payouts",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    PayoutId = table.Column<long>(type: "bigint", nullable: false),
                    BookingId = table.Column<long>(type: "bigint", nullable: false),
                    SessionId = table.Column<long>(type: "bigint", nullable: true),
                    PayoutAmountIrr = table.Column<long>(type: "bigint", nullable: false),
                    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_NursePayoutBookingLinks", x => x.Id);
                    table.ForeignKey(
                        name: "FK_NursePayoutBookingLinks_BookingSessions_SessionId",
                        column: x => x.SessionId,
                        principalSchema: "booking",
                        principalTable: "BookingSessions",
                        principalColumn: "Id");
                    table.ForeignKey(
                        name: "FK_NursePayoutBookingLinks_Bookings_BookingId",
                        column: x => x.BookingId,
                        principalSchema: "booking",
                        principalTable: "Bookings",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_NursePayoutBookingLinks_NursePayouts_PayoutId",
                        column: x => x.PayoutId,
                        principalSchema: "payouts",
                        principalTable: "NursePayouts",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.InsertData(
                schema: "ops",
                table: "PlatformConfigs",
                columns: new[] { "Id", "CreatedAt", "CreatedById", "DataType", "Description", "Key", "ModifiedAt", "ModifiedById", "Value" },
                values: new object[,]
                {
                    { 22L, new DateTimeOffset(new DateTime(2026, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), new TimeSpan(0, 0, 0, 0, 0)), null, "decimal", "IRR net-amount threshold above which a payout is routed via SATNA (real-time) instead of PAYA (batch) (b13).", "payout_satna_threshold_irr", null, null, "1000000000" },
                    { 23L, new DateTimeOffset(new DateTime(2026, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), new TimeSpan(0, 0, 0, 0, 0)), null, "bool", "When on, a BNPL-paid booking is payout-eligible only after its provider settlement is received (b13; default off — the DEFERRED settled_at guard).", "require_bnpl_settlement_for_payout", null, null, "false" }
                });

            migrationBuilder.CreateIndex(
                name: "IX_NursePayoutBatches_InitiatedByAdminId",
                schema: "payouts",
                table: "NursePayoutBatches",
                column: "InitiatedByAdminId");

            migrationBuilder.CreateIndex(
                name: "IX_NursePayoutBatches_ProcessingDate",
                schema: "payouts",
                table: "NursePayoutBatches",
                column: "ProcessingDate");

            migrationBuilder.CreateIndex(
                name: "IX_NursePayoutBatches_Status",
                schema: "payouts",
                table: "NursePayoutBatches",
                column: "Status");

            migrationBuilder.CreateIndex(
                name: "IX_NursePayoutBookingLinks_BookingId",
                schema: "payouts",
                table: "NursePayoutBookingLinks",
                column: "BookingId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_NursePayoutBookingLinks_PayoutId",
                schema: "payouts",
                table: "NursePayoutBookingLinks",
                column: "PayoutId");

            migrationBuilder.CreateIndex(
                name: "IX_NursePayoutBookingLinks_SessionId",
                schema: "payouts",
                table: "NursePayoutBookingLinks",
                column: "SessionId");

            migrationBuilder.CreateIndex(
                name: "IX_NursePayouts_BankAccountId",
                schema: "payouts",
                table: "NursePayouts",
                column: "BankAccountId");

            migrationBuilder.CreateIndex(
                name: "IX_NursePayouts_BatchId",
                schema: "payouts",
                table: "NursePayouts",
                column: "BatchId");

            migrationBuilder.CreateIndex(
                name: "IX_NursePayouts_NurseId",
                schema: "payouts",
                table: "NursePayouts",
                column: "NurseId");

            migrationBuilder.CreateIndex(
                name: "IX_NursePayouts_Status",
                schema: "payouts",
                table: "NursePayouts",
                column: "Status");
        }

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

            migrationBuilder.DropTable(
                name: "NursePayouts",
                schema: "payouts");

            migrationBuilder.DropTable(
                name: "NursePayoutBatches",
                schema: "payouts");

            migrationBuilder.DeleteData(
                schema: "ops",
                table: "PlatformConfigs",
                keyColumn: "Id",
                keyValue: 22L);

            migrationBuilder.DeleteData(
                schema: "ops",
                table: "PlatformConfigs",
                keyColumn: "Id",
                keyValue: 23L);
        }
    }
}
