using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using TakeoutSaaS.Infrastructure.App.Persistence;

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations;

/// <summary>
/// 新增财务中心成本管理表结构。
/// </summary>
[DbContext(typeof(TakeoutAppDbContext))]
[Migration("20260305010000_AddFinanceCostModule")]
public sealed class AddFinanceCostModule : Migration
{
    /// <inheritdoc />
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "finance_cost_entries",
            columns: table => new
            {
                Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                    .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                Dimension = table.Column<int>(type: "integer", nullable: false, comment: "统计维度。"),
                StoreId = table.Column<long>(type: "bigint", nullable: true, comment: "门店标识（租户汇总维度为空）。"),
                CostMonth = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "成本月份（UTC 每月第一天）。"),
                Category = table.Column<int>(type: "integer", nullable: false, comment: "成本分类。"),
                TotalAmount = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "分类总金额。"),
                CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间（UTC）。"),
                UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "最近一次更新时间（UTC），从未更新时为 null。"),
                DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "软删除时间（UTC），未删除时为 null。"),
                CreatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "创建人用户标识，匿名或系统操作时为 null。"),
                UpdatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "最后更新人用户标识，匿名或系统操作时为 null。"),
                DeletedBy = table.Column<long>(type: "bigint", nullable: true, comment: "删除人用户标识（软删除），未删除时为 null。"),
                TenantId = table.Column<long>(type: "bigint", nullable: false, comment: "所属租户 ID。")
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_finance_cost_entries", x => x.Id);
            },
            comment: "财务成本录入月度汇总。");

        migrationBuilder.CreateTable(
            name: "finance_cost_entry_items",
            columns: table => new
            {
                Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                    .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                EntryId = table.Column<long>(type: "bigint", nullable: false, comment: "关联汇总标识。"),
                Dimension = table.Column<int>(type: "integer", nullable: false, comment: "统计维度。"),
                StoreId = table.Column<long>(type: "bigint", nullable: true, comment: "门店标识（租户汇总维度为空）。"),
                CostMonth = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "成本月份（UTC 每月第一天）。"),
                Category = table.Column<int>(type: "integer", nullable: false, comment: "成本分类。"),
                ItemName = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "明细名称。"),
                Amount = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "明细金额。"),
                Quantity = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: true, comment: "数量（人工类可用）。"),
                UnitPrice = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: true, comment: "单价（人工类可用）。"),
                SortOrder = table.Column<int>(type: "integer", nullable: false, defaultValue: 100, comment: "排序值。"),
                CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间（UTC）。"),
                UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "最近一次更新时间（UTC），从未更新时为 null。"),
                DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "软删除时间（UTC），未删除时为 null。"),
                CreatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "创建人用户标识，匿名或系统操作时为 null。"),
                UpdatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "最后更新人用户标识，匿名或系统操作时为 null。"),
                DeletedBy = table.Column<long>(type: "bigint", nullable: true, comment: "删除人用户标识（软删除），未删除时为 null。"),
                TenantId = table.Column<long>(type: "bigint", nullable: false, comment: "所属租户 ID。")
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_finance_cost_entry_items", x => x.Id);
                table.ForeignKey(
                    name: "FK_finance_cost_entry_items_finance_cost_entries_EntryId",
                    column: x => x.EntryId,
                    principalTable: "finance_cost_entries",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
            },
            comment: "财务成本录入明细项。");

        migrationBuilder.CreateIndex(
            name: "IX_finance_cost_entries_TenantId_Dimension_StoreId_CostMonth",
            table: "finance_cost_entries",
            columns: new[] { "TenantId", "Dimension", "StoreId", "CostMonth" });

        migrationBuilder.CreateIndex(
            name: "IX_finance_cost_entries_TenantId_Dimension_StoreId_CostMonth_C~",
            table: "finance_cost_entries",
            columns: new[] { "TenantId", "Dimension", "StoreId", "CostMonth", "Category" },
            unique: true);

        migrationBuilder.CreateIndex(
            name: "IX_finance_cost_entry_items_EntryId",
            table: "finance_cost_entry_items",
            column: "EntryId");

        migrationBuilder.CreateIndex(
            name: "IX_finance_cost_entry_items_TenantId_Dimension_StoreId_CostMon~",
            table: "finance_cost_entry_items",
            columns: new[] { "TenantId", "Dimension", "StoreId", "CostMonth", "Category", "SortOrder" });
    }

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

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