-- =============================================
-- Preview Company Data Reset
-- =============================================
-- This script shows what WOULD be deleted without actually deleting anything.
-- Use this to verify before running the actual ResetCompanyData.sql script.
-- =============================================

-- CONFIGURATION: Set ONE of these (comment out the other)
DECLARE @CompanyCode NVARCHAR(50) = 'DEMO';  -- Use company code
-- DECLARE @CompanyId INT = 1;  -- OR use company ID directly

-- Get CompanyId from CompanyCode if needed
IF @CompanyCode IS NOT NULL AND EXISTS(SELECT 1 FROM Companies WHERE CompanyCode = @CompanyCode)
BEGIN
    SELECT @CompanyId = Id FROM Companies WHERE CompanyCode = @CompanyCode;
END

-- Validate company exists
IF @CompanyId IS NULL OR NOT EXISTS(SELECT 1 FROM Companies WHERE Id = @CompanyId)
BEGIN
    RAISERROR('Company not found! Please check @CompanyId or @CompanyCode', 16, 1);
    RETURN;
END

-- Display company information
DECLARE @CompanyName NVARCHAR(200);
SELECT @CompanyName = CompanyName, @CompanyCode = CompanyCode FROM Companies WHERE Id = @CompanyId;

PRINT '=============================================';
PRINT 'PREVIEW: DATA TO BE DELETED FOR COMPANY:';
PRINT '  Company ID: ' + CAST(@CompanyId AS NVARCHAR(10));
PRINT '  Company Code: ' + @CompanyCode;
PRINT '  Company Name: ' + @CompanyName;
PRINT '=============================================';
PRINT '';
PRINT 'NOTE: This is a PREVIEW only. No data will be deleted.';
PRINT '';

DECLARE @Count INT;
DECLARE @TotalCount INT = 0;

-- Job Photos
SELECT @Count = COUNT(*)
FROM JobPhotos
WHERE JobId IN (SELECT Id FROM Jobs WHERE CompanyId = @CompanyId);
PRINT 'Job Photos: ' + CAST(@Count AS NVARCHAR(10));
SET @TotalCount = @TotalCount + @Count;

-- Job Notes
SELECT @Count = COUNT(*)
FROM JobNotes
WHERE JobId IN (SELECT Id FROM Jobs WHERE CompanyId = @CompanyId);
PRINT 'Job Notes: ' + CAST(@Count AS NVARCHAR(10));
SET @TotalCount = @TotalCount + @Count;

-- Job Items
SELECT @Count = COUNT(*)
FROM JobItems
WHERE JobId IN (SELECT Id FROM Jobs WHERE CompanyId = @CompanyId);
PRINT 'Job Items: ' + CAST(@Count AS NVARCHAR(10));
SET @TotalCount = @TotalCount + @Count;

-- Jobs
SELECT @Count = COUNT(*) FROM Jobs WHERE CompanyId = @CompanyId;
PRINT 'Jobs: ' + CAST(@Count AS NVARCHAR(10));
SET @TotalCount = @TotalCount + @Count;

-- Quote Items
SELECT @Count = COUNT(*)
FROM QuoteItems
WHERE QuoteId IN (SELECT Id FROM Quotes WHERE CompanyId = @CompanyId);
PRINT 'Quote Items: ' + CAST(@Count AS NVARCHAR(10));
SET @TotalCount = @TotalCount + @Count;

-- Quotes
SELECT @Count = COUNT(*) FROM Quotes WHERE CompanyId = @CompanyId;
PRINT 'Quotes: ' + CAST(@Count AS NVARCHAR(10));
SET @TotalCount = @TotalCount + @Count;

-- Appointments
IF OBJECT_ID('Appointments', 'U') IS NOT NULL
BEGIN
    SELECT @Count = COUNT(*) FROM Appointments WHERE CompanyId = @CompanyId;
    PRINT 'Appointments: ' + CAST(@Count AS NVARCHAR(10));
    SET @TotalCount = @TotalCount + @Count;
END

-- Inventory Transactions
SELECT @Count = COUNT(*)
FROM InventoryTransactions
WHERE InventoryItemId IN (SELECT Id FROM InventoryItems WHERE CompanyId = @CompanyId);
PRINT 'Inventory Transactions: ' + CAST(@Count AS NVARCHAR(10));
SET @TotalCount = @TotalCount + @Count;

-- Inventory Items
SELECT @Count = COUNT(*) FROM InventoryItems WHERE CompanyId = @CompanyId;
PRINT 'Inventory Items: ' + CAST(@Count AS NVARCHAR(10));
SET @TotalCount = @TotalCount + @Count;

-- Maintenance Records
SELECT @Count = COUNT(*)
FROM MaintenanceRecords
WHERE EquipmentId IN (SELECT Id FROM Equipment WHERE CompanyId = @CompanyId);
PRINT 'Maintenance Records: ' + CAST(@Count AS NVARCHAR(10));
SET @TotalCount = @TotalCount + @Count;

-- Equipment
SELECT @Count = COUNT(*) FROM Equipment WHERE CompanyId = @CompanyId;
PRINT 'Equipment: ' + CAST(@Count AS NVARCHAR(10));
SET @TotalCount = @TotalCount + @Count;

-- Catalog Items
IF OBJECT_ID('CatalogItems', 'U') IS NOT NULL
BEGIN
    SELECT @Count = COUNT(*) FROM CatalogItems WHERE CompanyId = @CompanyId;
    PRINT 'Catalog Items: ' + CAST(@Count AS NVARCHAR(10));
    SET @TotalCount = @TotalCount + @Count;
END

-- Catalog Categories
IF OBJECT_ID('CatalogCategories', 'U') IS NOT NULL
BEGIN
    SELECT @Count = COUNT(*) FROM CatalogCategories WHERE CompanyId = @CompanyId;
    PRINT 'Catalog Categories: ' + CAST(@Count AS NVARCHAR(10));
    SET @TotalCount = @TotalCount + @Count;
END

-- Suppliers
IF OBJECT_ID('Suppliers', 'U') IS NOT NULL
BEGIN
    SELECT @Count = COUNT(*) FROM Suppliers WHERE CompanyId = @CompanyId;
    PRINT 'Suppliers: ' + CAST(@Count AS NVARCHAR(10));
    SET @TotalCount = @TotalCount + @Count;
END

-- Customers
SELECT @Count = COUNT(*) FROM Customers WHERE CompanyId = @CompanyId;
PRINT 'Customers: ' + CAST(@Count AS NVARCHAR(10));
SET @TotalCount = @TotalCount + @Count;

-- Pricing Tiers
SELECT @Count = COUNT(*) FROM PricingTiers WHERE CompanyId = @CompanyId;
PRINT 'Pricing Tiers: ' + CAST(@Count AS NVARCHAR(10));
SET @TotalCount = @TotalCount + @Count;

-- Company Operating Costs
SELECT @Count = COUNT(*) FROM CompanyOperatingCosts WHERE CompanyId = @CompanyId;
PRINT 'Company Operating Costs: ' + CAST(@Count AS NVARCHAR(10));
SET @TotalCount = @TotalCount + @Count;

-- Company Preferences
IF OBJECT_ID('CompanyPreferences', 'U') IS NOT NULL
BEGIN
    SELECT @Count = COUNT(*) FROM CompanyPreferences WHERE CompanyId = @CompanyId;
    PRINT 'Company Preferences: ' + CAST(@Count AS NVARCHAR(10));
    SET @TotalCount = @TotalCount + @Count;
END

PRINT '';
PRINT '=============================================';
PRINT 'TOTAL RECORDS TO BE DELETED: ' + CAST(@TotalCount AS NVARCHAR(10));
PRINT '=============================================';
PRINT '';
PRINT 'PRESERVED DATA (will NOT be deleted):';
PRINT '  - Company record: ' + @CompanyName;
PRINT '  - Users associated with company';
PRINT '  - Job Status Lookups';
PRINT '  - Job Priority Lookups';
PRINT '  - Quote Status Lookups';
PRINT '  - Inventory Category Lookups';
PRINT '  - Appointment Status Lookups';
PRINT '  - Appointment Type Lookups';
PRINT '';

-- Show sample data for verification
PRINT '=============================================';
PRINT 'SAMPLE DATA (first 5 of each type):';
PRINT '=============================================';
PRINT '';

-- Sample Customers
IF EXISTS(SELECT 1 FROM Customers WHERE CompanyId = @CompanyId)
BEGIN
    PRINT 'CUSTOMERS:';
    SELECT TOP 5
        Id,
        CompanyName,
        ISNULL(ContactFirstName + ' ' + ContactLastName, 'N/A') AS ContactName,
        Email
    FROM Customers
    WHERE CompanyId = @CompanyId
    ORDER BY Id;
    PRINT '';
END

-- Sample Jobs
IF EXISTS(SELECT 1 FROM Jobs WHERE CompanyId = @CompanyId)
BEGIN
    PRINT 'JOBS:';
    SELECT TOP 5
        j.Id,
        j.JobNumber,
        c.CompanyName AS CustomerName,
        j.Status,
        j.CreatedAt
    FROM Jobs j
    LEFT JOIN Customers c ON j.CustomerId = c.Id
    WHERE j.CompanyId = @CompanyId
    ORDER BY j.Id;
    PRINT '';
END

-- Sample Inventory
IF EXISTS(SELECT 1 FROM InventoryItems WHERE CompanyId = @CompanyId)
BEGIN
    PRINT 'INVENTORY ITEMS:';
    SELECT TOP 5
        Id,
        SKU,
        Name,
        QuantityOnHand,
        UnitOfMeasure
    FROM InventoryItems
    WHERE CompanyId = @CompanyId
    ORDER BY Id;
    PRINT '';
END

PRINT '';
PRINT 'To proceed with deletion, run: ResetCompanyData.sql';
