using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace ROLAC.API.Migrations
{
    /// <inheritdoc />
    public partial class AddGivingModule : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "GivingCategories",
                columns: table => new
                {
                    Id = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Name_en = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    Name_zh = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
                    Description_en = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    Description_zh = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    IsActive = table.Column<bool>(type: "boolean", nullable: false),
                    SortOrder = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    CreatedBy = table.Column<string>(type: "character varying(450)", maxLength: 450, nullable: false),
                    UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    UpdatedBy = table.Column<string>(type: "character varying(450)", maxLength: 450, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_GivingCategories", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "OfferingSessions",
                columns: table => new
                {
                    Id = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    SessionDate = table.Column<DateOnly>(type: "date", nullable: false),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, defaultValue: "Draft"),
                    CashTotal = table.Column<decimal>(type: "numeric(18,2)", nullable: false),
                    CheckTotal = table.Column<decimal>(type: "numeric(18,2)", nullable: false),
                    SystemTotal = table.Column<decimal>(type: "numeric(18,2)", nullable: false),
                    Difference = table.Column<decimal>(type: "numeric(18,2)", nullable: false),
                    Notes = table.Column<string>(type: "text", nullable: true),
                    SubmittedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    SubmittedBy = table.Column<string>(type: "character varying(450)", maxLength: 450, nullable: true),
                    ReconciledAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    ReconciledBy = table.Column<string>(type: "character varying(450)", maxLength: 450, nullable: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    CreatedBy = table.Column<string>(type: "character varying(450)", maxLength: 450, nullable: false),
                    UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    UpdatedBy = table.Column<string>(type: "character varying(450)", maxLength: 450, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OfferingSessions", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "Givings",
                columns: table => new
                {
                    Id = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    MemberId = table.Column<int>(type: "integer", nullable: true),
                    GivingCategoryId = table.Column<int>(type: "integer", nullable: false),
                    OfferingSessionId = table.Column<int>(type: "integer", nullable: true),
                    Amount = table.Column<decimal>(type: "numeric(18,2)", nullable: false),
                    PaymentMethod = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    CheckNumber = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
                    ZelleReferenceCode = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
                    PayPalTransactionId = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
                    GivingDate = table.Column<DateOnly>(type: "date", nullable: false),
                    IsAnonymous = table.Column<bool>(type: "boolean", nullable: false),
                    Notes = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    CreatedBy = table.Column<string>(type: "character varying(450)", maxLength: 450, nullable: false),
                    UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    UpdatedBy = table.Column<string>(type: "character varying(450)", maxLength: 450, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Givings", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Givings_GivingCategories_GivingCategoryId",
                        column: x => x.GivingCategoryId,
                        principalTable: "GivingCategories",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_Givings_Members_MemberId",
                        column: x => x.MemberId,
                        principalTable: "Members",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "FK_Givings_OfferingSessions_OfferingSessionId",
                        column: x => x.OfferingSessionId,
                        principalTable: "OfferingSessions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Givings_GivingCategoryId",
                table: "Givings",
                column: "GivingCategoryId");

            migrationBuilder.CreateIndex(
                name: "IX_Givings_GivingDate",
                table: "Givings",
                column: "GivingDate");

            migrationBuilder.CreateIndex(
                name: "IX_Givings_MemberId_GivingDate",
                table: "Givings",
                columns: new[] { "MemberId", "GivingDate" });

            migrationBuilder.CreateIndex(
                name: "IX_Givings_OfferingSessionId",
                table: "Givings",
                column: "OfferingSessionId",
                filter: "\"OfferingSessionId\" IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_OfferingSessions_SessionDate",
                table: "OfferingSessions",
                column: "SessionDate",
                unique: true);
        }

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

            migrationBuilder.DropTable(
                name: "GivingCategories");

            migrationBuilder.DropTable(
                name: "OfferingSessions");
        }
    }
}
