﻿BEGIN TRANSACTION;
GO

ALTER TABLE [Suppliers] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [Quotes] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [QuoteItems] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [PricingTiers] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [MaintenanceRecords] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [JobStatusHistory] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [Jobs] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [JobPhotos] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [JobNotes] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [JobItems] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [InventoryTransactions] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [InventoryItems] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [Equipment] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [Customers] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [CustomerNotes] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [AspNetUsers] ADD [CompanyId] int NOT NULL DEFAULT 0;
GO

ALTER TABLE [AspNetUsers] ADD [CompanyRole] nvarchar(max) NULL;
GO

CREATE TABLE [Companies] (
    [Id] int NOT NULL IDENTITY,
    [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,
    [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,
    [CreatedAt] datetime2 NOT NULL,
    [UpdatedAt] datetime2 NULL,
    [CreatedBy] nvarchar(max) NULL,
    [UpdatedBy] nvarchar(max) NULL,
    [IsDeleted] bit NOT NULL,
    [DeletedAt] datetime2 NULL,
    [DeletedBy] nvarchar(max) NULL,
    CONSTRAINT [PK_Companies] PRIMARY KEY ([Id])
);
GO

UPDATE [PricingTiers] SET [CompanyId] = 0, [CreatedAt] = '2026-02-06T00:44:39.1275198Z'
WHERE [Id] = 1;
SELECT @@ROWCOUNT;

GO

UPDATE [PricingTiers] SET [CompanyId] = 0, [CreatedAt] = '2026-02-06T00:44:39.1275205Z'
WHERE [Id] = 2;
SELECT @@ROWCOUNT;

GO

UPDATE [PricingTiers] SET [CompanyId] = 0, [CreatedAt] = '2026-02-06T00:44:39.1275207Z'
WHERE [Id] = 3;
SELECT @@ROWCOUNT;

GO

CREATE INDEX [IX_Suppliers_CompanyId] ON [Suppliers] ([CompanyId]);
GO

CREATE INDEX [IX_Quotes_CompanyId] ON [Quotes] ([CompanyId]);
GO

CREATE INDEX [IX_PricingTiers_CompanyId] ON [PricingTiers] ([CompanyId]);
GO

CREATE INDEX [IX_Jobs_CompanyId] ON [Jobs] ([CompanyId]);
GO

CREATE INDEX [IX_InventoryItems_CompanyId] ON [InventoryItems] ([CompanyId]);
GO

CREATE INDEX [IX_Equipment_CompanyId] ON [Equipment] ([CompanyId]);
GO

CREATE INDEX [IX_Customers_CompanyId] ON [Customers] ([CompanyId]);
GO

CREATE INDEX [IX_AspNetUsers_CompanyId] ON [AspNetUsers] ([CompanyId]);
GO

CREATE UNIQUE INDEX [IX_Companies_CompanyCode] ON [Companies] ([CompanyCode]) WHERE [CompanyCode] IS NOT NULL;
GO

ALTER TABLE [AspNetUsers] ADD CONSTRAINT [FK_AspNetUsers_Companies_CompanyId] FOREIGN KEY ([CompanyId]) REFERENCES [Companies] ([Id]) ON DELETE NO ACTION;
GO

ALTER TABLE [Customers] ADD CONSTRAINT [FK_Customers_Companies_CompanyId] FOREIGN KEY ([CompanyId]) REFERENCES [Companies] ([Id]) ON DELETE NO ACTION;
GO

ALTER TABLE [Equipment] ADD CONSTRAINT [FK_Equipment_Companies_CompanyId] FOREIGN KEY ([CompanyId]) REFERENCES [Companies] ([Id]) ON DELETE NO ACTION;
GO

ALTER TABLE [InventoryItems] ADD CONSTRAINT [FK_InventoryItems_Companies_CompanyId] FOREIGN KEY ([CompanyId]) REFERENCES [Companies] ([Id]) ON DELETE NO ACTION;
GO

ALTER TABLE [Jobs] ADD CONSTRAINT [FK_Jobs_Companies_CompanyId] FOREIGN KEY ([CompanyId]) REFERENCES [Companies] ([Id]) ON DELETE NO ACTION;
GO

ALTER TABLE [PricingTiers] ADD CONSTRAINT [FK_PricingTiers_Companies_CompanyId] FOREIGN KEY ([CompanyId]) REFERENCES [Companies] ([Id]) ON DELETE NO ACTION;
GO

ALTER TABLE [Quotes] ADD CONSTRAINT [FK_Quotes_Companies_CompanyId] FOREIGN KEY ([CompanyId]) REFERENCES [Companies] ([Id]) ON DELETE NO ACTION;
GO

ALTER TABLE [Suppliers] ADD CONSTRAINT [FK_Suppliers_Companies_CompanyId] FOREIGN KEY ([CompanyId]) REFERENCES [Companies] ([Id]) ON DELETE NO ACTION;
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20260206004522_AddMultiTenancyFixed', N'8.0.11');
GO

COMMIT;
GO

