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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddMemberCenterModule : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "ColorHex",
                table: "member_tiers",
                type: "character varying(16)",
                maxLength: 16,
                nullable: false,
                defaultValue: "#999999");

            migrationBuilder.AddColumn<int>(
                name: "DowngradeWindowDays",
                table: "member_tiers",
                type: "integer",
                nullable: false,
                defaultValue: 90);

            migrationBuilder.AddColumn<string>(
                name: "IconKey",
                table: "member_tiers",
                type: "character varying(32)",
                maxLength: 32,
                nullable: false,
                defaultValue: "user");

            migrationBuilder.AddColumn<bool>(
                name: "IsDefault",
                table: "member_tiers",
                type: "boolean",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<decimal>(
                name: "UpgradeAmountThreshold",
                table: "member_tiers",
                type: "numeric(18,2)",
                precision: 18,
                scale: 2,
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "UpgradeOrderCountThreshold",
                table: "member_tiers",
                type: "integer",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "UpgradeRuleType",
                table: "member_tiers",
                type: "character varying(16)",
                maxLength: 16,
                nullable: false,
                defaultValue: "none");

            migrationBuilder.AddColumn<decimal>(
                name: "StoredBalance",
                table: "member_profiles",
                type: "numeric(18,2)",
                precision: 18,
                scale: 2,
                nullable: false,
                defaultValue: 0m);

            migrationBuilder.AddColumn<decimal>(
                name: "StoredGiftBalance",
                table: "member_profiles",
                type: "numeric(18,2)",
                precision: 18,
                scale: 2,
                nullable: false,
                defaultValue: 0m);

            migrationBuilder.AddColumn<decimal>(
                name: "StoredRechargeBalance",
                table: "member_profiles",
                type: "numeric(18,2)",
                precision: 18,
                scale: 2,
                nullable: false,
                defaultValue: 0m);

            migrationBuilder.CreateTable(
                name: "member_day_settings",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    IsEnabled = table.Column<bool>(type: "boolean", nullable: false, comment: "是否启用会员日。"),
                    Weekday = table.Column<int>(type: "integer", nullable: false, comment: "周几（1-7）。"),
                    ExtraDiscountRate = table.Column<decimal>(type: "numeric(5,2)", precision: 5, scale: 2, 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_day_settings", x => x.Id);
                },
                comment: "会员日设置。");

            migrationBuilder.CreateTable(
                name: "member_profile_tags",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    MemberProfileId = table.Column<long>(type: "bigint", nullable: false, comment: "会员标识。"),
                    TagName = table.Column<string>(type: "character varying(32)", maxLength: 32, 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_profile_tags", x => x.Id);
                },
                comment: "会员标签。");

            migrationBuilder.CreateIndex(
                name: "IX_member_day_settings_TenantId",
                table: "member_day_settings",
                column: "TenantId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_member_profiles_TenantId_MemberTierId",
                table: "member_profiles",
                columns: new[] { "TenantId", "MemberTierId" });

            migrationBuilder.CreateIndex(
                name: "IX_member_profile_tags_TenantId_MemberProfileId",
                table: "member_profile_tags",
                columns: new[] { "TenantId", "MemberProfileId" });

            migrationBuilder.CreateIndex(
                name: "IX_member_profile_tags_TenantId_MemberProfileId_TagName",
                table: "member_profile_tags",
                columns: new[] { "TenantId", "MemberProfileId", "TagName" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_member_tiers_TenantId_SortOrder",
                table: "member_tiers",
                columns: new[] { "TenantId", "SortOrder" });
        }

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

            migrationBuilder.DropTable(
                name: "member_profile_tags");

            migrationBuilder.DropIndex(
                name: "IX_member_profiles_TenantId_MemberTierId",
                table: "member_profiles");

            migrationBuilder.DropIndex(
                name: "IX_member_tiers_TenantId_SortOrder",
                table: "member_tiers");

            migrationBuilder.DropColumn(
                name: "ColorHex",
                table: "member_tiers");

            migrationBuilder.DropColumn(
                name: "DowngradeWindowDays",
                table: "member_tiers");

            migrationBuilder.DropColumn(
                name: "IconKey",
                table: "member_tiers");

            migrationBuilder.DropColumn(
                name: "IsDefault",
                table: "member_tiers");

            migrationBuilder.DropColumn(
                name: "UpgradeAmountThreshold",
                table: "member_tiers");

            migrationBuilder.DropColumn(
                name: "UpgradeOrderCountThreshold",
                table: "member_tiers");

            migrationBuilder.DropColumn(
                name: "UpgradeRuleType",
                table: "member_tiers");

            migrationBuilder.DropColumn(
                name: "StoredBalance",
                table: "member_profiles");

            migrationBuilder.DropColumn(
                name: "StoredGiftBalance",
                table: "member_profiles");

            migrationBuilder.DropColumn(
                name: "StoredRechargeBalance",
                table: "member_profiles");
        }
    }
}
