-- =============================================================================
-- fix-inventory-categories.sql
-- Links inventory items that have a Category string but no InventoryCategoryId.
-- This affects items imported via CSV before the category fix was applied.
-- Safe to run multiple times (only touches items with InventoryCategoryId IS NULL).
-- Run against: PowderCoatingDb
-- =============================================================================

-- STEP 1: Preview what will be updated (run this first, review before proceeding)
SELECT
    ii.Id,
    ii.SKU,
    ii.Name,
    ii.CompanyId,
    ii.Category        AS CurrentCategory,
    cat.DisplayName    AS ResolvedTo,
    cat.CategoryCode,
    cat.IsCoating
FROM InventoryItems ii
JOIN InventoryCategoryLookups cat
    ON cat.CompanyId  = ii.CompanyId
   AND cat.IsDeleted  = 0
   AND ii.IsDeleted   = 0
   AND ii.InventoryCategoryId IS NULL
   AND (
        ii.Category = cat.DisplayName
        OR ii.Category = cat.CategoryCode
        OR (cat.CategoryCode = 'POWDER'     AND ii.Category IN ('Powder Coatings','Powder Coating','Powders','Powder'))
        OR (cat.CategoryCode = 'PRIMER'     AND ii.Category IN ('Primers','Primer'))
        OR (cat.CategoryCode = 'CLEANER'    AND ii.Category IN ('Cleaners','Cleaner'))
        OR (cat.CategoryCode = 'MASKING'    AND ii.Category IN ('Masking','Masking Tape','Masking Supplies'))
        OR (cat.CategoryCode = 'ABRASIVE'   AND ii.Category IN ('Abrasive','Abrasives','Blast Media','Abrasive Media'))
        OR (cat.CategoryCode = 'CHEMICAL'   AND ii.Category IN ('Chemicals','Chemical'))
        OR (cat.CategoryCode = 'CONSUMABLE' AND ii.Category IN ('Consumable','Consumables'))
        OR (cat.CategoryCode = 'TOOL'       AND ii.Category IN ('Tools','Tool','Tools & Equipment','Equipment'))
        OR (cat.CategoryCode = 'OTHER'      AND ii.Category IN ('General','Other'))
   )
ORDER BY ii.CompanyId, ii.Id


-- STEP 2: Apply the fix (run after reviewing Step 1)
UPDATE ii
SET
    ii.InventoryCategoryId = cat.Id,
    ii.Category            = cat.DisplayName,
    ii.UpdatedAt           = GETDATE()
FROM InventoryItems ii
JOIN InventoryCategoryLookups cat
    ON cat.CompanyId  = ii.CompanyId
   AND cat.IsDeleted  = 0
   AND ii.IsDeleted   = 0
   AND ii.InventoryCategoryId IS NULL
   AND (
        ii.Category = cat.DisplayName
        OR ii.Category = cat.CategoryCode
        OR (cat.CategoryCode = 'POWDER'     AND ii.Category IN ('Powder Coatings','Powder Coating','Powders','Powder'))
        OR (cat.CategoryCode = 'PRIMER'     AND ii.Category IN ('Primers','Primer'))
        OR (cat.CategoryCode = 'CLEANER'    AND ii.Category IN ('Cleaners','Cleaner'))
        OR (cat.CategoryCode = 'MASKING'    AND ii.Category IN ('Masking','Masking Tape','Masking Supplies'))
        OR (cat.CategoryCode = 'ABRASIVE'   AND ii.Category IN ('Abrasive','Abrasives','Blast Media','Abrasive Media'))
        OR (cat.CategoryCode = 'CHEMICAL'   AND ii.Category IN ('Chemicals','Chemical'))
        OR (cat.CategoryCode = 'CONSUMABLE' AND ii.Category IN ('Consumable','Consumables'))
        OR (cat.CategoryCode = 'TOOL'       AND ii.Category IN ('Tools','Tool','Tools & Equipment','Equipment'))
        OR (cat.CategoryCode = 'OTHER'      AND ii.Category IN ('General','Other'))
   )


-- STEP 3: Check for any remaining unmatched items (need manual review in the UI)
SELECT Id, SKU, Name, CompanyId, Category
FROM InventoryItems
WHERE IsDeleted = 0
  AND InventoryCategoryId IS NULL
  AND Category IS NOT NULL
ORDER BY CompanyId, Category
