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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddFinanceInvoiceModule : 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 每月第一天 00:00:00）。"),
                    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_invoice_records",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    InvoiceNo = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false, comment: "发票号码。"),
                    ApplicantName = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "申请人。"),
                    CompanyName = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false, comment: "开票抬头（公司名）。"),
                    TaxpayerNumber = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true, comment: "纳税人识别号快照。"),
                    InvoiceType = table.Column<int>(type: "integer", nullable: false, comment: "发票类型。"),
                    Amount = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "开票金额。"),
                    OrderNo = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false, comment: "关联订单号。"),
                    ContactEmail = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true, comment: "接收邮箱。"),
                    ContactPhone = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true, comment: "联系电话。"),
                    ApplyRemark = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true, comment: "申请备注。"),
                    Status = table.Column<int>(type: "integer", nullable: false, comment: "发票状态。"),
                    AppliedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "申请时间（UTC）。"),
                    IssuedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "开票时间（UTC）。"),
                    IssuedByUserId = table.Column<long>(type: "bigint", nullable: true, comment: "开票人 ID。"),
                    IssueRemark = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true, comment: "开票备注。"),
                    VoidedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "作废时间（UTC）。"),
                    VoidedByUserId = table.Column<long>(type: "bigint", nullable: true, comment: "作废人 ID。"),
                    VoidReason = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true, 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_invoice_records", x => x.Id);
                },
                comment: "租户发票记录。");

            migrationBuilder.CreateTable(
                name: "finance_invoice_settings",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    CompanyName = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false, comment: "企业名称。"),
                    TaxpayerNumber = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "纳税人识别号。"),
                    RegisteredAddress = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true, comment: "注册地址。"),
                    RegisteredPhone = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true, comment: "注册电话。"),
                    BankName = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true, comment: "开户银行。"),
                    BankAccount = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true, comment: "银行账号。"),
                    EnableElectronicNormalInvoice = table.Column<bool>(type: "boolean", nullable: false, comment: "是否启用电子普通发票。"),
                    EnableElectronicSpecialInvoice = table.Column<bool>(type: "boolean", nullable: false, comment: "是否启用电子专用发票。"),
                    EnableAutoIssue = table.Column<bool>(type: "boolean", nullable: false, comment: "是否启用自动开票。"),
                    AutoIssueMaxAmount = 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_invoice_settings", 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 每月第一天 00:00:00）。"),
                    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" });

            migrationBuilder.CreateIndex(
                name: "IX_finance_invoice_records_TenantId_InvoiceNo",
                table: "finance_invoice_records",
                columns: new[] { "TenantId", "InvoiceNo" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_finance_invoice_records_TenantId_InvoiceType_AppliedAt",
                table: "finance_invoice_records",
                columns: new[] { "TenantId", "InvoiceType", "AppliedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_finance_invoice_records_TenantId_OrderNo",
                table: "finance_invoice_records",
                columns: new[] { "TenantId", "OrderNo" });

            migrationBuilder.CreateIndex(
                name: "IX_finance_invoice_records_TenantId_Status_AppliedAt",
                table: "finance_invoice_records",
                columns: new[] { "TenantId", "Status", "AppliedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_finance_invoice_records_TenantId_Status_IssuedAt",
                table: "finance_invoice_records",
                columns: new[] { "TenantId", "Status", "IssuedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_finance_invoice_settings_TenantId",
                table: "finance_invoice_settings",
                column: "TenantId",
                unique: true);
        }

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

            migrationBuilder.DropTable(
                name: "finance_invoice_records");

            migrationBuilder.DropTable(
                name: "finance_invoice_settings");

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