-- Safe Multi-Tenancy Migration Script with proper SQL Server settings
USE PowderCoatingDb;
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO

PRINT '=== Starting Multi-Tenancy Migration ===';

-- Step 1: Verify Companies table was created
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Companies')
BEGIN
    RAISERROR('ERROR: Companies table was not created properly!', 16, 1);
    RETURN;
END
PRINT 'Companies table exists';

-- Step 2: Add CompanyId to AspNetUsers
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[AspNetUsers]') AND name = 'CompanyId')
BEGIN
    ALTER TABLE [dbo].[AspNetUsers] ADD [CompanyId] INT NOT NULL CONSTRAINT DF_AspNetUsers_CompanyId DEFAULT 1;
    PRINT 'Added CompanyId to AspNetUsers';
END
ELSE
BEGIN
    PRINT 'CompanyId already exists in AspNetUsers';
END

-- Step 3: Add CompanyRole to AspNetUsers
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[AspNetUsers]') AND name = 'CompanyRole')
BEGIN
    ALTER TABLE [dbo].[AspNetUsers] ADD [CompanyRole] NVARCHAR(MAX) NULL;
    PRINT 'Added CompanyRole to AspNetUsers';
END

-- Step 4: Add CompanyId to all other tables
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[Customers]') AND name = 'CompanyId')
BEGIN
    ALTER TABLE [dbo].[Customers] ADD [CompanyId] INT NOT NULL CONSTRAINT DF_Customers_CompanyId DEFAULT 1;
    PRINT 'Added CompanyId to Customers';
END

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

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

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

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

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

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

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

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

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

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[PricingTiers]') AND name = 'CompanyId')
BEGIN
    ALTER TABLE [dbo].[PricingTiers] ADD [CompanyId] INT NOT NULL CONSTRAINT DF_PricingTiers_CompanyId DEFAULT 1;
    PRINT 'Added CompanyId to PricingTiers';
    -- Update seeded pricing tiers
    UPDATE [dbo].[PricingTiers] SET [CompanyId] = 1 WHERE [CompanyId] = 0;
END

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

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

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

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

GO

-- Step 5: Create Indexes
PRINT '=== Creating Indexes ===';

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_Customers_CompanyId')
    CREATE INDEX [IX_Customers_CompanyId] ON [dbo].[Customers]([CompanyId]);

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_Jobs_CompanyId')
    CREATE INDEX [IX_Jobs_CompanyId] ON [dbo].[Jobs]([CompanyId]);

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_Equipment_CompanyId')
    CREATE INDEX [IX_Equipment_CompanyId] ON [dbo].[Equipment]([CompanyId]);

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_Quotes_CompanyId')
    CREATE INDEX [IX_Quotes_CompanyId] ON [dbo].[Quotes]([CompanyId]);

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_InventoryItems_CompanyId')
    CREATE INDEX [IX_InventoryItems_CompanyId] ON [dbo].[InventoryItems]([CompanyId]);

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_Suppliers_CompanyId')
    CREATE INDEX [IX_Suppliers_CompanyId] ON [dbo].[Suppliers]([CompanyId]);

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_PricingTiers_CompanyId')
    CREATE INDEX [IX_PricingTiers_CompanyId] ON [dbo].[PricingTiers]([CompanyId]);

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_AspNetUsers_CompanyId')
    CREATE INDEX [IX_AspNetUsers_CompanyId] ON [dbo].[AspNetUsers]([CompanyId]);

PRINT 'Indexes created';
GO

-- Step 6: Add Foreign Keys
PRINT '=== Adding Foreign Keys ===';

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE name = 'FK_AspNetUsers_Companies_CompanyId')
BEGIN
    ALTER TABLE [dbo].[AspNetUsers]
        ADD CONSTRAINT [FK_AspNetUsers_Companies_CompanyId]
        FOREIGN KEY ([CompanyId]) REFERENCES [dbo].[Companies]([Id]);
    PRINT 'Added FK: AspNetUsers -> Companies';
END

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE name = 'FK_Customers_Companies_CompanyId')
BEGIN
    ALTER TABLE [dbo].[Customers]
        ADD CONSTRAINT [FK_Customers_Companies_CompanyId]
        FOREIGN KEY ([CompanyId]) REFERENCES [dbo].[Companies]([Id]);
    PRINT 'Added FK: Customers -> Companies';
END

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE name = 'FK_Jobs_Companies_CompanyId')
BEGIN
    ALTER TABLE [dbo].[Jobs]
        ADD CONSTRAINT [FK_Jobs_Companies_CompanyId]
        FOREIGN KEY ([CompanyId]) REFERENCES [dbo].[Companies]([Id]);
    PRINT 'Added FK: Jobs -> Companies';
END

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE name = 'FK_Equipment_Companies_CompanyId')
BEGIN
    ALTER TABLE [dbo].[Equipment]
        ADD CONSTRAINT [FK_Equipment_Companies_CompanyId]
        FOREIGN KEY ([CompanyId]) REFERENCES [dbo].[Companies]([Id]);
    PRINT 'Added FK: Equipment -> Companies';
END

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE name = 'FK_Quotes_Companies_CompanyId')
BEGIN
    ALTER TABLE [dbo].[Quotes]
        ADD CONSTRAINT [FK_Quotes_Companies_CompanyId]
        FOREIGN KEY ([CompanyId]) REFERENCES [dbo].[Companies]([Id]);
    PRINT 'Added FK: Quotes -> Companies';
END

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE name = 'FK_InventoryItems_Companies_CompanyId')
BEGIN
    ALTER TABLE [dbo].[InventoryItems]
        ADD CONSTRAINT [FK_InventoryItems_Companies_CompanyId]
        FOREIGN KEY ([CompanyId]) REFERENCES [dbo].[Companies]([Id]);
    PRINT 'Added FK: InventoryItems -> Companies';
END

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE name = 'FK_Suppliers_Companies_CompanyId')
BEGIN
    ALTER TABLE [dbo].[Suppliers]
        ADD CONSTRAINT [FK_Suppliers_Companies_CompanyId]
        FOREIGN KEY ([CompanyId]) REFERENCES [dbo].[Companies]([Id]);
    PRINT 'Added FK: Suppliers -> Companies';
END

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE name = 'FK_PricingTiers_Companies_CompanyId')
BEGIN
    ALTER TABLE [dbo].[PricingTiers]
        ADD CONSTRAINT [FK_PricingTiers_Companies_CompanyId]
        FOREIGN KEY ([CompanyId]) REFERENCES [dbo].[Companies]([Id]);
    PRINT 'Added FK: PricingTiers -> Companies';
END

PRINT 'All foreign keys added';
GO

PRINT '';
PRINT '==============================================';
PRINT '   Multi-Tenancy Migration COMPLETE!';
PRINT '==============================================';
PRINT '';
PRINT 'Next steps:';
PRINT '1. Run: dotnet run --project src/PowderCoating.Web';
PRINT '2. Login: superadmin@powdercoating.com / SuperAdmin123!';
PRINT '3. Or login: admin@demo.com / CompanyAdmin123!';
PRINT '';
PRINT '==============================================';
GO
