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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddMemberStoredCardModule : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "member_stored_card_plans",
                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: "门店标识。"),
                    RechargeAmount = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "充值金额。"),
                    GiftAmount = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "赠送金额。"),
                    SortOrder = table.Column<int>(type: "integer", 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_member_stored_card_plans", x => x.Id);
                },
                comment: "会员储值卡充值方案。");

            migrationBuilder.CreateTable(
                name: "member_stored_card_recharge_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: "门店标识。"),
                    MemberId = table.Column<long>(type: "bigint", nullable: false, comment: "会员标识。"),
                    PlanId = table.Column<long>(type: "bigint", nullable: true, comment: "充值方案标识（可空，表示非方案充值）。"),
                    RecordNo = 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: "会员名称快照。"),
                    MemberMobileMasked = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false, comment: "会员手机号快照（脱敏）。"),
                    RechargeAmount = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "充值金额。"),
                    GiftAmount = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "赠送金额。"),
                    ArrivedAmount = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "到账金额（充值+赠送）。"),
                    PaymentMethod = table.Column<int>(type: "integer", nullable: false, comment: "支付方式。"),
                    Remark = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true, comment: "备注。"),
                    RechargedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "充值时间（UTC）。"),
                    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_stored_card_recharge_records", x => x.Id);
                },
                comment: "会员储值卡充值记录。");

            migrationBuilder.CreateIndex(
                name: "IX_member_stored_card_plans_TenantId_StoreId_RechargeAmount_Gi~",
                table: "member_stored_card_plans",
                columns: new[] { "TenantId", "StoreId", "RechargeAmount", "GiftAmount" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_member_stored_card_plans_TenantId_StoreId_SortOrder",
                table: "member_stored_card_plans",
                columns: new[] { "TenantId", "StoreId", "SortOrder" });

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

            migrationBuilder.CreateIndex(
                name: "IX_member_stored_card_recharge_records_TenantId_StoreId_Member~",
                table: "member_stored_card_recharge_records",
                columns: new[] { "TenantId", "StoreId", "MemberId", "RechargedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_member_stored_card_recharge_records_TenantId_StoreId_PlanId~",
                table: "member_stored_card_recharge_records",
                columns: new[] { "TenantId", "StoreId", "PlanId", "RechargedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_member_stored_card_recharge_records_TenantId_StoreId_Rechar~",
                table: "member_stored_card_recharge_records",
                columns: new[] { "TenantId", "StoreId", "RechargedAt" });

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

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

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