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

#nullable disable

namespace foodmarket.Infrastructure.Persistence.Migrations
{
    /// <summary>Phase6b — списание (Loss).
    ///
    /// Документ списания со склада с указанием причины (LossReason): брак,
    /// просрочка, повреждение, недостача, прочее. При проведении создаёт
    /// <c>stock_movements</c> с типом <c>WriteOff</c> и отрицательным
    /// Quantity.
    ///
    /// Шаги идемпотентны через IF NOT EXISTS.</summary>
    [DbContext(typeof(AppDbContext))]
    [Migration("20260528010000_Phase6b_Losses")]
    public partial class Phase6b_Losses : Migration
    {
        protected override void Up(MigrationBuilder b)
        {
            b.Sql(@"
                CREATE TABLE IF NOT EXISTS public.losses (
                    ""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,
                    ""CurrencyId""       uuid NOT NULL,
                    ""Reason""           integer NOT NULL,
                    ""Notes""            varchar(1000),
                    ""Total""            numeric(18,4) NOT NULL,
                    ""PostedAt""         timestamp with time zone,
                    ""PostedByUserId""   uuid,
                    ""CreatedAt""        timestamp with time zone NOT NULL,
                    ""UpdatedAt""        timestamp with time zone,
                    CONSTRAINT ""FK_losses_stores_StoreId"" FOREIGN KEY (""StoreId"") REFERENCES public.stores(""Id"") ON DELETE RESTRICT,
                    CONSTRAINT ""FK_losses_currencies_CurrencyId"" FOREIGN KEY (""CurrencyId"") REFERENCES public.currencies(""Id"") ON DELETE RESTRICT
                );

                CREATE UNIQUE INDEX IF NOT EXISTS ""IX_losses_OrganizationId_Number"" ON public.losses (""OrganizationId"", ""Number"");
                CREATE INDEX IF NOT EXISTS ""IX_losses_OrganizationId_Date"" ON public.losses (""OrganizationId"", ""Date"");
                CREATE INDEX IF NOT EXISTS ""IX_losses_OrganizationId_Status"" ON public.losses (""OrganizationId"", ""Status"");
                CREATE INDEX IF NOT EXISTS ""IX_losses_OrganizationId_Reason"" ON public.losses (""OrganizationId"", ""Reason"");
                CREATE INDEX IF NOT EXISTS ""IX_losses_StoreId"" ON public.losses (""StoreId"");
                CREATE INDEX IF NOT EXISTS ""IX_losses_CurrencyId"" ON public.losses (""CurrencyId"");

                CREATE TABLE IF NOT EXISTS public.loss_lines (
                    ""Id""               uuid PRIMARY KEY,
                    ""OrganizationId""   uuid NOT NULL,
                    ""LossId""           uuid NOT NULL,
                    ""ProductId""        uuid NOT NULL,
                    ""Quantity""         numeric(18,4) NOT NULL,
                    ""UnitCost""         numeric(18,4) NOT NULL,
                    ""LineTotal""        numeric(18,4) NOT NULL,
                    ""SortOrder""        integer NOT NULL,
                    ""CreatedAt""        timestamp with time zone NOT NULL,
                    ""UpdatedAt""        timestamp with time zone,
                    CONSTRAINT ""FK_loss_lines_losses_LossId"" FOREIGN KEY (""LossId"") REFERENCES public.losses(""Id"") ON DELETE CASCADE,
                    CONSTRAINT ""FK_loss_lines_products_ProductId"" FOREIGN KEY (""ProductId"") REFERENCES public.products(""Id"") ON DELETE RESTRICT
                );

                CREATE INDEX IF NOT EXISTS ""IX_loss_lines_LossId"" ON public.loss_lines (""LossId"");
                CREATE INDEX IF NOT EXISTS ""IX_loss_lines_ProductId"" ON public.loss_lines (""ProductId"");
                CREATE INDEX IF NOT EXISTS ""IX_loss_lines_OrganizationId_ProductId"" ON public.loss_lines (""OrganizationId"", ""ProductId"");
            ");
        }

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