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

#nullable disable

namespace ROLAC.API.Migrations
{
    /// <inheritdoc />
    public partial class AddForm990FunctionalExpenses : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "DefaultFunctionalClass",
                table: "Ministries",
                type: "character varying(20)",
                maxLength: 20,
                nullable: false,
                defaultValue: "Program");

            migrationBuilder.AddColumn<int>(
                name: "Form990LineId",
                table: "ExpenseSubCategories",
                type: "integer",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "FunctionalClass",
                table: "Expenses",
                type: "character varying(20)",
                maxLength: 20,
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "Form990LineId",
                table: "ExpenseCategoryGroups",
                type: "integer",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "Form990ExpenseLines",
                columns: table => new
                {
                    Id = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    LineCode = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: false),
                    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),
                    SortOrder = table.Column<int>(type: "integer", nullable: false),
                    IsActive = table.Column<bool>(type: "boolean", 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_Form990ExpenseLines", x => x.Id);
                });

            migrationBuilder.CreateIndex(
                name: "IX_ExpenseSubCategories_Form990LineId",
                table: "ExpenseSubCategories",
                column: "Form990LineId");

            migrationBuilder.CreateIndex(
                name: "IX_ExpenseCategoryGroups_Form990LineId",
                table: "ExpenseCategoryGroups",
                column: "Form990LineId");

            migrationBuilder.CreateIndex(
                name: "IX_Form990ExpenseLines_LineCode",
                table: "Form990ExpenseLines",
                column: "LineCode",
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_ExpenseCategoryGroups_Form990ExpenseLines_Form990LineId",
                table: "ExpenseCategoryGroups",
                column: "Form990LineId",
                principalTable: "Form990ExpenseLines",
                principalColumn: "Id",
                onDelete: ReferentialAction.SetNull);

            migrationBuilder.AddForeignKey(
                name: "FK_ExpenseSubCategories_Form990ExpenseLines_Form990LineId",
                table: "ExpenseSubCategories",
                column: "Form990LineId",
                principalTable: "Form990ExpenseLines",
                principalColumn: "Id",
                onDelete: ReferentialAction.SetNull);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_ExpenseCategoryGroups_Form990ExpenseLines_Form990LineId",
                table: "ExpenseCategoryGroups");

            migrationBuilder.DropForeignKey(
                name: "FK_ExpenseSubCategories_Form990ExpenseLines_Form990LineId",
                table: "ExpenseSubCategories");

            migrationBuilder.DropTable(
                name: "Form990ExpenseLines");

            migrationBuilder.DropIndex(
                name: "IX_ExpenseSubCategories_Form990LineId",
                table: "ExpenseSubCategories");

            migrationBuilder.DropIndex(
                name: "IX_ExpenseCategoryGroups_Form990LineId",
                table: "ExpenseCategoryGroups");

            migrationBuilder.DropColumn(
                name: "DefaultFunctionalClass",
                table: "Ministries");

            migrationBuilder.DropColumn(
                name: "Form990LineId",
                table: "ExpenseSubCategories");

            migrationBuilder.DropColumn(
                name: "FunctionalClass",
                table: "Expenses");

            migrationBuilder.DropColumn(
                name: "Form990LineId",
                table: "ExpenseCategoryGroups");
        }
    }
}
