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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations.DictionaryDb
{
    /// <inheritdoc />
    public partial class InitSnowflake_Dictionary : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "dictionary_groups",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Code = 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: "分组名称。"),
                    Scope = table.Column<int>(type: "integer", nullable: false, comment: "分组作用域：系统/业务。"),
                    Description = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true, comment: "描述信息。"),
                    IsEnabled = table.Column<bool>(type: "boolean", nullable: false, defaultValue: 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_dictionary_groups", x => x.Id);
                },
                comment: "参数字典分组（系统参数、业务参数）。");

            migrationBuilder.CreateTable(
                name: "dictionary_items",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    GroupId = table.Column<long>(type: "bigint", nullable: false, comment: "关联分组 ID。"),
                    Key = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "字典项键。"),
                    Value = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false, comment: "字典项值。"),
                    IsDefault = table.Column<bool>(type: "boolean", nullable: false, comment: "是否默认项。"),
                    IsEnabled = table.Column<bool>(type: "boolean", nullable: false, defaultValue: true, comment: "是否启用。"),
                    SortOrder = table.Column<int>(type: "integer", nullable: false, defaultValue: 100, comment: "排序值，越小越靠前。"),
                    Description = table.Column<string>(type: "character varying(512)", maxLength: 512, 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_dictionary_items", x => x.Id);
                    table.ForeignKey(
                        name: "FK_dictionary_items_dictionary_groups_GroupId",
                        column: x => x.GroupId,
                        principalTable: "dictionary_groups",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "参数字典项。");

            migrationBuilder.CreateIndex(
                name: "IX_dictionary_groups_TenantId",
                table: "dictionary_groups",
                column: "TenantId");

            migrationBuilder.CreateIndex(
                name: "IX_dictionary_groups_TenantId_Code",
                table: "dictionary_groups",
                columns: new[] { "TenantId", "Code" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_dictionary_items_GroupId_Key",
                table: "dictionary_items",
                columns: new[] { "GroupId", "Key" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_dictionary_items_TenantId",
                table: "dictionary_items",
                column: "TenantId");
        }

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

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