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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddPunchCardModule : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "punch_card_instances",
                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。"),
                    PunchCardTemplateId = table.Column<long>(type: "bigint", nullable: false, comment: "次卡模板 ID。"),
                    InstanceNo = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false, comment: "实例编号（业务唯一）。"),
                    MemberName = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "会员名称。"),
                    MemberPhoneMasked = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false, comment: "会员手机号（脱敏）。"),
                    PurchasedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "购买时间（UTC）。"),
                    ExpiresAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "过期时间（UTC，可空）。"),
                    TotalTimes = table.Column<int>(type: "integer", nullable: false, comment: "总次数。"),
                    RemainingTimes = table.Column<int>(type: "integer", nullable: false, comment: "剩余次数。"),
                    PaidAmount = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "实付金额。"),
                    Status = table.Column<int>(type: "integer", 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_punch_card_instances", x => x.Id);
                },
                comment: "次卡实例（顾客购买后生成）。");

            migrationBuilder.CreateTable(
                name: "punch_card_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。"),
                    Name = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "次卡名称。"),
                    CoverImageUrl = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true, comment: "封面图片地址。"),
                    SalePrice = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "售价。"),
                    OriginalPrice = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: true, comment: "原价。"),
                    TotalTimes = table.Column<int>(type: "integer", nullable: false, comment: "总次数。"),
                    ValidityType = table.Column<int>(type: "integer", nullable: false, comment: "有效期类型。"),
                    ValidityDays = table.Column<int>(type: "integer", nullable: true, comment: "固定天数（ValidityType=Days 时有效）。"),
                    ValidFrom = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "固定开始日期（UTC，ValidityType=DateRange 时有效）。"),
                    ValidTo = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "固定结束日期（UTC，ValidityType=DateRange 时有效）。"),
                    ScopeType = table.Column<int>(type: "integer", nullable: false, comment: "适用范围类型。"),
                    ScopeCategoryIdsJson = table.Column<string>(type: "text", nullable: false, comment: "指定分类 ID JSON。"),
                    ScopeTagIdsJson = table.Column<string>(type: "text", nullable: false, comment: "指定标签 ID JSON。"),
                    ScopeProductIdsJson = table.Column<string>(type: "text", nullable: false, comment: "指定商品 ID JSON。"),
                    UsageMode = table.Column<int>(type: "integer", nullable: false, comment: "使用模式。"),
                    UsageCapAmount = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: true, comment: "金额上限（UsageMode=Cap 时有效）。"),
                    DailyLimit = table.Column<int>(type: "integer", nullable: true, comment: "每日限用次数。"),
                    PerOrderLimit = table.Column<int>(type: "integer", nullable: true, comment: "每单限用次数。"),
                    PerUserPurchaseLimit = table.Column<int>(type: "integer", nullable: true, comment: "每人限购张数。"),
                    AllowTransfer = table.Column<bool>(type: "boolean", nullable: false, comment: "是否允许转赠。"),
                    ExpireStrategy = table.Column<int>(type: "integer", nullable: false, comment: "过期策略。"),
                    Description = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true, comment: "次卡描述。"),
                    NotifyChannelsJson = table.Column<string>(type: "text", nullable: false, comment: "购买通知渠道 JSON。"),
                    Status = table.Column<int>(type: "integer", 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_punch_card_templates", x => x.Id);
                },
                comment: "次卡模板配置。");

            migrationBuilder.CreateTable(
                name: "punch_card_usage_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: "门店 ID。"),
                    PunchCardTemplateId = table.Column<long>(type: "bigint", nullable: false, comment: "次卡模板 ID。"),
                    PunchCardInstanceId = table.Column<long>(type: "bigint", nullable: false, comment: "次卡实例 ID。"),
                    RecordNo = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false, comment: "使用单号。"),
                    ProductName = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false, comment: "兑换商品名称。"),
                    UsedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "使用时间（UTC）。"),
                    UsedTimes = table.Column<int>(type: "integer", nullable: false, comment: "本次使用次数。"),
                    RemainingTimesAfterUse = table.Column<int>(type: "integer", nullable: false, comment: "使用后剩余次数。"),
                    StatusAfterUse = table.Column<int>(type: "integer", nullable: false, comment: "本次记录状态。"),
                    ExtraPayAmount = table.Column<decimal>(type: "numeric(18,2)", precision: 18, 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_punch_card_usage_records", x => x.Id);
                },
                comment: "次卡使用记录。");

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

            migrationBuilder.CreateIndex(
                name: "IX_punch_card_instances_TenantId_StoreId_PunchCardTemplateId",
                table: "punch_card_instances",
                columns: new[] { "TenantId", "StoreId", "PunchCardTemplateId" });

            migrationBuilder.CreateIndex(
                name: "IX_punch_card_instances_TenantId_StoreId_Status_ExpiresAt",
                table: "punch_card_instances",
                columns: new[] { "TenantId", "StoreId", "Status", "ExpiresAt" });

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

            migrationBuilder.CreateIndex(
                name: "IX_punch_card_templates_TenantId_StoreId_Status",
                table: "punch_card_templates",
                columns: new[] { "TenantId", "StoreId", "Status" });

            migrationBuilder.CreateIndex(
                name: "IX_punch_card_usage_records_TenantId_StoreId_PunchCardInstance~",
                table: "punch_card_usage_records",
                columns: new[] { "TenantId", "StoreId", "PunchCardInstanceId", "UsedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_punch_card_usage_records_TenantId_StoreId_PunchCardTemplate~",
                table: "punch_card_usage_records",
                columns: new[] { "TenantId", "StoreId", "PunchCardTemplateId", "UsedAt" });

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

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

            migrationBuilder.DropTable(
                name: "punch_card_templates");

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