using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

            migrationBuilder.CreateTable(
                name: "LedgerEntries",
                schema: "payments",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    TransactionGroupId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    AccountType = table.Column<string>(type: "nvarchar(40)", maxLength: 40, nullable: false),
                    NurseId = table.Column<long>(type: "bigint", nullable: true),
                    Direction = table.Column<string>(type: "nvarchar(6)", maxLength: 6, nullable: false),
                    AmountIrr = table.Column<long>(type: "bigint", nullable: false),
                    BookingId = table.Column<long>(type: "bigint", nullable: true),
                    SourceRefType = table.Column<string>(type: "nvarchar(40)", maxLength: 40, nullable: false),
                    SourceRefId = table.Column<long>(type: "bigint", nullable: false),
                    Memo = table.Column<string>(type: "nvarchar(300)", maxLength: 300, nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_LedgerEntries", x => x.Id);
                    table.ForeignKey(
                        name: "FK_LedgerEntries_Bookings_BookingId",
                        column: x => x.BookingId,
                        principalSchema: "booking",
                        principalTable: "Bookings",
                        principalColumn: "Id");
                    table.ForeignKey(
                        name: "FK_LedgerEntries_NurseProfiles_NurseId",
                        column: x => x.NurseId,
                        principalSchema: "usr",
                        principalTable: "NurseProfiles",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateTable(
                name: "PaymentGateways",
                schema: "payments",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    ProviderCode = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    Type = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    DisplayName = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
                    ConfigJson = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    IsActive = table.Column<bool>(type: "bit", nullable: false),
                    Priority = table.Column<int>(type: "int", 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_PaymentGateways", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "PaymentWebhookEvents",
                schema: "payments",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    ProviderCode = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    ExternalEventId = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
                    EventType = table.Column<string>(type: "nvarchar(80)", maxLength: 80, nullable: false),
                    SignatureValid = table.Column<bool>(type: "bit", nullable: false),
                    PayloadJson = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    ProcessingStatus = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    RelatedPaymentTransactionId = table.Column<long>(type: "bigint", nullable: true),
                    ReceivedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    ProcessedAt = table.Column<DateTime>(type: "datetime2", 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_PaymentWebhookEvents", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "PaymentTransactions",
                schema: "payments",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    BookingRequestId = table.Column<long>(type: "bigint", nullable: false),
                    BookingId = table.Column<long>(type: "bigint", nullable: true),
                    CustomerId = table.Column<long>(type: "bigint", nullable: false),
                    GatewayId = table.Column<long>(type: "bigint", nullable: false),
                    Amount = table.Column<long>(type: "bigint", nullable: false),
                    Currency = table.Column<string>(type: "nvarchar(3)", maxLength: 3, nullable: false),
                    Status = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    GatewayTransactionId = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
                    GatewayReferenceCode = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
                    GatewayResponseCode = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
                    GatewayResponseJson = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    IsInstallment = table.Column<bool>(type: "bit", nullable: false),
                    IpAddress = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    UserAgent = table.Column<string>(type: "nvarchar(400)", maxLength: 400, 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_PaymentTransactions", x => x.Id);
                    table.ForeignKey(
                        name: "FK_PaymentTransactions_BookingRequests_BookingRequestId",
                        column: x => x.BookingRequestId,
                        principalSchema: "booking",
                        principalTable: "BookingRequests",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_PaymentTransactions_Bookings_BookingId",
                        column: x => x.BookingId,
                        principalSchema: "booking",
                        principalTable: "Bookings",
                        principalColumn: "Id");
                    table.ForeignKey(
                        name: "FK_PaymentTransactions_CustomerProfiles_CustomerId",
                        column: x => x.CustomerId,
                        principalSchema: "usr",
                        principalTable: "CustomerProfiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_PaymentTransactions_PaymentGateways_GatewayId",
                        column: x => x.GatewayId,
                        principalSchema: "payments",
                        principalTable: "PaymentGateways",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_LedgerEntries_AccountType_NurseId",
                schema: "payments",
                table: "LedgerEntries",
                columns: new[] { "AccountType", "NurseId" });

            migrationBuilder.CreateIndex(
                name: "IX_LedgerEntries_BookingId",
                schema: "payments",
                table: "LedgerEntries",
                column: "BookingId");

            migrationBuilder.CreateIndex(
                name: "IX_LedgerEntries_NurseId",
                schema: "payments",
                table: "LedgerEntries",
                column: "NurseId");

            migrationBuilder.CreateIndex(
                name: "IX_LedgerEntries_SourceRefType_SourceRefId",
                schema: "payments",
                table: "LedgerEntries",
                columns: new[] { "SourceRefType", "SourceRefId" });

            migrationBuilder.CreateIndex(
                name: "IX_LedgerEntries_TransactionGroupId",
                schema: "payments",
                table: "LedgerEntries",
                column: "TransactionGroupId");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentGateways_Type_IsActive_Priority",
                schema: "payments",
                table: "PaymentGateways",
                columns: new[] { "Type", "IsActive", "Priority" });

            migrationBuilder.CreateIndex(
                name: "IX_PaymentTransactions_BookingId",
                schema: "payments",
                table: "PaymentTransactions",
                column: "BookingId",
                unique: true,
                filter: "[Status] = 'succeeded' AND [BookingId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentTransactions_BookingId_Status",
                schema: "payments",
                table: "PaymentTransactions",
                columns: new[] { "BookingId", "Status" });

            migrationBuilder.CreateIndex(
                name: "IX_PaymentTransactions_BookingRequestId_Status",
                schema: "payments",
                table: "PaymentTransactions",
                columns: new[] { "BookingRequestId", "Status" });

            migrationBuilder.CreateIndex(
                name: "IX_PaymentTransactions_CustomerId",
                schema: "payments",
                table: "PaymentTransactions",
                column: "CustomerId");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentTransactions_GatewayId",
                schema: "payments",
                table: "PaymentTransactions",
                column: "GatewayId");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentTransactions_GatewayReferenceCode",
                schema: "payments",
                table: "PaymentTransactions",
                column: "GatewayReferenceCode",
                unique: true,
                filter: "[GatewayReferenceCode] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_PaymentWebhookEvents_ProviderCode_ExternalEventId",
                schema: "payments",
                table: "PaymentWebhookEvents",
                columns: new[] { "ProviderCode", "ExternalEventId" },
                unique: true);
        }

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

            migrationBuilder.DropTable(
                name: "PaymentTransactions",
                schema: "payments");

            migrationBuilder.DropTable(
                name: "PaymentWebhookEvents",
                schema: "payments");

            migrationBuilder.DropTable(
                name: "PaymentGateways",
                schema: "payments");
        }
    }
}
