using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using TakeoutSaaS.Infrastructure.Dictionary.Persistence;

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations.DictionaryDb
{
    /// <summary>
    /// 为字典表添加 RowVersion 自动生成触发器。
    /// </summary>
    [DbContext(typeof(DictionaryDbContext))]
    [Migration("20251230170000_AddDictionaryRowVersionTriggers")]
    public partial class AddDictionaryRowVersionTriggers : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // 1. 创建通用的 RowVersion 生成函数
            migrationBuilder.Sql(
                """
                CREATE OR REPLACE FUNCTION public.set_dictionary_row_version()
                RETURNS trigger AS $$
                BEGIN
                    NEW."RowVersion" = decode(md5(random()::text || clock_timestamp()::text), 'hex');
                    RETURN NEW;
                END;
                $$ LANGUAGE plpgsql;
                """);

            // 2. 为 dictionary_groups 表创建触发器
            migrationBuilder.Sql(
                """
                DROP TRIGGER IF EXISTS trg_dictionary_groups_row_version ON dictionary_groups;
                CREATE TRIGGER trg_dictionary_groups_row_version
                BEFORE INSERT OR UPDATE ON dictionary_groups
                FOR EACH ROW EXECUTE FUNCTION public.set_dictionary_row_version();
                """);

            // 3. 为 dictionary_items 表创建触发器
            migrationBuilder.Sql(
                """
                DROP TRIGGER IF EXISTS trg_dictionary_items_row_version ON dictionary_items;
                CREATE TRIGGER trg_dictionary_items_row_version
                BEFORE INSERT OR UPDATE ON dictionary_items
                FOR EACH ROW EXECUTE FUNCTION public.set_dictionary_row_version();
                """);

            // 4. 回填现有 dictionary_groups 数据的 RowVersion
            migrationBuilder.Sql(
                """
                UPDATE dictionary_groups
                SET "RowVersion" = decode(md5(random()::text || clock_timestamp()::text), 'hex')
                WHERE "RowVersion" IS NULL OR octet_length("RowVersion") = 0;
                """);

            // 5. 回填现有 dictionary_items 数据的 RowVersion
            migrationBuilder.Sql(
                """
                UPDATE dictionary_items
                SET "RowVersion" = decode(md5(random()::text || clock_timestamp()::text), 'hex')
                WHERE "RowVersion" IS NULL OR octet_length("RowVersion") = 0;
                """);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql(
                """
                DROP TRIGGER IF EXISTS trg_dictionary_groups_row_version ON dictionary_groups;
                DROP TRIGGER IF EXISTS trg_dictionary_items_row_version ON dictionary_items;
                DROP FUNCTION IF EXISTS public.set_dictionary_row_version();
                """);
        }
    }
}
