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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations.IdentityDb
{
    /// <inheritdoc />
    public partial class InitSnowflake_Identity : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "identity_users",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Account = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "登录账号。"),
                    DisplayName = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "展示名称。"),
                    PasswordHash = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false, comment: "密码哈希。"),
                    MerchantId = table.Column<long>(type: "bigint", nullable: true, comment: "所属商户（平台管理员为空）。"),
                    Roles = table.Column<string>(type: "text", nullable: false, comment: "角色集合。"),
                    Permissions = table.Column<string>(type: "text", nullable: false, comment: "权限集合。"),
                    Avatar = table.Column<string>(type: "character varying(256)", maxLength: 256, 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_identity_users", x => x.Id);
                },
                comment: "管理后台账户实体（平台管理员、租户管理员或商户员工）。");

            migrationBuilder.CreateTable(
                name: "mini_users",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    OpenId = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false, comment: "微信 OpenId。"),
                    UnionId = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true, comment: "微信 UnionId，可能为空。"),
                    Nickname = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "昵称。"),
                    Avatar = table.Column<string>(type: "character varying(256)", maxLength: 256, 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_mini_users", x => x.Id);
                },
                comment: "小程序用户实体。");

            migrationBuilder.CreateIndex(
                name: "IX_identity_users_TenantId",
                table: "identity_users",
                column: "TenantId");

            migrationBuilder.CreateIndex(
                name: "IX_identity_users_TenantId_Account",
                table: "identity_users",
                columns: new[] { "TenantId", "Account" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_mini_users_TenantId",
                table: "mini_users",
                column: "TenantId");

            migrationBuilder.CreateIndex(
                name: "IX_mini_users_TenantId_OpenId",
                table: "mini_users",
                columns: new[] { "TenantId", "OpenId" },
                unique: true);
        }

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

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