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

#nullable disable

namespace foodmarket.Infrastructure.Persistence.Migrations
{
    /// <summary>Phase6c — перемещение (Transfer).
    ///
    /// Документ перемещения товара между складами одной организации. При
    /// проведении создаётся ПАРА движений в stock_movements: TransferOut
    /// (-Qty из FromStore) + TransferIn (+Qty в ToStore). Атомарно.</summary>
    [DbContext(typeof(AppDbContext))]
    [Migration("20260528020000_Phase6c_Transfers")]
    public partial class Phase6c_Transfers : Migration
    {
        protected override void Up(MigrationBuilder b)
        {
            b.Sql(@"
                CREATE TABLE IF NOT EXISTS public.transfers (
                    ""Id""               uuid PRIMARY KEY,
                    ""OrganizationId""   uuid NOT NULL,
                    ""Number""           varchar(50) NOT NULL,
                    ""Date""             timestamp with time zone NOT NULL,
                    ""Status""           integer NOT NULL,
                    ""FromStoreId""      uuid NOT NULL,
                    ""ToStoreId""        uuid 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_transfers_stores_FromStoreId"" FOREIGN KEY (""FromStoreId"") REFERENCES public.stores(""Id"") ON DELETE RESTRICT,
                    CONSTRAINT ""FK_transfers_stores_ToStoreId"" FOREIGN KEY (""ToStoreId"") REFERENCES public.stores(""Id"") ON DELETE RESTRICT
                );

                CREATE UNIQUE INDEX IF NOT EXISTS ""IX_transfers_OrganizationId_Number"" ON public.transfers (""OrganizationId"", ""Number"");
                CREATE INDEX IF NOT EXISTS ""IX_transfers_OrganizationId_Date"" ON public.transfers (""OrganizationId"", ""Date"");
                CREATE INDEX IF NOT EXISTS ""IX_transfers_OrganizationId_Status"" ON public.transfers (""OrganizationId"", ""Status"");
                CREATE INDEX IF NOT EXISTS ""IX_transfers_FromStoreId"" ON public.transfers (""FromStoreId"");
                CREATE INDEX IF NOT EXISTS ""IX_transfers_ToStoreId"" ON public.transfers (""ToStoreId"");

                CREATE TABLE IF NOT EXISTS public.transfer_lines (
                    ""Id""               uuid PRIMARY KEY,
                    ""OrganizationId""   uuid NOT NULL,
                    ""TransferId""       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_transfer_lines_transfers_TransferId"" FOREIGN KEY (""TransferId"") REFERENCES public.transfers(""Id"") ON DELETE CASCADE,
                    CONSTRAINT ""FK_transfer_lines_products_ProductId"" FOREIGN KEY (""ProductId"") REFERENCES public.products(""Id"") ON DELETE RESTRICT
                );

                CREATE INDEX IF NOT EXISTS ""IX_transfer_lines_TransferId"" ON public.transfer_lines (""TransferId"");
                CREATE INDEX IF NOT EXISTS ""IX_transfer_lines_ProductId"" ON public.transfer_lines (""ProductId"");
                CREATE INDEX IF NOT EXISTS ""IX_transfer_lines_OrganizationId_ProductId"" ON public.transfer_lines (""OrganizationId"", ""ProductId"");
            ");
        }

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