-- Pre-migration script to prepare database for multi-tenancy
-- Run this BEFORE applying the AddMultiTenancy migration

USE PowderCoatingDb;
GO

-- Step 1: Create Companies table manually (without constraints initially)
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Companies')
BEGIN
    CREATE TABLE Companies (
        Id INT IDENTITY(1,1) PRIMARY KEY,
        CompanyName NVARCHAR(MAX) NOT NULL,
        CompanyCode NVARCHAR(450) NULL,
        PrimaryContactName NVARCHAR(MAX) NOT NULL,
        PrimaryContactEmail NVARCHAR(MAX) NOT NULL,
        Phone NVARCHAR(MAX) NULL,
        Address NVARCHAR(MAX) NULL,
        City NVARCHAR(MAX) NULL,
        State NVARCHAR(MAX) NULL,
        ZipCode NVARCHAR(MAX) NULL,
        IsActive BIT NOT NULL DEFAULT 1,
        SubscriptionStartDate DATETIME2 NOT NULL,
        SubscriptionEndDate DATETIME2 NULL,
        SubscriptionPlan NVARCHAR(MAX) NULL,
        TimeZone NVARCHAR(MAX) NULL,
        LogoPath NVARCHAR(MAX) NULL,
        Settings NVARCHAR(MAX) NULL,
        CompanyId INT NOT NULL DEFAULT 0,
        CreatedAt DATETIME2 NOT NULL,
        UpdatedAt DATETIME2 NULL,
        CreatedBy NVARCHAR(MAX) NULL,
        UpdatedBy NVARCHAR(MAX) NULL,
        IsDeleted BIT NOT NULL DEFAULT 0,
        DeletedAt DATETIME2 NULL,
        DeletedBy NVARCHAR(MAX) NULL
    );

    -- Insert default company
    INSERT INTO Companies (
        CompanyName, CompanyCode, PrimaryContactName, PrimaryContactEmail,
        Phone, Address, City, State, ZipCode,
        IsActive, SubscriptionStartDate, SubscriptionPlan, TimeZone,
        CompanyId, CreatedAt, IsDeleted
    ) VALUES (
        'Demo Company', 'DEMO', 'Admin User', 'admin@demo.com',
        '(555) 123-4567', '123 Demo Street', 'Demo City', 'CA', '90210',
        1, GETUTCDATE(), 'Enterprise', 'America/New_York',
        0, GETUTCDATE(), 0
    );

    -- Update CompanyId to self-reference
    UPDATE Companies SET CompanyId = Id WHERE Id = 1;

    PRINT 'Default company created with ID = 1';
END
ELSE
BEGIN
    PRINT 'Companies table already exists';
END
GO

-- Step 2: Add CompanyId columns to all tables (if they don't exist)
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('AspNetUsers') AND name = 'CompanyId')
BEGIN
    ALTER TABLE AspNetUsers ADD CompanyId INT NULL;
    UPDATE AspNetUsers SET CompanyId = 1 WHERE CompanyId IS NULL;
    ALTER TABLE AspNetUsers ALTER COLUMN CompanyId INT NOT NULL;
    PRINT 'Added CompanyId to AspNetUsers';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('Customers') AND name = 'CompanyId')
BEGIN
    ALTER TABLE Customers ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to Customers';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('Jobs') AND name = 'CompanyId')
BEGIN
    ALTER TABLE Jobs ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to Jobs';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('JobItems') AND name = 'CompanyId')
BEGIN
    ALTER TABLE JobItems ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to JobItems';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('Quotes') AND name = 'CompanyId')
BEGIN
    ALTER TABLE Quotes ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to Quotes';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('QuoteItems') AND name = 'CompanyId')
BEGIN
    ALTER TABLE QuoteItems ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to QuoteItems';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('InventoryItems') AND name = 'CompanyId')
BEGIN
    ALTER TABLE InventoryItems ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to InventoryItems';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('InventoryTransactions') AND name = 'CompanyId')
BEGIN
    ALTER TABLE InventoryTransactions ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to InventoryTransactions';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('Equipment') AND name = 'CompanyId')
BEGIN
    ALTER TABLE Equipment ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to Equipment';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('MaintenanceRecords') AND name = 'CompanyId')
BEGIN
    ALTER TABLE MaintenanceRecords ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to MaintenanceRecords';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('Suppliers') AND name = 'CompanyId')
BEGIN
    ALTER TABLE Suppliers ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to Suppliers';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('PricingTiers') AND name = 'CompanyId')
BEGIN
    ALTER TABLE PricingTiers ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to PricingTiers';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('JobPhotos') AND name = 'CompanyId')
BEGIN
    ALTER TABLE JobPhotos ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to JobPhotos';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('JobNotes') AND name = 'CompanyId')
BEGIN
    ALTER TABLE JobNotes ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to JobNotes';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('CustomerNotes') AND name = 'CompanyId')
BEGIN
    ALTER TABLE CustomerNotes ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to CustomerNotes';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('JobStatusHistory') AND name = 'CompanyId')
BEGIN
    ALTER TABLE JobStatusHistory ADD CompanyId INT NOT NULL DEFAULT 1;
    PRINT 'Added CompanyId to JobStatusHistory';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('AspNetUsers') AND name = 'CompanyRole')
BEGIN
    ALTER TABLE AspNetUsers ADD CompanyRole NVARCHAR(MAX) NULL;
    PRINT 'Added CompanyRole to AspNetUsers';
END

PRINT 'Database prepared for multi-tenancy migration';
GO
