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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddStoreExtendedSettings : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "FineRuleJson",
                table: "store_pickup_settings",
                type: "text",
                nullable: true,
                comment: "精细规则 JSON。");

            migrationBuilder.AddColumn<int>(
                name: "Mode",
                table: "store_pickup_settings",
                type: "integer",
                nullable: false,
                defaultValue: 0,
                comment: "自提配置模式。");

            migrationBuilder.AddColumn<decimal>(
                name: "CutleryFeeAmount",
                table: "store_fees",
                type: "numeric(10,2)",
                precision: 10,
                scale: 2,
                nullable: false,
                defaultValue: 0m,
                comment: "餐具费金额。");

            migrationBuilder.AddColumn<bool>(
                name: "CutleryFeeEnabled",
                table: "store_fees",
                type: "boolean",
                nullable: false,
                defaultValue: false,
                comment: "是否启用餐具费。");

            migrationBuilder.AddColumn<decimal>(
                name: "RushFeeAmount",
                table: "store_fees",
                type: "numeric(10,2)",
                precision: 10,
                scale: 2,
                nullable: false,
                defaultValue: 0m,
                comment: "加急费金额。");

            migrationBuilder.AddColumn<bool>(
                name: "RushFeeEnabled",
                table: "store_fees",
                type: "boolean",
                nullable: false,
                defaultValue: false,
                comment: "是否启用加急费。");

            migrationBuilder.AddColumn<string>(
                name: "Color",
                table: "store_delivery_zones",
                type: "character varying(32)",
                maxLength: 32,
                nullable: true,
                comment: "区域颜色。");

            migrationBuilder.AddColumn<int>(
                name: "Priority",
                table: "store_delivery_zones",
                type: "integer",
                nullable: false,
                defaultValue: 100,
                comment: "优先级（数值越小越优先）。");

            migrationBuilder.CreateTable(
                name: "store_delivery_settings",
                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: "门店 ID。"),
                    Mode = table.Column<int>(type: "integer", nullable: false, comment: "配送模式。"),
                    EtaAdjustmentMinutes = table.Column<int>(type: "integer", nullable: false, comment: "配送时效加成（分钟）。"),
                    FreeDeliveryThreshold = table.Column<decimal>(type: "numeric(10,2)", precision: 10, scale: 2, nullable: true, comment: "免配送费门槛。"),
                    HourlyCapacityLimit = table.Column<int>(type: "integer", nullable: false, comment: "每小时配送上限。"),
                    MaxDeliveryDistance = table.Column<decimal>(type: "numeric(10,2)", precision: 10, scale: 2, nullable: false, comment: "最大配送距离（公里）。"),
                    RadiusTiersJson = table.Column<string>(type: "text", nullable: true, comment: "半径梯度配置 JSON。"),
                    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_delivery_settings", x => x.Id);
                },
                comment: "门店配送设置聚合。");

            migrationBuilder.CreateTable(
                name: "store_dinein_settings",
                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: "门店 ID。"),
                    Enabled = table.Column<bool>(type: "boolean", nullable: false, defaultValue: true, comment: "是否启用堂食。"),
                    DefaultDiningMinutes = table.Column<int>(type: "integer", nullable: false, defaultValue: 90, comment: "默认用餐时长（分钟）。"),
                    OvertimeReminderMinutes = table.Column<int>(type: "integer", nullable: false, defaultValue: 10, 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_dinein_settings", x => x.Id);
                },
                comment: "门店堂食基础设置。");

            migrationBuilder.CreateTable(
                name: "store_staff_templates",
                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: "门店 ID。"),
                    MorningStartTime = table.Column<TimeSpan>(type: "interval", nullable: false, comment: "早班开始时间。"),
                    MorningEndTime = table.Column<TimeSpan>(type: "interval", nullable: false, comment: "早班结束时间。"),
                    EveningStartTime = table.Column<TimeSpan>(type: "interval", nullable: false, comment: "晚班开始时间。"),
                    EveningEndTime = table.Column<TimeSpan>(type: "interval", nullable: false, comment: "晚班结束时间。"),
                    FullStartTime = table.Column<TimeSpan>(type: "interval", nullable: false, comment: "全天班开始时间。"),
                    FullEndTime = table.Column<TimeSpan>(type: "interval", 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_store_staff_templates", x => x.Id);
                },
                comment: "门店员工班次模板。");

            migrationBuilder.CreateTable(
                name: "store_staff_weekly_schedules",
                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: "门店 ID。"),
                    StaffId = table.Column<long>(type: "bigint", nullable: false, comment: "员工 ID。"),
                    DayOfWeek = table.Column<int>(type: "integer", nullable: false, comment: "星期（0=周一，6=周日）。"),
                    ShiftType = table.Column<int>(type: "integer", nullable: false, comment: "班次类型。"),
                    StartTime = table.Column<TimeSpan>(type: "interval", nullable: true, comment: "开始时间（休息时为空）。"),
                    EndTime = table.Column<TimeSpan>(type: "interval", 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_staff_weekly_schedules", x => x.Id);
                },
                comment: "门店员工每周排班。");

            migrationBuilder.CreateTable(
                name: "tenant_visibility_role_rules",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    QuotaVisibleRoleCodes = table.Column<string[]>(type: "text[]", nullable: false, comment: "配额可见角色编码集合。"),
                    BillingVisibleRoleCodes = table.Column<string[]>(type: "text[]", 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: false, 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: false, 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_tenant_visibility_role_rules", x => x.Id);
                },
                comment: "租户账单/配额可见角色规则。");

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

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

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

            migrationBuilder.CreateIndex(
                name: "IX_store_staff_weekly_schedules_TenantId_StoreId_DayOfWeek",
                table: "store_staff_weekly_schedules",
                columns: new[] { "TenantId", "StoreId", "DayOfWeek" });

            migrationBuilder.CreateIndex(
                name: "IX_store_staff_weekly_schedules_TenantId_StoreId_StaffId_DayOf~",
                table: "store_staff_weekly_schedules",
                columns: new[] { "TenantId", "StoreId", "StaffId", "DayOfWeek" },
                unique: true);

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

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

            migrationBuilder.DropTable(
                name: "store_dinein_settings");

            migrationBuilder.DropTable(
                name: "store_staff_templates");

            migrationBuilder.DropTable(
                name: "store_staff_weekly_schedules");

            migrationBuilder.DropTable(
                name: "tenant_visibility_role_rules");

            migrationBuilder.DropColumn(
                name: "FineRuleJson",
                table: "store_pickup_settings");

            migrationBuilder.DropColumn(
                name: "Mode",
                table: "store_pickup_settings");

            migrationBuilder.DropColumn(
                name: "CutleryFeeAmount",
                table: "store_fees");

            migrationBuilder.DropColumn(
                name: "CutleryFeeEnabled",
                table: "store_fees");

            migrationBuilder.DropColumn(
                name: "RushFeeAmount",
                table: "store_fees");

            migrationBuilder.DropColumn(
                name: "RushFeeEnabled",
                table: "store_fees");

            migrationBuilder.DropColumn(
                name: "Color",
                table: "store_delivery_zones");

            migrationBuilder.DropColumn(
                name: "Priority",
                table: "store_delivery_zones");
        }
    }
}
