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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations.IdentityDb
{
    /// <inheritdoc />
    public partial class AddRoleTemplateTables : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "role_template_permissions",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    RoleTemplateId = table.Column<long>(type: "bigint", nullable: false, comment: "模板 ID。"),
                    PermissionCode = table.Column<string>(type: "character varying(128)", maxLength: 128, 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。")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_role_template_permissions", x => x.Id);
                },
                comment: "角色模板-权限关系（平台级）。");

            migrationBuilder.CreateTable(
                name: "role_templates",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    TemplateCode = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "模板编码（唯一）。"),
                    Name = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false, comment: "模板名称。"),
                    Description = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true, comment: "模板描述。"),
                    IsActive = table.Column<bool>(type: "boolean", 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。")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_role_templates", x => x.Id);
                },
                comment: "角色模板定义（平台级）。");

            migrationBuilder.CreateIndex(
                name: "IX_role_template_permissions_RoleTemplateId_PermissionCode",
                table: "role_template_permissions",
                columns: new[] { "RoleTemplateId", "PermissionCode" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_role_templates_TemplateCode",
                table: "role_templates",
                column: "TemplateCode",
                unique: true);
        }

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

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