-- ===================================================
-- CLEANUP SEED DATA SCRIPT
-- Use this to remove all seed data and start fresh
-- ===================================================

-- STEP 1: Identify your company ID
SELECT Id, CompanyName, CompanyCode FROM Companies WHERE IsDeleted = 0

-- STEP 2: Set your company ID here
DECLARE @CompanyId INT = 1  -- CHANGE THIS TO YOUR COMPANY ID

PRINT 'Cleaning up seed data for CompanyId: ' + CAST(@CompanyId AS VARCHAR)

-- STEP 3: Delete all seed data in correct order (respecting foreign keys)
BEGIN TRANSACTION

    -- Delete Job-related data
    PRINT 'Deleting job photos...'
    DELETE FROM JobPhotos WHERE JobId IN (SELECT Id FROM Jobs WHERE CompanyId = @CompanyId)

    PRINT 'Deleting job notes...'
    DELETE FROM JobNotes WHERE JobId IN (SELECT Id FROM Jobs WHERE CompanyId = @CompanyId)

    PRINT 'Deleting job items...'
    DELETE FROM JobItems WHERE JobId IN (SELECT Id FROM Jobs WHERE CompanyId = @CompanyId)

    PRINT 'Deleting jobs...'
    DELETE FROM Jobs WHERE CompanyId = @CompanyId

    -- Delete Quote-related data
    PRINT 'Deleting quote items...'
    DELETE FROM QuoteItems WHERE QuoteId IN (SELECT Id FROM Quotes WHERE CompanyId = @CompanyId)

    PRINT 'Deleting quotes...'
    DELETE FROM Quotes WHERE CompanyId = @CompanyId

    -- Delete Maintenance records
    PRINT 'Deleting maintenance records...'
    DELETE FROM MaintenanceRecords WHERE EquipmentId IN (SELECT Id FROM Equipment WHERE CompanyId = @CompanyId)

    -- Delete Equipment
    PRINT 'Deleting equipment...'
    DELETE FROM Equipment WHERE CompanyId = @CompanyId

    -- Delete Inventory-related data
    PRINT 'Deleting inventory transactions...'
    DELETE FROM InventoryTransactions WHERE InventoryItemId IN (SELECT Id FROM InventoryItems WHERE CompanyId = @CompanyId)

    PRINT 'Deleting inventory items...'
    DELETE FROM InventoryItems WHERE CompanyId = @CompanyId

    -- Delete Catalog
    PRINT 'Deleting catalog items...'
    DELETE FROM CatalogItems WHERE CompanyId = @CompanyId

    -- Delete Customers
    PRINT 'Deleting customers...'
    DELETE FROM Customers WHERE CompanyId = @CompanyId

    -- Delete Pricing Tiers
    PRINT 'Deleting pricing tiers...'
    DELETE FROM PricingTiers WHERE CompanyId = @CompanyId

    -- Delete Operating Costs
    PRINT 'Deleting operating costs...'
    DELETE FROM CompanyOperatingCosts WHERE CompanyId = @CompanyId

    PRINT 'Seed data cleanup complete!'

    -- Show what's left
    SELECT
        'Customers' as TableName, COUNT(*) as RemainingRecords FROM Customers WHERE CompanyId = @CompanyId
    UNION ALL
    SELECT 'InventoryItems', COUNT(*) FROM InventoryItems WHERE CompanyId = @CompanyId
    UNION ALL
    SELECT 'Equipment', COUNT(*) FROM Equipment WHERE CompanyId = @CompanyId
    UNION ALL
    SELECT 'PricingTiers', COUNT(*) FROM PricingTiers WHERE CompanyId = @CompanyId
    UNION ALL
    SELECT 'Quotes', COUNT(*) FROM Quotes WHERE CompanyId = @CompanyId
    UNION ALL
    SELECT 'Jobs', COUNT(*) FROM Jobs WHERE CompanyId = @CompanyId
    UNION ALL
    SELECT 'CatalogItems', COUNT(*) FROM CatalogItems WHERE CompanyId = @CompanyId

COMMIT TRANSACTION

PRINT 'Ready for fresh seed data!'

-- NOTE: This does NOT delete:
-- - The company itself
-- - User accounts
-- - Roles
-- These are preserved so you can log in and re-seed
