using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace ERPCore.Infra.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class InitialCreate : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "categories",
                columns: table => new
                {
                    CategoryId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    ParentId = table.Column<int>(type: "integer", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_categories", x => x.CategoryId);
                    table.ForeignKey(
                        name: "FK_categories_categories_ParentId",
                        column: x => x.ParentId,
                        principalTable: "categories",
                        principalColumn: "CategoryId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "journal_entry_stubs",
                columns: table => new
                {
                    JournalId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    SourceDocType = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: false),
                    SourceDocId = table.Column<int>(type: "integer", nullable: false),
                    DebitAccount = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    CreditAccount = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    Amount = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_journal_entry_stubs", x => x.JournalId);
                });

            migrationBuilder.CreateTable(
                name: "number_sequences",
                columns: table => new
                {
                    SequenceId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    doc_type = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: false),
                    year = table.Column<int>(type: "integer", nullable: false),
                    last_number = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_number_sequences", x => x.SequenceId);
                });

            migrationBuilder.CreateTable(
                name: "reason_codes",
                columns: table => new
                {
                    ReasonCodeId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Code = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    Description = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    Context = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_reason_codes", x => x.ReasonCodeId);
                });

            migrationBuilder.CreateTable(
                name: "uoms",
                columns: table => new
                {
                    UomId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_uoms", x => x.UomId);
                });

            migrationBuilder.CreateTable(
                name: "users",
                columns: table => new
                {
                    UserId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Username = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    DisplayName = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    auth_user_id = table.Column<Guid>(type: "uuid", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_users", x => x.UserId);
                });

            migrationBuilder.CreateTable(
                name: "vendors",
                columns: table => new
                {
                    VendorId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Code = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    Terms = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
                    TaxReg = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
                    Currency = table.Column<string>(type: "character varying(3)", maxLength: 3, nullable: false, defaultValue: "LKR"),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, defaultValue: "Active"),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    xmin = table.Column<uint>(type: "xid", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_vendors", x => x.VendorId);
                });

            migrationBuilder.CreateTable(
                name: "warehouses",
                columns: table => new
                {
                    WarehouseId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Code = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_warehouses", x => x.WarehouseId);
                });

            migrationBuilder.CreateTable(
                name: "audit_logs",
                columns: table => new
                {
                    AuditId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    UserId = table.Column<int>(type: "integer", nullable: false),
                    EntityType = table.Column<string>(type: "character varying(80)", maxLength: 80, nullable: false),
                    EntityId = table.Column<int>(type: "integer", nullable: false),
                    Action = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: false),
                    ChangeSet = table.Column<string>(type: "jsonb", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_audit_logs", x => x.AuditId);
                    table.ForeignKey(
                        name: "FK_audit_logs_users_UserId",
                        column: x => x.UserId,
                        principalTable: "users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "requisitions",
                columns: table => new
                {
                    RequisitionId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    DocNo = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: false),
                    RequestedBy = table.Column<int>(type: "integer", nullable: false),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_requisitions", x => x.RequisitionId);
                    table.ForeignKey(
                        name: "FK_requisitions_users_RequestedBy",
                        column: x => x.RequestedBy,
                        principalTable: "users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "items",
                columns: table => new
                {
                    ItemId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Sku = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    Description = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
                    CategoryId = table.Column<int>(type: "integer", nullable: false),
                    BaseUomId = table.Column<int>(type: "integer", nullable: false),
                    DefaultVendorId = table.Column<int>(type: "integer", nullable: true),
                    ItemType = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    TrackingMode = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    TaxClass = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: true),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, defaultValue: "Active"),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    xmin = table.Column<uint>(type: "xid", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_items", x => x.ItemId);
                    table.ForeignKey(
                        name: "FK_items_categories_CategoryId",
                        column: x => x.CategoryId,
                        principalTable: "categories",
                        principalColumn: "CategoryId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_items_uoms_BaseUomId",
                        column: x => x.BaseUomId,
                        principalTable: "uoms",
                        principalColumn: "UomId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_items_vendors_DefaultVendorId",
                        column: x => x.DefaultVendorId,
                        principalTable: "vendors",
                        principalColumn: "VendorId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "bins",
                columns: table => new
                {
                    BinId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    WarehouseId = table.Column<int>(type: "integer", nullable: false),
                    Code = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    BinType = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_bins", x => x.BinId);
                    table.ForeignKey(
                        name: "FK_bins_warehouses_WarehouseId",
                        column: x => x.WarehouseId,
                        principalTable: "warehouses",
                        principalColumn: "WarehouseId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "purchase_returns",
                columns: table => new
                {
                    ReturnId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    DocNo = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: false),
                    VendorId = table.Column<int>(type: "integer", nullable: false),
                    WarehouseId = table.Column<int>(type: "integer", nullable: false),
                    ReasonCodeId = table.Column<int>(type: "integer", nullable: false),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    CreatedBy = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_purchase_returns", x => x.ReturnId);
                    table.ForeignKey(
                        name: "FK_purchase_returns_reason_codes_ReasonCodeId",
                        column: x => x.ReasonCodeId,
                        principalTable: "reason_codes",
                        principalColumn: "ReasonCodeId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_purchase_returns_users_CreatedBy",
                        column: x => x.CreatedBy,
                        principalTable: "users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_purchase_returns_vendors_VendorId",
                        column: x => x.VendorId,
                        principalTable: "vendors",
                        principalColumn: "VendorId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_purchase_returns_warehouses_WarehouseId",
                        column: x => x.WarehouseId,
                        principalTable: "warehouses",
                        principalColumn: "WarehouseId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "stock_adjustments",
                columns: table => new
                {
                    AdjustmentId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    DocNo = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: false),
                    WarehouseId = table.Column<int>(type: "integer", nullable: false),
                    ReasonCodeId = table.Column<int>(type: "integer", nullable: false),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    CreatedBy = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    xmin = table.Column<uint>(type: "xid", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_stock_adjustments", x => x.AdjustmentId);
                    table.ForeignKey(
                        name: "FK_stock_adjustments_reason_codes_ReasonCodeId",
                        column: x => x.ReasonCodeId,
                        principalTable: "reason_codes",
                        principalColumn: "ReasonCodeId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_adjustments_users_CreatedBy",
                        column: x => x.CreatedBy,
                        principalTable: "users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_adjustments_warehouses_WarehouseId",
                        column: x => x.WarehouseId,
                        principalTable: "warehouses",
                        principalColumn: "WarehouseId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "stock_counts",
                columns: table => new
                {
                    CountId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    DocNo = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: false),
                    WarehouseId = table.Column<int>(type: "integer", nullable: false),
                    CountType = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    CreatedBy = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    xmin = table.Column<uint>(type: "xid", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_stock_counts", x => x.CountId);
                    table.ForeignKey(
                        name: "FK_stock_counts_users_CreatedBy",
                        column: x => x.CreatedBy,
                        principalTable: "users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_counts_warehouses_WarehouseId",
                        column: x => x.WarehouseId,
                        principalTable: "warehouses",
                        principalColumn: "WarehouseId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "stock_transfers",
                columns: table => new
                {
                    TransferId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    DocNo = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: false),
                    SrcWarehouseId = table.Column<int>(type: "integer", nullable: false),
                    DestWarehouseId = table.Column<int>(type: "integer", nullable: false),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    CreatedBy = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    xmin = table.Column<uint>(type: "xid", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_stock_transfers", x => x.TransferId);
                    table.ForeignKey(
                        name: "FK_stock_transfers_users_CreatedBy",
                        column: x => x.CreatedBy,
                        principalTable: "users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_transfers_warehouses_DestWarehouseId",
                        column: x => x.DestWarehouseId,
                        principalTable: "warehouses",
                        principalColumn: "WarehouseId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_transfers_warehouses_SrcWarehouseId",
                        column: x => x.SrcWarehouseId,
                        principalTable: "warehouses",
                        principalColumn: "WarehouseId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "purchase_orders",
                columns: table => new
                {
                    PoId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    DocNo = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: false),
                    VendorId = table.Column<int>(type: "integer", nullable: false),
                    RequisitionId = table.Column<int>(type: "integer", nullable: true),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    ApprovalRequired = table.Column<bool>(type: "boolean", nullable: false),
                    CreatedBy = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    xmin = table.Column<uint>(type: "xid", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_purchase_orders", x => x.PoId);
                    table.ForeignKey(
                        name: "FK_purchase_orders_requisitions_RequisitionId",
                        column: x => x.RequisitionId,
                        principalTable: "requisitions",
                        principalColumn: "RequisitionId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_purchase_orders_users_CreatedBy",
                        column: x => x.CreatedBy,
                        principalTable: "users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_purchase_orders_vendors_VendorId",
                        column: x => x.VendorId,
                        principalTable: "vendors",
                        principalColumn: "VendorId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "rfqs",
                columns: table => new
                {
                    RfqId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    DocNo = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: false),
                    RequisitionId = table.Column<int>(type: "integer", nullable: false),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_rfqs", x => x.RfqId);
                    table.ForeignKey(
                        name: "FK_rfqs_requisitions_RequisitionId",
                        column: x => x.RequisitionId,
                        principalTable: "requisitions",
                        principalColumn: "RequisitionId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "batches",
                columns: table => new
                {
                    BatchId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    BatchNo = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    ExpiryDate = table.Column<DateOnly>(type: "date", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_batches", x => x.BatchId);
                    table.ForeignKey(
                        name: "FK_batches_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "item_reorders",
                columns: table => new
                {
                    ReorderId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    WarehouseId = table.Column<int>(type: "integer", nullable: false),
                    ReorderPoint = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    ReorderQty = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_item_reorders", x => x.ReorderId);
                    table.ForeignKey(
                        name: "FK_item_reorders_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_item_reorders_warehouses_WarehouseId",
                        column: x => x.WarehouseId,
                        principalTable: "warehouses",
                        principalColumn: "WarehouseId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "requisition_lines",
                columns: table => new
                {
                    ReqLineId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    RequisitionId = table.Column<int>(type: "integer", nullable: false),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    Qty = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    RequiredBy = table.Column<DateOnly>(type: "date", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_requisition_lines", x => x.ReqLineId);
                    table.ForeignKey(
                        name: "FK_requisition_lines_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_requisition_lines_requisitions_RequisitionId",
                        column: x => x.RequisitionId,
                        principalTable: "requisitions",
                        principalColumn: "RequisitionId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "serials",
                columns: table => new
                {
                    SerialId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    SerialNo = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_serials", x => x.SerialId);
                    table.ForeignKey(
                        name: "FK_serials_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "uom_conversions",
                columns: table => new
                {
                    ConversionId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    FromUomId = table.Column<int>(type: "integer", nullable: false),
                    ToUomId = table.Column<int>(type: "integer", nullable: false),
                    Factor = table.Column<decimal>(type: "numeric(18,6)", precision: 18, scale: 6, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_uom_conversions", x => x.ConversionId);
                    table.ForeignKey(
                        name: "FK_uom_conversions_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_uom_conversions_uoms_FromUomId",
                        column: x => x.FromUomId,
                        principalTable: "uoms",
                        principalColumn: "UomId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_uom_conversions_uoms_ToUomId",
                        column: x => x.ToUomId,
                        principalTable: "uoms",
                        principalColumn: "UomId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "stock_count_lines",
                columns: table => new
                {
                    CountLineId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    CountId = table.Column<int>(type: "integer", nullable: false),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    BinId = table.Column<int>(type: "integer", nullable: true),
                    SystemQty = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    CountedQty = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: true),
                    Variance = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_stock_count_lines", x => x.CountLineId);
                    table.ForeignKey(
                        name: "FK_stock_count_lines_bins_BinId",
                        column: x => x.BinId,
                        principalTable: "bins",
                        principalColumn: "BinId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_count_lines_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_count_lines_stock_counts_CountId",
                        column: x => x.CountId,
                        principalTable: "stock_counts",
                        principalColumn: "CountId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "grns",
                columns: table => new
                {
                    GrnId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    DocNo = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: false),
                    PoId = table.Column<int>(type: "integer", nullable: true),
                    VendorId = table.Column<int>(type: "integer", nullable: false),
                    WarehouseId = table.Column<int>(type: "integer", nullable: false),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    CreatedBy = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    PostedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    xmin = table.Column<uint>(type: "xid", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_grns", x => x.GrnId);
                    table.ForeignKey(
                        name: "FK_grns_purchase_orders_PoId",
                        column: x => x.PoId,
                        principalTable: "purchase_orders",
                        principalColumn: "PoId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_grns_users_CreatedBy",
                        column: x => x.CreatedBy,
                        principalTable: "users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_grns_vendors_VendorId",
                        column: x => x.VendorId,
                        principalTable: "vendors",
                        principalColumn: "VendorId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_grns_warehouses_WarehouseId",
                        column: x => x.WarehouseId,
                        principalTable: "warehouses",
                        principalColumn: "WarehouseId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "po_lines",
                columns: table => new
                {
                    PoLineId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    PoId = table.Column<int>(type: "integer", nullable: false),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    UomId = table.Column<int>(type: "integer", nullable: false),
                    WarehouseId = table.Column<int>(type: "integer", nullable: false),
                    Qty = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    UnitPrice = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    Tax = table.Column<decimal>(type: "numeric(9,4)", precision: 9, scale: 4, nullable: false),
                    QtyReceived = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_po_lines", x => x.PoLineId);
                    table.ForeignKey(
                        name: "FK_po_lines_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_po_lines_purchase_orders_PoId",
                        column: x => x.PoId,
                        principalTable: "purchase_orders",
                        principalColumn: "PoId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_po_lines_uoms_UomId",
                        column: x => x.UomId,
                        principalTable: "uoms",
                        principalColumn: "UomId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_po_lines_warehouses_WarehouseId",
                        column: x => x.WarehouseId,
                        principalTable: "warehouses",
                        principalColumn: "WarehouseId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "rfq_lines",
                columns: table => new
                {
                    RfqLineId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    RfqId = table.Column<int>(type: "integer", nullable: false),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    Qty = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_rfq_lines", x => x.RfqLineId);
                    table.ForeignKey(
                        name: "FK_rfq_lines_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_rfq_lines_rfqs_RfqId",
                        column: x => x.RfqId,
                        principalTable: "rfqs",
                        principalColumn: "RfqId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "vendor_quotations",
                columns: table => new
                {
                    QuotationId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    RfqId = table.Column<int>(type: "integer", nullable: false),
                    VendorId = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_vendor_quotations", x => x.QuotationId);
                    table.ForeignKey(
                        name: "FK_vendor_quotations_rfqs_RfqId",
                        column: x => x.RfqId,
                        principalTable: "rfqs",
                        principalColumn: "RfqId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_vendor_quotations_vendors_VendorId",
                        column: x => x.VendorId,
                        principalTable: "vendors",
                        principalColumn: "VendorId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "stock_adjustment_lines",
                columns: table => new
                {
                    AdjLineId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    AdjustmentId = table.Column<int>(type: "integer", nullable: false),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    BinId = table.Column<int>(type: "integer", nullable: true),
                    BatchId = table.Column<int>(type: "integer", nullable: true),
                    SerialId = table.Column<int>(type: "integer", nullable: true),
                    QtyDelta = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_stock_adjustment_lines", x => x.AdjLineId);
                    table.ForeignKey(
                        name: "FK_stock_adjustment_lines_batches_BatchId",
                        column: x => x.BatchId,
                        principalTable: "batches",
                        principalColumn: "BatchId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_adjustment_lines_bins_BinId",
                        column: x => x.BinId,
                        principalTable: "bins",
                        principalColumn: "BinId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_adjustment_lines_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_adjustment_lines_serials_SerialId",
                        column: x => x.SerialId,
                        principalTable: "serials",
                        principalColumn: "SerialId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_adjustment_lines_stock_adjustments_AdjustmentId",
                        column: x => x.AdjustmentId,
                        principalTable: "stock_adjustments",
                        principalColumn: "AdjustmentId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "stock_ledger",
                columns: table => new
                {
                    LedgerId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    WarehouseId = table.Column<int>(type: "integer", nullable: false),
                    BinId = table.Column<int>(type: "integer", nullable: true),
                    BatchId = table.Column<int>(type: "integer", nullable: true),
                    SerialId = table.Column<int>(type: "integer", nullable: true),
                    UserId = table.Column<int>(type: "integer", nullable: false),
                    Direction = table.Column<string>(type: "character varying(5)", maxLength: 5, nullable: false),
                    QtyBase = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    UnitCost = table.Column<decimal>(type: "numeric(18,6)", precision: 18, scale: 6, nullable: false),
                    Value = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    RunningBalance = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    SourceDocType = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: false),
                    SourceDocId = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_stock_ledger", x => x.LedgerId);
                    table.ForeignKey(
                        name: "FK_stock_ledger_batches_BatchId",
                        column: x => x.BatchId,
                        principalTable: "batches",
                        principalColumn: "BatchId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_ledger_bins_BinId",
                        column: x => x.BinId,
                        principalTable: "bins",
                        principalColumn: "BinId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_ledger_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_ledger_serials_SerialId",
                        column: x => x.SerialId,
                        principalTable: "serials",
                        principalColumn: "SerialId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_ledger_users_UserId",
                        column: x => x.UserId,
                        principalTable: "users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_ledger_warehouses_WarehouseId",
                        column: x => x.WarehouseId,
                        principalTable: "warehouses",
                        principalColumn: "WarehouseId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "stock_transfer_lines",
                columns: table => new
                {
                    TransferLineId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    TransferId = table.Column<int>(type: "integer", nullable: false),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    SrcBinId = table.Column<int>(type: "integer", nullable: true),
                    DestBinId = table.Column<int>(type: "integer", nullable: true),
                    BatchId = table.Column<int>(type: "integer", nullable: true),
                    SerialId = table.Column<int>(type: "integer", nullable: true),
                    Qty = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    UnitCost = table.Column<decimal>(type: "numeric(18,6)", precision: 18, scale: 6, nullable: true),
                    QtyReceived = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_stock_transfer_lines", x => x.TransferLineId);
                    table.ForeignKey(
                        name: "FK_stock_transfer_lines_batches_BatchId",
                        column: x => x.BatchId,
                        principalTable: "batches",
                        principalColumn: "BatchId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_transfer_lines_bins_DestBinId",
                        column: x => x.DestBinId,
                        principalTable: "bins",
                        principalColumn: "BinId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_transfer_lines_bins_SrcBinId",
                        column: x => x.SrcBinId,
                        principalTable: "bins",
                        principalColumn: "BinId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_transfer_lines_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_transfer_lines_serials_SerialId",
                        column: x => x.SerialId,
                        principalTable: "serials",
                        principalColumn: "SerialId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_transfer_lines_stock_transfers_TransferId",
                        column: x => x.TransferId,
                        principalTable: "stock_transfers",
                        principalColumn: "TransferId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "grn_lines",
                columns: table => new
                {
                    GrnLineId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    GrnId = table.Column<int>(type: "integer", nullable: false),
                    PoLineId = table.Column<int>(type: "integer", nullable: true),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    UomId = table.Column<int>(type: "integer", nullable: false),
                    BinId = table.Column<int>(type: "integer", nullable: true),
                    BatchId = table.Column<int>(type: "integer", nullable: true),
                    Qty = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    UnitCost = table.Column<decimal>(type: "numeric(18,6)", precision: 18, scale: 6, nullable: false),
                    ReceivedValue = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    HoldStatus = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_grn_lines", x => x.GrnLineId);
                    table.ForeignKey(
                        name: "FK_grn_lines_batches_BatchId",
                        column: x => x.BatchId,
                        principalTable: "batches",
                        principalColumn: "BatchId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_grn_lines_bins_BinId",
                        column: x => x.BinId,
                        principalTable: "bins",
                        principalColumn: "BinId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_grn_lines_grns_GrnId",
                        column: x => x.GrnId,
                        principalTable: "grns",
                        principalColumn: "GrnId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_grn_lines_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_grn_lines_po_lines_PoLineId",
                        column: x => x.PoLineId,
                        principalTable: "po_lines",
                        principalColumn: "PoLineId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_grn_lines_uoms_UomId",
                        column: x => x.UomId,
                        principalTable: "uoms",
                        principalColumn: "UomId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "vendor_quotation_lines",
                columns: table => new
                {
                    QuotationLineId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    QuotationId = table.Column<int>(type: "integer", nullable: false),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    UnitPrice = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    LeadDays = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_vendor_quotation_lines", x => x.QuotationLineId);
                    table.ForeignKey(
                        name: "FK_vendor_quotation_lines_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_vendor_quotation_lines_vendor_quotations_QuotationId",
                        column: x => x.QuotationId,
                        principalTable: "vendor_quotations",
                        principalColumn: "QuotationId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "purchase_return_lines",
                columns: table => new
                {
                    ReturnLineId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    ReturnId = table.Column<int>(type: "integer", nullable: false),
                    GrnLineId = table.Column<int>(type: "integer", nullable: true),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    Qty = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_purchase_return_lines", x => x.ReturnLineId);
                    table.ForeignKey(
                        name: "FK_purchase_return_lines_grn_lines_GrnLineId",
                        column: x => x.GrnLineId,
                        principalTable: "grn_lines",
                        principalColumn: "GrnLineId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_purchase_return_lines_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_purchase_return_lines_purchase_returns_ReturnId",
                        column: x => x.ReturnId,
                        principalTable: "purchase_returns",
                        principalColumn: "ReturnId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "stock_layers",
                columns: table => new
                {
                    LayerId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    WarehouseId = table.Column<int>(type: "integer", nullable: false),
                    BatchId = table.Column<int>(type: "integer", nullable: true),
                    SerialId = table.Column<int>(type: "integer", nullable: true),
                    GrnLineId = table.Column<int>(type: "integer", nullable: true),
                    QtyReceived = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    QtyRemaining = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    UnitCost = table.Column<decimal>(type: "numeric(18,6)", precision: 18, scale: 6, nullable: false),
                    ReceiptDate = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_stock_layers", x => x.LayerId);
                    table.ForeignKey(
                        name: "FK_stock_layers_batches_BatchId",
                        column: x => x.BatchId,
                        principalTable: "batches",
                        principalColumn: "BatchId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_layers_grn_lines_GrnLineId",
                        column: x => x.GrnLineId,
                        principalTable: "grn_lines",
                        principalColumn: "GrnLineId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_layers_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_layers_serials_SerialId",
                        column: x => x.SerialId,
                        principalTable: "serials",
                        principalColumn: "SerialId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_stock_layers_warehouses_WarehouseId",
                        column: x => x.WarehouseId,
                        principalTable: "warehouses",
                        principalColumn: "WarehouseId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.InsertData(
                table: "users",
                columns: new[] { "UserId", "auth_user_id", "DisplayName", "Status", "Username" },
                values: new object[] { 1, null, "System", "Active", "system" });

            migrationBuilder.CreateIndex(
                name: "IX_audit_logs_CreatedAt",
                table: "audit_logs",
                column: "CreatedAt");

            migrationBuilder.CreateIndex(
                name: "IX_audit_logs_EntityType_EntityId",
                table: "audit_logs",
                columns: new[] { "EntityType", "EntityId" });

            migrationBuilder.CreateIndex(
                name: "IX_audit_logs_UserId",
                table: "audit_logs",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_batches_ItemId_BatchNo",
                table: "batches",
                columns: new[] { "ItemId", "BatchNo" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_bins_WarehouseId_Code",
                table: "bins",
                columns: new[] { "WarehouseId", "Code" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_categories_ParentId",
                table: "categories",
                column: "ParentId");

            migrationBuilder.CreateIndex(
                name: "IX_grn_lines_BatchId",
                table: "grn_lines",
                column: "BatchId");

            migrationBuilder.CreateIndex(
                name: "IX_grn_lines_BinId",
                table: "grn_lines",
                column: "BinId");

            migrationBuilder.CreateIndex(
                name: "IX_grn_lines_GrnId",
                table: "grn_lines",
                column: "GrnId");

            migrationBuilder.CreateIndex(
                name: "IX_grn_lines_ItemId",
                table: "grn_lines",
                column: "ItemId");

            migrationBuilder.CreateIndex(
                name: "IX_grn_lines_PoLineId",
                table: "grn_lines",
                column: "PoLineId");

            migrationBuilder.CreateIndex(
                name: "IX_grn_lines_UomId",
                table: "grn_lines",
                column: "UomId");

            migrationBuilder.CreateIndex(
                name: "IX_grns_CreatedBy",
                table: "grns",
                column: "CreatedBy");

            migrationBuilder.CreateIndex(
                name: "IX_grns_DocNo",
                table: "grns",
                column: "DocNo",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_grns_PoId",
                table: "grns",
                column: "PoId");

            migrationBuilder.CreateIndex(
                name: "IX_grns_Status",
                table: "grns",
                column: "Status");

            migrationBuilder.CreateIndex(
                name: "IX_grns_VendorId",
                table: "grns",
                column: "VendorId");

            migrationBuilder.CreateIndex(
                name: "IX_grns_WarehouseId",
                table: "grns",
                column: "WarehouseId");

            migrationBuilder.CreateIndex(
                name: "IX_item_reorders_ItemId_WarehouseId",
                table: "item_reorders",
                columns: new[] { "ItemId", "WarehouseId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_item_reorders_WarehouseId",
                table: "item_reorders",
                column: "WarehouseId");

            migrationBuilder.CreateIndex(
                name: "IX_items_BaseUomId",
                table: "items",
                column: "BaseUomId");

            migrationBuilder.CreateIndex(
                name: "IX_items_CategoryId",
                table: "items",
                column: "CategoryId");

            migrationBuilder.CreateIndex(
                name: "IX_items_DefaultVendorId",
                table: "items",
                column: "DefaultVendorId");

            migrationBuilder.CreateIndex(
                name: "IX_items_Sku",
                table: "items",
                column: "Sku",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_items_Status",
                table: "items",
                column: "Status");

            migrationBuilder.CreateIndex(
                name: "IX_journal_entry_stubs_SourceDocType_SourceDocId",
                table: "journal_entry_stubs",
                columns: new[] { "SourceDocType", "SourceDocId" });

            migrationBuilder.CreateIndex(
                name: "IX_number_sequences_doc_type_year",
                table: "number_sequences",
                columns: new[] { "doc_type", "year" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_po_lines_ItemId",
                table: "po_lines",
                column: "ItemId");

            migrationBuilder.CreateIndex(
                name: "IX_po_lines_PoId",
                table: "po_lines",
                column: "PoId");

            migrationBuilder.CreateIndex(
                name: "IX_po_lines_UomId",
                table: "po_lines",
                column: "UomId");

            migrationBuilder.CreateIndex(
                name: "IX_po_lines_WarehouseId",
                table: "po_lines",
                column: "WarehouseId");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_orders_CreatedBy",
                table: "purchase_orders",
                column: "CreatedBy");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_orders_DocNo",
                table: "purchase_orders",
                column: "DocNo",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_purchase_orders_RequisitionId",
                table: "purchase_orders",
                column: "RequisitionId");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_orders_Status",
                table: "purchase_orders",
                column: "Status");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_orders_VendorId",
                table: "purchase_orders",
                column: "VendorId");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_return_lines_GrnLineId",
                table: "purchase_return_lines",
                column: "GrnLineId");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_return_lines_ItemId",
                table: "purchase_return_lines",
                column: "ItemId");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_return_lines_ReturnId",
                table: "purchase_return_lines",
                column: "ReturnId");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_returns_CreatedBy",
                table: "purchase_returns",
                column: "CreatedBy");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_returns_DocNo",
                table: "purchase_returns",
                column: "DocNo",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_purchase_returns_ReasonCodeId",
                table: "purchase_returns",
                column: "ReasonCodeId");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_returns_VendorId",
                table: "purchase_returns",
                column: "VendorId");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_returns_WarehouseId",
                table: "purchase_returns",
                column: "WarehouseId");

            migrationBuilder.CreateIndex(
                name: "IX_reason_codes_Context_Code",
                table: "reason_codes",
                columns: new[] { "Context", "Code" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_requisition_lines_ItemId",
                table: "requisition_lines",
                column: "ItemId");

            migrationBuilder.CreateIndex(
                name: "IX_requisition_lines_RequisitionId",
                table: "requisition_lines",
                column: "RequisitionId");

            migrationBuilder.CreateIndex(
                name: "IX_requisitions_DocNo",
                table: "requisitions",
                column: "DocNo",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_requisitions_RequestedBy",
                table: "requisitions",
                column: "RequestedBy");

            migrationBuilder.CreateIndex(
                name: "IX_requisitions_Status",
                table: "requisitions",
                column: "Status");

            migrationBuilder.CreateIndex(
                name: "IX_rfq_lines_ItemId",
                table: "rfq_lines",
                column: "ItemId");

            migrationBuilder.CreateIndex(
                name: "IX_rfq_lines_RfqId",
                table: "rfq_lines",
                column: "RfqId");

            migrationBuilder.CreateIndex(
                name: "IX_rfqs_DocNo",
                table: "rfqs",
                column: "DocNo",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_rfqs_RequisitionId",
                table: "rfqs",
                column: "RequisitionId");

            migrationBuilder.CreateIndex(
                name: "IX_serials_ItemId_SerialNo",
                table: "serials",
                columns: new[] { "ItemId", "SerialNo" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_stock_adjustment_lines_AdjustmentId",
                table: "stock_adjustment_lines",
                column: "AdjustmentId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_adjustment_lines_BatchId",
                table: "stock_adjustment_lines",
                column: "BatchId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_adjustment_lines_BinId",
                table: "stock_adjustment_lines",
                column: "BinId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_adjustment_lines_ItemId",
                table: "stock_adjustment_lines",
                column: "ItemId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_adjustment_lines_SerialId",
                table: "stock_adjustment_lines",
                column: "SerialId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_adjustments_CreatedBy",
                table: "stock_adjustments",
                column: "CreatedBy");

            migrationBuilder.CreateIndex(
                name: "IX_stock_adjustments_DocNo",
                table: "stock_adjustments",
                column: "DocNo",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_stock_adjustments_ReasonCodeId",
                table: "stock_adjustments",
                column: "ReasonCodeId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_adjustments_WarehouseId",
                table: "stock_adjustments",
                column: "WarehouseId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_count_lines_BinId",
                table: "stock_count_lines",
                column: "BinId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_count_lines_CountId",
                table: "stock_count_lines",
                column: "CountId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_count_lines_ItemId",
                table: "stock_count_lines",
                column: "ItemId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_counts_CreatedBy",
                table: "stock_counts",
                column: "CreatedBy");

            migrationBuilder.CreateIndex(
                name: "IX_stock_counts_DocNo",
                table: "stock_counts",
                column: "DocNo",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_stock_counts_Status",
                table: "stock_counts",
                column: "Status");

            migrationBuilder.CreateIndex(
                name: "IX_stock_counts_WarehouseId",
                table: "stock_counts",
                column: "WarehouseId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_layers_BatchId",
                table: "stock_layers",
                column: "BatchId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_layers_GrnLineId",
                table: "stock_layers",
                column: "GrnLineId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_layers_ItemId_WarehouseId_ReceiptDate_LayerId",
                table: "stock_layers",
                columns: new[] { "ItemId", "WarehouseId", "ReceiptDate", "LayerId" });

            migrationBuilder.CreateIndex(
                name: "IX_stock_layers_SerialId",
                table: "stock_layers",
                column: "SerialId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_layers_WarehouseId",
                table: "stock_layers",
                column: "WarehouseId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_ledger_BatchId",
                table: "stock_ledger",
                column: "BatchId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_ledger_BinId",
                table: "stock_ledger",
                column: "BinId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_ledger_ItemId_WarehouseId_CreatedAt",
                table: "stock_ledger",
                columns: new[] { "ItemId", "WarehouseId", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_stock_ledger_ItemId_WarehouseId_LedgerId",
                table: "stock_ledger",
                columns: new[] { "ItemId", "WarehouseId", "LedgerId" });

            migrationBuilder.CreateIndex(
                name: "IX_stock_ledger_SerialId",
                table: "stock_ledger",
                column: "SerialId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_ledger_SourceDocType_SourceDocId",
                table: "stock_ledger",
                columns: new[] { "SourceDocType", "SourceDocId" });

            migrationBuilder.CreateIndex(
                name: "IX_stock_ledger_UserId",
                table: "stock_ledger",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_ledger_WarehouseId",
                table: "stock_ledger",
                column: "WarehouseId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_transfer_lines_BatchId",
                table: "stock_transfer_lines",
                column: "BatchId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_transfer_lines_DestBinId",
                table: "stock_transfer_lines",
                column: "DestBinId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_transfer_lines_ItemId",
                table: "stock_transfer_lines",
                column: "ItemId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_transfer_lines_SerialId",
                table: "stock_transfer_lines",
                column: "SerialId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_transfer_lines_SrcBinId",
                table: "stock_transfer_lines",
                column: "SrcBinId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_transfer_lines_TransferId",
                table: "stock_transfer_lines",
                column: "TransferId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_transfers_CreatedBy",
                table: "stock_transfers",
                column: "CreatedBy");

            migrationBuilder.CreateIndex(
                name: "IX_stock_transfers_DestWarehouseId",
                table: "stock_transfers",
                column: "DestWarehouseId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_transfers_DocNo",
                table: "stock_transfers",
                column: "DocNo",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_stock_transfers_SrcWarehouseId",
                table: "stock_transfers",
                column: "SrcWarehouseId");

            migrationBuilder.CreateIndex(
                name: "IX_stock_transfers_Status",
                table: "stock_transfers",
                column: "Status");

            migrationBuilder.CreateIndex(
                name: "IX_uom_conversions_FromUomId",
                table: "uom_conversions",
                column: "FromUomId");

            migrationBuilder.CreateIndex(
                name: "IX_uom_conversions_ItemId_FromUomId_ToUomId",
                table: "uom_conversions",
                columns: new[] { "ItemId", "FromUomId", "ToUomId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_uom_conversions_ToUomId",
                table: "uom_conversions",
                column: "ToUomId");

            migrationBuilder.CreateIndex(
                name: "IX_uoms_Name",
                table: "uoms",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_users_auth_user_id",
                table: "users",
                column: "auth_user_id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_users_Username",
                table: "users",
                column: "Username",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_vendor_quotation_lines_ItemId",
                table: "vendor_quotation_lines",
                column: "ItemId");

            migrationBuilder.CreateIndex(
                name: "IX_vendor_quotation_lines_QuotationId",
                table: "vendor_quotation_lines",
                column: "QuotationId");

            migrationBuilder.CreateIndex(
                name: "IX_vendor_quotations_RfqId_VendorId",
                table: "vendor_quotations",
                columns: new[] { "RfqId", "VendorId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_vendor_quotations_VendorId",
                table: "vendor_quotations",
                column: "VendorId");

            migrationBuilder.CreateIndex(
                name: "IX_vendors_Code",
                table: "vendors",
                column: "Code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_vendors_Status",
                table: "vendors",
                column: "Status");

            migrationBuilder.CreateIndex(
                name: "IX_warehouses_Code",
                table: "warehouses",
                column: "Code",
                unique: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "audit_logs");

            migrationBuilder.DropTable(
                name: "item_reorders");

            migrationBuilder.DropTable(
                name: "journal_entry_stubs");

            migrationBuilder.DropTable(
                name: "number_sequences");

            migrationBuilder.DropTable(
                name: "purchase_return_lines");

            migrationBuilder.DropTable(
                name: "requisition_lines");

            migrationBuilder.DropTable(
                name: "rfq_lines");

            migrationBuilder.DropTable(
                name: "stock_adjustment_lines");

            migrationBuilder.DropTable(
                name: "stock_count_lines");

            migrationBuilder.DropTable(
                name: "stock_layers");

            migrationBuilder.DropTable(
                name: "stock_ledger");

            migrationBuilder.DropTable(
                name: "stock_transfer_lines");

            migrationBuilder.DropTable(
                name: "uom_conversions");

            migrationBuilder.DropTable(
                name: "vendor_quotation_lines");

            migrationBuilder.DropTable(
                name: "purchase_returns");

            migrationBuilder.DropTable(
                name: "stock_adjustments");

            migrationBuilder.DropTable(
                name: "stock_counts");

            migrationBuilder.DropTable(
                name: "grn_lines");

            migrationBuilder.DropTable(
                name: "serials");

            migrationBuilder.DropTable(
                name: "stock_transfers");

            migrationBuilder.DropTable(
                name: "vendor_quotations");

            migrationBuilder.DropTable(
                name: "reason_codes");

            migrationBuilder.DropTable(
                name: "batches");

            migrationBuilder.DropTable(
                name: "bins");

            migrationBuilder.DropTable(
                name: "grns");

            migrationBuilder.DropTable(
                name: "po_lines");

            migrationBuilder.DropTable(
                name: "rfqs");

            migrationBuilder.DropTable(
                name: "items");

            migrationBuilder.DropTable(
                name: "purchase_orders");

            migrationBuilder.DropTable(
                name: "warehouses");

            migrationBuilder.DropTable(
                name: "categories");

            migrationBuilder.DropTable(
                name: "uoms");

            migrationBuilder.DropTable(
                name: "requisitions");

            migrationBuilder.DropTable(
                name: "vendors");

            migrationBuilder.DropTable(
                name: "users");
        }
    }
}
