-- Quick Database State Check for Lookup Tables
-- =====================================================

PRINT '=== LOOKUP TABLES STATUS ===';
PRINT '';

-- Check if lookup tables exist
IF EXISTS (SELECT * FROM sys.tables WHERE name = 'JobStatusLookups')
    PRINT '✓ JobStatusLookups table exists'
ELSE
    PRINT '✗ JobStatusLookups table MISSING';

IF EXISTS (SELECT * FROM sys.tables WHERE name = 'JobPriorityLookups')
    PRINT '✓ JobPriorityLookups table exists'
ELSE
    PRINT '✗ JobPriorityLookups table MISSING';

IF EXISTS (SELECT * FROM sys.tables WHERE name = 'QuoteStatusLookups')
    PRINT '✓ QuoteStatusLookups table exists'
ELSE
    PRINT '✗ QuoteStatusLookups table MISSING';

PRINT '';
PRINT '=== DATA COUNTS ===';
PRINT '';

-- Count lookup records per company
SELECT
    c.Name AS CompanyName,
    (SELECT COUNT(*) FROM JobStatusLookups WHERE CompanyId = c.Id) AS JobStatuses,
    (SELECT COUNT(*) FROM JobPriorityLookups WHERE CompanyId = c.Id) AS JobPriorities,
    (SELECT COUNT(*) FROM QuoteStatusLookups WHERE CompanyId = c.Id) AS QuoteStatuses,
    (SELECT COUNT(*) FROM Jobs WHERE CompanyId = c.Id) AS Jobs,
    (SELECT COUNT(*) FROM Quotes WHERE CompanyId = c.Id) AS Quotes
FROM Companies c
WHERE c.IsDeleted = 0;

PRINT '';
PRINT '=== JOBS WITH/WITHOUT LOOKUP IDS ===';
PRINT '';

-- Check if Jobs have lookup IDs
SELECT
    'Jobs with NULL JobStatusId' AS Issue,
    COUNT(*) AS Count
FROM Jobs
WHERE JobStatusId IS NULL
UNION ALL
SELECT
    'Jobs with NULL JobPriorityId' AS Issue,
    COUNT(*) AS Count
FROM Jobs
WHERE JobPriorityId IS NULL
UNION ALL
SELECT
    'Quotes with NULL QuoteStatusId' AS Issue,
    COUNT(*) AS Count
FROM Quotes
WHERE QuoteStatusId IS NULL;
