-- Database Diagnostics and Cleanup Script
-- Run this on your testing server to identify and fix seeding issues

-- ===================================================
-- STEP 1: Check for corrupted/malformed SKUs
-- ===================================================
PRINT 'Checking for corrupted inventory items...'
SELECT
    Id,
    SKU,
    Name,
    CompanyId,
    IsDeleted,
    CreatedAt
FROM InventoryItems
WHERE SKU LIKE '(-PWD-%' OR SKU LIKE '%-PWD-%' AND SKU NOT LIKE '[A-Z]%'
ORDER BY CreatedAt DESC

-- ===================================================
-- STEP 2: Check for duplicate SKUs
-- ===================================================
PRINT 'Checking for duplicate SKUs...'
SELECT
    SKU,
    COUNT(*) as DuplicateCount,
    STRING_AGG(CAST(Id AS VARCHAR), ', ') as IDs
FROM InventoryItems
WHERE IsDeleted = 0
GROUP BY SKU
HAVING COUNT(*) > 1

-- ===================================================
-- STEP 3: Check companies and their codes
-- ===================================================
PRINT 'Checking company data...'
SELECT
    Id,
    CompanyName,
    CompanyCode,
    IsActive,
    IsDeleted,
    SubscriptionPlan
FROM Companies
WHERE IsDeleted = 0
ORDER BY Id

-- ===================================================
-- STEP 4: Check inventory count per company
-- ===================================================
PRINT 'Inventory items per company...'
SELECT
    c.Id as CompanyId,
    c.CompanyName,
    c.CompanyCode,
    COUNT(i.Id) as InventoryItemCount
FROM Companies c
LEFT JOIN InventoryItems i ON c.Id = i.CompanyId AND i.IsDeleted = 0
WHERE c.IsDeleted = 0
GROUP BY c.Id, c.CompanyName, c.CompanyCode
ORDER BY c.Id

-- ===================================================
-- CLEANUP OPTIONS (commented out for safety)
-- Uncomment the section you need AFTER reviewing the results above
-- ===================================================

-- OPTION A: Delete ALL corrupted/malformed inventory items
/*
DELETE FROM InventoryItems
WHERE SKU LIKE '(-PWD-%' OR (SKU LIKE '%-PWD-%' AND SKU NOT LIKE '[A-Z]%')
PRINT 'Deleted corrupted inventory items'
*/

-- OPTION B: Delete ALL inventory items for a specific company (to re-seed)
/*
DECLARE @CompanyId INT = 1  -- Change this to your company ID
DELETE FROM InventoryItems WHERE CompanyId = @CompanyId
PRINT 'Deleted all inventory items for company ' + CAST(@CompanyId AS VARCHAR)
*/

-- OPTION C: Delete ALL seed data for a complete fresh start (DANGER!)
/*
-- WARNING: This deletes ALL business data but keeps users and companies
DELETE FROM JobPhotos
DELETE FROM JobNotes
DELETE FROM JobItems
DELETE FROM Jobs
DELETE FROM QuoteItems
DELETE FROM Quotes
DELETE FROM InventoryTransactions
DELETE FROM InventoryItems
DELETE FROM Equipment
DELETE FROM MaintenanceRecords
DELETE FROM CatalogItems
DELETE FROM PricingTiers
DELETE FROM CompanyOperatingCosts
DELETE FROM Customers
PRINT 'All seed data deleted - ready for fresh seeding'
*/

-- ===================================================
-- STEP 5: Verify database is ready for seeding
-- ===================================================
PRINT 'Verification complete. Review results above before running cleanup.'
