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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddMemberPointsMallModule : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "member_point_mall_products",
                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: "门店标识。"),
                    Name = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "展示名称。"),
                    ImageUrl = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true, comment: "展示图片地址。"),
                    RedeemType = table.Column<int>(type: "integer", nullable: false, comment: "兑换类型。"),
                    ProductId = table.Column<long>(type: "bigint", nullable: true, comment: "关联商品 ID（兑换商品时必填）。"),
                    CouponTemplateId = table.Column<long>(type: "bigint", nullable: true, comment: "关联优惠券模板 ID（兑换优惠券时必填）。"),
                    PhysicalName = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true, comment: "实物名称（兑换实物时必填）。"),
                    PickupMethod = table.Column<int>(type: "integer", nullable: true, comment: "实物领取方式。"),
                    Description = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true, comment: "商品描述。"),
                    ExchangeType = table.Column<int>(type: "integer", nullable: false, comment: "兑换方式（纯积分/积分+现金）。"),
                    RequiredPoints = table.Column<int>(type: "integer", nullable: false, comment: "所需积分。"),
                    CashAmount = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "现金部分（积分+现金时使用）。"),
                    StockTotal = table.Column<int>(type: "integer", nullable: false, comment: "初始库存数量。"),
                    StockAvailable = table.Column<int>(type: "integer", nullable: false, comment: "剩余库存数量。"),
                    PerMemberLimit = table.Column<int>(type: "integer", nullable: true, comment: "每人限兑次数（null 表示不限）。"),
                    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_member_point_mall_products", x => x.Id);
                },
                comment: "会员积分商城兑换商品。");

            migrationBuilder.CreateTable(
                name: "member_point_mall_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: "门店标识。"),
                    RecordNo = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false, comment: "兑换记录单号。"),
                    PointMallProductId = table.Column<long>(type: "bigint", nullable: false, comment: "关联积分商品 ID。"),
                    MemberId = table.Column<long>(type: "bigint", nullable: false, comment: "会员标识。"),
                    MemberName = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "会员名称快照。"),
                    MemberMobileMasked = 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: "商品名称快照。"),
                    RedeemType = table.Column<int>(type: "integer", nullable: false, comment: "兑换类型快照。"),
                    ExchangeType = table.Column<int>(type: "integer", nullable: false, comment: "兑换方式快照。"),
                    UsedPoints = table.Column<int>(type: "integer", nullable: false, comment: "消耗积分。"),
                    CashAmount = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "现金部分。"),
                    Status = table.Column<int>(type: "integer", nullable: false, comment: "记录状态。"),
                    RedeemedAt = 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）。"),
                    VerifiedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "核销时间（UTC）。"),
                    VerifyMethod = table.Column<int>(type: "integer", nullable: true, comment: "核销方式。"),
                    VerifyRemark = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true, comment: "核销备注。"),
                    VerifiedBy = table.Column<long>(type: "bigint", 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_member_point_mall_records", x => x.Id);
                },
                comment: "会员积分商城兑换记录。");

            migrationBuilder.CreateTable(
                name: "member_point_mall_rules",
                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: "门店标识。"),
                    IsConsumeRewardEnabled = table.Column<bool>(type: "boolean", nullable: false, comment: "是否启用消费获取。"),
                    ConsumeAmountPerStep = table.Column<int>(type: "integer", nullable: false, comment: "每消费多少元触发一次积分计算。"),
                    ConsumeRewardPointsPerStep = table.Column<int>(type: "integer", nullable: false, comment: "每步获得积分。"),
                    IsReviewRewardEnabled = table.Column<bool>(type: "boolean", nullable: false, comment: "是否启用评价奖励。"),
                    ReviewRewardPoints = table.Column<int>(type: "integer", nullable: false, comment: "评价奖励积分。"),
                    IsRegisterRewardEnabled = table.Column<bool>(type: "boolean", nullable: false, comment: "是否启用注册奖励。"),
                    RegisterRewardPoints = table.Column<int>(type: "integer", nullable: false, comment: "注册奖励积分。"),
                    IsSigninRewardEnabled = table.Column<bool>(type: "boolean", nullable: false, comment: "是否启用签到奖励。"),
                    SigninRewardPoints = table.Column<int>(type: "integer", nullable: false, comment: "签到奖励积分。"),
                    ExpiryMode = 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_member_point_mall_rules", x => x.Id);
                },
                comment: "会员积分商城规则配置。");

            migrationBuilder.CreateIndex(
                name: "IX_member_point_mall_products_TenantId_StoreId_CouponTemplateId",
                table: "member_point_mall_products",
                columns: new[] { "TenantId", "StoreId", "CouponTemplateId" });

            migrationBuilder.CreateIndex(
                name: "IX_member_point_mall_products_TenantId_StoreId_Name",
                table: "member_point_mall_products",
                columns: new[] { "TenantId", "StoreId", "Name" });

            migrationBuilder.CreateIndex(
                name: "IX_member_point_mall_products_TenantId_StoreId_ProductId",
                table: "member_point_mall_products",
                columns: new[] { "TenantId", "StoreId", "ProductId" });

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

            migrationBuilder.CreateIndex(
                name: "IX_member_point_mall_records_TenantId_StoreId_MemberId_RedeemedAt",
                table: "member_point_mall_records",
                columns: new[] { "TenantId", "StoreId", "MemberId", "RedeemedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_member_point_mall_records_TenantId_StoreId_PointMallProductId_RedeemedAt",
                table: "member_point_mall_records",
                columns: new[] { "TenantId", "StoreId", "PointMallProductId", "RedeemedAt" });

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

            migrationBuilder.CreateIndex(
                name: "IX_member_point_mall_records_TenantId_StoreId_RedeemedAt",
                table: "member_point_mall_records",
                columns: new[] { "TenantId", "StoreId", "RedeemedAt" });

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

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

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

            migrationBuilder.DropTable(
                name: "member_point_mall_records");

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