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

#nullable disable

namespace foodmarket.Infrastructure.Persistence.Migrations
{
    /// <summary>Phase6d — инвентаризация (InventoryDoc).
    ///
    /// Документ инвентаризации хранит снимок учётных остатков (bookQty) и
    /// фактические количества (actualQty) с разницей diff = actual - book.
    /// При проведении создаётся корректирующее движение типа
    /// InventoryAdjustment на diff для каждой строки.</summary>
    [DbContext(typeof(AppDbContext))]
    [Migration("20260528030000_Phase6d_Inventories")]
    public partial class Phase6d_Inventories : Migration
    {
        protected override void Up(MigrationBuilder b)
        {
            b.Sql(@"
                CREATE TABLE IF NOT EXISTS public.inventories (
                    ""Id""               uuid PRIMARY KEY,
                    ""OrganizationId""   uuid NOT NULL,
                    ""Number""           varchar(50) NOT NULL,
                    ""Date""             timestamp with time zone NOT NULL,
                    ""Status""           integer NOT NULL,
                    ""StoreId""          uuid NOT NULL,
                    ""Notes""            varchar(1000),
                    ""PostedAt""         timestamp with time zone,
                    ""PostedByUserId""   uuid,
                    ""CreatedAt""        timestamp with time zone NOT NULL,
                    ""UpdatedAt""        timestamp with time zone,
                    CONSTRAINT ""FK_inventories_stores_StoreId"" FOREIGN KEY (""StoreId"") REFERENCES public.stores(""Id"") ON DELETE RESTRICT
                );

                CREATE UNIQUE INDEX IF NOT EXISTS ""IX_inventories_OrganizationId_Number"" ON public.inventories (""OrganizationId"", ""Number"");
                CREATE INDEX IF NOT EXISTS ""IX_inventories_OrganizationId_Date"" ON public.inventories (""OrganizationId"", ""Date"");
                CREATE INDEX IF NOT EXISTS ""IX_inventories_OrganizationId_Status"" ON public.inventories (""OrganizationId"", ""Status"");
                CREATE INDEX IF NOT EXISTS ""IX_inventories_StoreId"" ON public.inventories (""StoreId"");

                CREATE TABLE IF NOT EXISTS public.inventory_lines (
                    ""Id""               uuid PRIMARY KEY,
                    ""OrganizationId""   uuid NOT NULL,
                    ""InventoryDocId""   uuid NOT NULL,
                    ""ProductId""        uuid NOT NULL,
                    ""BookQty""          numeric(18,4) NOT NULL,
                    ""ActualQty""        numeric(18,4) NOT NULL,
                    ""Diff""             numeric(18,4) NOT NULL,
                    ""UnitCost""         numeric(18,4) NOT NULL,
                    ""SortOrder""        integer NOT NULL,
                    ""CreatedAt""        timestamp with time zone NOT NULL,
                    ""UpdatedAt""        timestamp with time zone,
                    CONSTRAINT ""FK_inventory_lines_inventories_InventoryDocId"" FOREIGN KEY (""InventoryDocId"") REFERENCES public.inventories(""Id"") ON DELETE CASCADE,
                    CONSTRAINT ""FK_inventory_lines_products_ProductId"" FOREIGN KEY (""ProductId"") REFERENCES public.products(""Id"") ON DELETE RESTRICT
                );

                CREATE INDEX IF NOT EXISTS ""IX_inventory_lines_InventoryDocId"" ON public.inventory_lines (""InventoryDocId"");
                CREATE INDEX IF NOT EXISTS ""IX_inventory_lines_ProductId"" ON public.inventory_lines (""ProductId"");
                CREATE INDEX IF NOT EXISTS ""IX_inventory_lines_OrganizationId_ProductId"" ON public.inventory_lines (""OrganizationId"", ""ProductId"");
            ");
        }

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