-- ============================================================================
-- NUCLEAR DELETE - Delete ALL data for a company (including fresh seeds)
-- Run this, then immediately seed WITHOUT restarting the app
-- ============================================================================

DECLARE @CompanyId INT = 1;

PRINT 'NUCLEAR DELETE - Deleting EVERYTHING for Company ID: ' + CAST(@CompanyId AS NVARCHAR(10));
PRINT '';

BEGIN TRANSACTION;

BEGIN TRY
    -- Delete in reverse order (most dependent first)
    DELETE FROM JobStatusHistory WHERE JobId IN (SELECT Id FROM Jobs WHERE CompanyId = @CompanyId);
    DELETE FROM JobPhotos WHERE JobId IN (SELECT Id FROM Jobs WHERE CompanyId = @CompanyId);
    DELETE FROM JobNotes WHERE JobId IN (SELECT Id FROM Jobs WHERE CompanyId = @CompanyId);
    DELETE FROM Appointments WHERE CompanyId = @CompanyId;
    DELETE FROM JobItems WHERE JobId IN (SELECT Id FROM Jobs WHERE CompanyId = @CompanyId);
    DELETE FROM QuoteItems WHERE QuoteId IN (SELECT Id FROM Quotes WHERE CompanyId = @CompanyId);
    DELETE FROM InventoryTransactions WHERE InventoryItemId IN (SELECT Id FROM InventoryItems WHERE CompanyId = @CompanyId);
    DELETE FROM MaintenanceRecords WHERE EquipmentId IN (SELECT Id FROM Equipment WHERE CompanyId = @CompanyId);

    DELETE FROM Jobs WHERE CompanyId = @CompanyId;
    DELETE FROM Quotes WHERE CompanyId = @CompanyId;
    DELETE FROM CatalogItems WHERE CompanyId = @CompanyId;
    DELETE FROM CatalogCategories WHERE CompanyId = @CompanyId;

    DELETE FROM Customers WHERE CompanyId = @CompanyId;
    DELETE FROM InventoryItems WHERE CompanyId = @CompanyId;
    DELETE FROM Equipment WHERE CompanyId = @CompanyId;
    DELETE FROM ShopWorkers WHERE CompanyId = @CompanyId;
    DELETE FROM PricingTiers WHERE CompanyId = @CompanyId;
    DELETE FROM CompanyOperatingCosts WHERE CompanyId = @CompanyId;
    DELETE FROM Suppliers WHERE CompanyId = @CompanyId;

    DELETE FROM JobStatusLookup WHERE CompanyId = @CompanyId;
    DELETE FROM JobPriorityLookup WHERE CompanyId = @CompanyId;
    DELETE FROM QuoteStatusLookup WHERE CompanyId = @CompanyId;
    DELETE FROM AppointmentStatusLookup WHERE CompanyId = @CompanyId;
    DELETE FROM AppointmentTypeLookup WHERE CompanyId = @CompanyId;
    DELETE FROM InventoryCategoryLookup WHERE CompanyId = @CompanyId;

    COMMIT TRANSACTION;
    PRINT 'SUCCESS! All data deleted. NOW SEED IMMEDIATELY (do not restart app)!';
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION;
    PRINT 'ERROR: ' + ERROR_MESSAGE();
    THROW;
END CATCH;
