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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddStoreManagementEntities : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<DateTime>(
                name: "ActivatedAt",
                table: "stores",
                type: "timestamp with time zone",
                nullable: true,
                comment: "审核通过时间。");

            migrationBuilder.AddColumn<int>(
                name: "AuditStatus",
                table: "stores",
                type: "integer",
                nullable: false,
                defaultValue: 0,
                comment: "审核状态。");

            migrationBuilder.AddColumn<int>(
                name: "BusinessStatus",
                table: "stores",
                type: "integer",
                nullable: false,
                defaultValue: 0,
                comment: "经营状态。");

            migrationBuilder.AddColumn<long>(
                name: "CategoryId",
                table: "stores",
                type: "bigint",
                nullable: true,
                comment: "行业类目 ID。");

            migrationBuilder.AddColumn<int>(
                name: "ClosureReason",
                table: "stores",
                type: "integer",
                nullable: true,
                comment: "歇业原因。");

            migrationBuilder.AddColumn<string>(
                name: "ClosureReasonText",
                table: "stores",
                type: "character varying(500)",
                maxLength: 500,
                nullable: true,
                comment: "歇业原因补充说明。");

            migrationBuilder.AddColumn<string>(
                name: "ForceCloseReason",
                table: "stores",
                type: "character varying(500)",
                maxLength: 500,
                nullable: true,
                comment: "强制关闭原因。");

            migrationBuilder.AddColumn<DateTime>(
                name: "ForceClosedAt",
                table: "stores",
                type: "timestamp with time zone",
                nullable: true,
                comment: "强制关闭时间。");

            migrationBuilder.AddColumn<int>(
                name: "OwnershipType",
                table: "stores",
                type: "integer",
                nullable: false,
                defaultValue: 0,
                comment: "主体类型。");

            migrationBuilder.AddColumn<string>(
                name: "RejectionReason",
                table: "stores",
                type: "character varying(500)",
                maxLength: 500,
                nullable: true,
                comment: "审核驳回原因。");

            migrationBuilder.AddColumn<string>(
                name: "SignboardImageUrl",
                table: "stores",
                type: "character varying(500)",
                maxLength: 500,
                nullable: true,
                comment: "门头招牌图 URL。");

            migrationBuilder.AddColumn<DateTime>(
                name: "SubmittedAt",
                table: "stores",
                type: "timestamp with time zone",
                nullable: true,
                comment: "提交审核时间。");

            migrationBuilder.CreateTable(
                name: "store_audit_records",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    StoreId = table.Column<long>(type: "bigint", nullable: false, comment: "门店标识。"),
                    Action = table.Column<int>(type: "integer", nullable: false, comment: "操作类型。"),
                    PreviousStatus = table.Column<int>(type: "integer", nullable: true, comment: "操作前状态。"),
                    NewStatus = table.Column<int>(type: "integer", nullable: false, comment: "操作后状态。"),
                    OperatorId = table.Column<long>(type: "bigint", nullable: true, comment: "操作人 ID。"),
                    OperatorName = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false, comment: "操作人名称。"),
                    RejectionReasonId = table.Column<long>(type: "bigint", nullable: true, comment: "驳回理由 ID。"),
                    RejectionReason = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true, comment: "驳回理由文本。"),
                    Remarks = table.Column<string>(type: "character varying(1000)", maxLength: 1000, 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_store_audit_records", x => x.Id);
                },
                comment: "门店审核记录。");

            migrationBuilder.CreateTable(
                name: "store_fees",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    StoreId = table.Column<long>(type: "bigint", nullable: false, comment: "门店标识。"),
                    MinimumOrderAmount = table.Column<decimal>(type: "numeric(10,2)", precision: 10, scale: 2, nullable: false, comment: "起送费（元）。"),
                    BaseDeliveryFee = table.Column<decimal>(type: "numeric(10,2)", precision: 10, scale: 2, nullable: false, comment: "基础配送费（元）。"),
                    PackagingFeeMode = table.Column<int>(type: "integer", nullable: false, comment: "打包费模式。"),
                    FixedPackagingFee = table.Column<decimal>(type: "numeric(10,2)", precision: 10, scale: 2, nullable: false, comment: "固定打包费（总计模式有效）。"),
                    FreeDeliveryThreshold = table.Column<decimal>(type: "numeric(10,2)", precision: 10, scale: 2, 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_store_fees", x => x.Id);
                },
                comment: "门店费用配置。");

            migrationBuilder.CreateTable(
                name: "store_qualifications",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    StoreId = table.Column<long>(type: "bigint", nullable: false, comment: "门店标识。"),
                    QualificationType = table.Column<int>(type: "integer", nullable: false, comment: "资质类型。"),
                    FileUrl = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: false, comment: "证照文件 URL。"),
                    DocumentNumber = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true, comment: "证照编号。"),
                    IssuedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "签发日期。"),
                    ExpiresAt = table.Column<DateTime>(type: "timestamp with time zone", 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_store_qualifications", x => x.Id);
                },
                comment: "门店资质证照。");

            migrationBuilder.CreateIndex(
                name: "IX_stores_Longitude_Latitude",
                table: "stores",
                columns: new[] { "Longitude", "Latitude" },
                filter: "\"Longitude\" IS NOT NULL AND \"Latitude\" IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_stores_TenantId_AuditStatus",
                table: "stores",
                columns: new[] { "TenantId", "AuditStatus" });

            migrationBuilder.CreateIndex(
                name: "IX_stores_TenantId_BusinessStatus",
                table: "stores",
                columns: new[] { "TenantId", "BusinessStatus" });

            migrationBuilder.CreateIndex(
                name: "IX_stores_TenantId_OwnershipType",
                table: "stores",
                columns: new[] { "TenantId", "OwnershipType" });

            migrationBuilder.CreateIndex(
                name: "IX_store_audit_records_CreatedAt",
                table: "store_audit_records",
                column: "CreatedAt");

            migrationBuilder.CreateIndex(
                name: "IX_store_audit_records_TenantId_StoreId",
                table: "store_audit_records",
                columns: new[] { "TenantId", "StoreId" });

            migrationBuilder.CreateIndex(
                name: "IX_store_fees_TenantId",
                table: "store_fees",
                column: "TenantId");

            migrationBuilder.CreateIndex(
                name: "IX_store_fees_TenantId_StoreId",
                table: "store_fees",
                columns: new[] { "TenantId", "StoreId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_store_qualifications_ExpiresAt",
                table: "store_qualifications",
                column: "ExpiresAt",
                filter: "\"ExpiresAt\" IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_store_qualifications_TenantId_StoreId",
                table: "store_qualifications",
                columns: new[] { "TenantId", "StoreId" });
        }

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

            migrationBuilder.DropTable(
                name: "store_fees");

            migrationBuilder.DropTable(
                name: "store_qualifications");

            migrationBuilder.DropIndex(
                name: "IX_stores_Longitude_Latitude",
                table: "stores");

            migrationBuilder.DropIndex(
                name: "IX_stores_TenantId_AuditStatus",
                table: "stores");

            migrationBuilder.DropIndex(
                name: "IX_stores_TenantId_BusinessStatus",
                table: "stores");

            migrationBuilder.DropIndex(
                name: "IX_stores_TenantId_OwnershipType",
                table: "stores");

            migrationBuilder.DropColumn(
                name: "ActivatedAt",
                table: "stores");

            migrationBuilder.DropColumn(
                name: "AuditStatus",
                table: "stores");

            migrationBuilder.DropColumn(
                name: "BusinessStatus",
                table: "stores");

            migrationBuilder.DropColumn(
                name: "CategoryId",
                table: "stores");

            migrationBuilder.DropColumn(
                name: "ClosureReason",
                table: "stores");

            migrationBuilder.DropColumn(
                name: "ClosureReasonText",
                table: "stores");

            migrationBuilder.DropColumn(
                name: "ForceCloseReason",
                table: "stores");

            migrationBuilder.DropColumn(
                name: "ForceClosedAt",
                table: "stores");

            migrationBuilder.DropColumn(
                name: "OwnershipType",
                table: "stores");

            migrationBuilder.DropColumn(
                name: "RejectionReason",
                table: "stores");

            migrationBuilder.DropColumn(
                name: "SignboardImageUrl",
                table: "stores");

            migrationBuilder.DropColumn(
                name: "SubmittedAt",
                table: "stores");
        }
    }
}
