using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using foodmarket.Infrastructure.Persistence;

#nullable disable

namespace foodmarket.Infrastructure.Persistence.Migrations
{
    /// <summary>Phase7a — pos_batch_acks для идемпотентности /api/pos/sales.
    ///
    /// Хранит сериализованный ответ по (OrgId, IdempotencyKey). Уникальный
    /// индекс — guarantee против двойного создания продаж при ретрае POS'ом.
    /// JSONB-колонка ResponseJson: ответ воссоздаётся атомарно из БД, а не
    /// перестраивается заново.</summary>
    [DbContext(typeof(AppDbContext))]
    [Migration("20260528100000_Phase7a_PosBatchAcks")]
    public partial class Phase7a_PosBatchAcks : Migration
    {
        protected override void Up(MigrationBuilder b)
        {
            b.Sql(@"
                CREATE TABLE IF NOT EXISTS public.pos_batch_acks (
                    ""Id""               uuid PRIMARY KEY,
                    ""OrganizationId""   uuid NOT NULL,
                    ""IdempotencyKey""   uuid NOT NULL,
                    ""ResponseJson""     jsonb NOT NULL,
                    ""CreatedAt""        timestamp with time zone NOT NULL,
                    ""UpdatedAt""        timestamp with time zone
                );

                CREATE UNIQUE INDEX IF NOT EXISTS ""IX_pos_batch_acks_OrganizationId_IdempotencyKey""
                    ON public.pos_batch_acks (""OrganizationId"", ""IdempotencyKey"");
                CREATE INDEX IF NOT EXISTS ""IX_pos_batch_acks_CreatedAt""
                    ON public.pos_batch_acks (""CreatedAt"");
            ");
        }

        protected override void Down(MigrationBuilder b)
        {
            b.Sql(@"DROP TABLE IF EXISTS public.pos_batch_acks;");
        }
    }
}
