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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations.DictionaryDb
{
    /// <inheritdoc />
    public partial class AddDictionaryLabelOverrides : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "dictionary_label_overrides",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    TenantId = table.Column<long>(type: "bigint", nullable: false, comment: "所属租户 ID（覆盖目标租户）。"),
                    DictionaryItemId = table.Column<long>(type: "bigint", nullable: false, comment: "被覆盖的字典项 ID。"),
                    OriginalValue = table.Column<string>(type: "jsonb", nullable: false, comment: "原始显示值（JSON 格式，多语言）。"),
                    OverrideValue = table.Column<string>(type: "jsonb", nullable: false, comment: "覆盖后的显示值（JSON 格式，多语言）。"),
                    OverrideType = table.Column<int>(type: "integer", nullable: false, comment: "覆盖类型。"),
                    Reason = 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）。"),
                    DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "删除时间（UTC）。"),
                    CreatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "创建人用户标识。"),
                    UpdatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "最后更新人用户标识。"),
                    DeletedBy = table.Column<long>(type: "bigint", nullable: true, comment: "删除人用户标识。")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_dictionary_label_overrides", x => x.Id);
                    table.ForeignKey(
                        name: "FK_dictionary_label_overrides_dictionary_items_DictionaryItemId",
                        column: x => x.DictionaryItemId,
                        principalTable: "dictionary_items",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "字典标签覆盖配置。");

            migrationBuilder.CreateIndex(
                name: "IX_dictionary_label_overrides_DictionaryItemId",
                table: "dictionary_label_overrides",
                column: "DictionaryItemId");

            migrationBuilder.CreateIndex(
                name: "IX_dictionary_label_overrides_TenantId",
                table: "dictionary_label_overrides",
                column: "TenantId");

            migrationBuilder.CreateIndex(
                name: "IX_dictionary_label_overrides_TenantId_DictionaryItemId",
                table: "dictionary_label_overrides",
                columns: new[] { "TenantId", "DictionaryItemId" },
                unique: true,
                filter: "\"DeletedAt\" IS NULL");

            migrationBuilder.CreateIndex(
                name: "IX_dictionary_label_overrides_TenantId_OverrideType",
                table: "dictionary_label_overrides",
                columns: new[] { "TenantId", "OverrideType" });
        }

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