﻿-- =============================================================================
-- PowderCoating App - Initial Seed SQL
-- Generated: 2026-02-28 04:10:44 UTC
--
-- Run this against your Azure SQL database when startup seeding fails.
-- All inserts are guarded with IF NOT EXISTS and are safe to re-run.
-- =============================================================================

SET NOCOUNT ON;
GO

-- 1. Default Company

IF NOT EXISTS (SELECT 1 FROM Companies WHERE CompanyCode = 'DEMO')
BEGIN
    SET IDENTITY_INSERT Companies ON;

    INSERT INTO Companies (
        Id, CompanyId, CompanyName, CompanyCode,
        PrimaryContactName, PrimaryContactEmail, Phone,
        Address, City, State, ZipCode,
        IsActive, SubscriptionPlan, SubscriptionStatus,
        SubscriptionStartDate, TimeZone,
        CreatedAt, IsDeleted
    ) VALUES (
        1, 1, 'Demo Company', 'DEMO',
        'Admin User', 'admin@demo.com', '(555) 123-4567',
        '123 Demo Street', 'Demo City', 'CA', '90210',
        1, 2, 0,
        '2026-02-28 04:10:44', 'America/New_York',
        '2026-02-28 04:10:44', 0
    );

    SET IDENTITY_INSERT Companies OFF;
    PRINT 'Company inserted.';
END
ELSE
    PRINT 'Company already exists - skipped.';
GO

-- 2. Roles

IF NOT EXISTS (SELECT 1 FROM AspNetRoles WHERE NormalizedName = 'SUPERADMIN')
    INSERT INTO AspNetRoles (Id, Name, NormalizedName, ConcurrencyStamp)
    VALUES ('208a163b-c303-4083-856e-4060bc5a923d', 'SuperAdmin', 'SUPERADMIN', NEWID());

IF NOT EXISTS (SELECT 1 FROM AspNetRoles WHERE NormalizedName = 'ADMINISTRATOR')
    INSERT INTO AspNetRoles (Id, Name, NormalizedName, ConcurrencyStamp)
    VALUES ('bf92ab6a-0d14-42d5-b19d-b5f1a5da8df0', 'Administrator', 'ADMINISTRATOR', NEWID());

IF NOT EXISTS (SELECT 1 FROM AspNetRoles WHERE NormalizedName = 'MANAGER')
    INSERT INTO AspNetRoles (Id, Name, NormalizedName, ConcurrencyStamp)
    VALUES ('c12dcd9d-408a-4d38-ba55-ef2a3f88ccb5', 'Manager', 'MANAGER', NEWID());

IF NOT EXISTS (SELECT 1 FROM AspNetRoles WHERE NormalizedName = 'EMPLOYEE')
    INSERT INTO AspNetRoles (Id, Name, NormalizedName, ConcurrencyStamp)
    VALUES ('1ab093d9-f1fb-439e-9194-5fe76be041dc', 'Employee', 'EMPLOYEE', NEWID());

IF NOT EXISTS (SELECT 1 FROM AspNetRoles WHERE NormalizedName = 'SHOPFLOOR')
    INSERT INTO AspNetRoles (Id, Name, NormalizedName, ConcurrencyStamp)
    VALUES ('13ba213d-3118-4d93-ac5d-d0674369f409', 'ShopFloor', 'SHOPFLOOR', NEWID());

IF NOT EXISTS (SELECT 1 FROM AspNetRoles WHERE NormalizedName = 'READONLY')
    INSERT INTO AspNetRoles (Id, Name, NormalizedName, ConcurrencyStamp)
    VALUES ('1181bde5-0034-439b-8351-c7821faf6c28', 'ReadOnly', 'READONLY', NEWID());

PRINT 'Roles inserted.';
GO

-- 3. SuperAdmin Users

-- User 1: superadmin@powdercoating.com / SuperAdmin123!
IF NOT EXISTS (SELECT 1 FROM AspNetUsers WHERE NormalizedEmail = 'SUPERADMIN@POWDERCOATING.COM')
BEGIN
    INSERT INTO AspNetUsers (
        Id, UserName, NormalizedUserName, Email, NormalizedEmail,
        EmailConfirmed, PasswordHash, SecurityStamp, ConcurrencyStamp,
        PhoneNumber, PhoneNumberConfirmed, TwoFactorEnabled,
        LockoutEnd, LockoutEnabled, AccessFailedCount,
        CompanyId, CompanyRole,
        FirstName, LastName, EmployeeNumber,
        HireDate, IsActive,
        Department, Position, HourlyRate,
        Theme, DateFormat, TimeZone, SidebarColor,
        CanViewShopFloor, CanManageJobs, CanManageInventory, CanManageCustomers,
        CanCreateQuotes, CanApproveQuotes, CanManageCalendar, CanViewCalendar,
        CanManageProducts, CanViewProducts, CanManageEquipment,
        CanManageSuppliers, CanManageMaintenance,
        CreatedAt
    ) VALUES (
        'd0a94944-60ab-49dd-aff5-3f9ae6a351bc',
        'superadmin@powdercoating.com', 'SUPERADMIN@POWDERCOATING.COM',
        'superadmin@powdercoating.com', 'SUPERADMIN@POWDERCOATING.COM',
        1, 'AQAAAAEAAYagAAAAEFSYOADLy+Inki1mJqCFcIUyEsv584NI8kyE5rorPABmuphaRajTEFbXK/VoCGQrJA==', '3b604497-51cd-401f-92cd-eda6c7c60ff2', '7f532e9b-89ee-4a6c-ab12-ab16b69f45a9',
        NULL, 0, 0, NULL, 1, 0,
        1, NULL,
        'Super', 'Admin', 'SA-001',
        '2026-02-28 04:10:44', 1,
        'Platform', 'Super Administrator', 0,
        'light', 'MM/dd/yyyy', 'America/New_York', 'ocean',
        1, 1, 1, 1,
        1, 1, 1, 1,
        1, 1, 1,
        1, 1,
        '2026-02-28 04:10:44'
    );
    PRINT 'User superadmin@powdercoating.com inserted.';
END
ELSE
    PRINT 'User superadmin@powdercoating.com already exists - skipped.';
GO

-- User 2: admin@powdercoating.com / Admin123!
IF NOT EXISTS (SELECT 1 FROM AspNetUsers WHERE NormalizedEmail = 'ADMIN@POWDERCOATING.COM')
BEGIN
    INSERT INTO AspNetUsers (
        Id, UserName, NormalizedUserName, Email, NormalizedEmail,
        EmailConfirmed, PasswordHash, SecurityStamp, ConcurrencyStamp,
        PhoneNumber, PhoneNumberConfirmed, TwoFactorEnabled,
        LockoutEnd, LockoutEnabled, AccessFailedCount,
        CompanyId, CompanyRole,
        FirstName, LastName, EmployeeNumber,
        HireDate, IsActive,
        Department, Position, HourlyRate,
        Theme, DateFormat, TimeZone, SidebarColor,
        CanViewShopFloor, CanManageJobs, CanManageInventory, CanManageCustomers,
        CanCreateQuotes, CanApproveQuotes, CanManageCalendar, CanViewCalendar,
        CanManageProducts, CanViewProducts, CanManageEquipment,
        CanManageSuppliers, CanManageMaintenance,
        CreatedAt
    ) VALUES (
        '6b0f44fe-8547-41ac-81b7-6451a49077c3',
        'admin@powdercoating.com', 'ADMIN@POWDERCOATING.COM',
        'admin@powdercoating.com', 'ADMIN@POWDERCOATING.COM',
        1, 'AQAAAAEAAYagAAAAENnCYaYw3wsQ5ZW8u7tHBs6eEtAcFKJ+35B93yDq0TMiP524/UcWO8qEoTcfyiAJnw==', '7d2b9944-345a-4819-af02-2f7f56e28120', 'b4b820d5-3168-465a-b107-2a0e99e3eab1',
        NULL, 0, 0, NULL, 1, 0,
        1, NULL,
        'Admin', 'User', 'SA-002',
        '2026-02-28 04:10:44', 1,
        'Platform', 'Platform Administrator', 0,
        'light', 'MM/dd/yyyy', 'America/New_York', 'ocean',
        1, 1, 1, 1,
        1, 1, 1, 1,
        1, 1, 1,
        1, 1,
        '2026-02-28 04:10:44'
    );
    PRINT 'User admin@powdercoating.com inserted.';
END
ELSE
    PRINT 'User admin@powdercoating.com already exists - skipped.';
GO

-- 4. Assign SuperAdmin role to both users

DECLARE @roleId   NVARCHAR(450) = (SELECT Id FROM AspNetRoles WHERE NormalizedName = 'SUPERADMIN');
DECLARE @u1Id     NVARCHAR(450) = (SELECT Id FROM AspNetUsers  WHERE NormalizedEmail = 'SUPERADMIN@POWDERCOATING.COM');
DECLARE @u2Id     NVARCHAR(450) = (SELECT Id FROM AspNetUsers  WHERE NormalizedEmail = 'ADMIN@POWDERCOATING.COM');

IF @u1Id IS NOT NULL AND @roleId IS NOT NULL
    AND NOT EXISTS (SELECT 1 FROM AspNetUserRoles WHERE UserId = @u1Id AND RoleId = @roleId)
BEGIN
    INSERT INTO AspNetUserRoles (UserId, RoleId) VALUES (@u1Id, @roleId);
    PRINT 'Role assigned to superadmin@powdercoating.com.';
END

IF @u2Id IS NOT NULL AND @roleId IS NOT NULL
    AND NOT EXISTS (SELECT 1 FROM AspNetUserRoles WHERE UserId = @u2Id AND RoleId = @roleId)
BEGIN
    INSERT INTO AspNetUserRoles (UserId, RoleId) VALUES (@u2Id, @roleId);
    PRINT 'Role assigned to admin@powdercoating.com.';
END
GO

PRINT '================================================';
PRINT 'Seed complete.';
PRINT 'Login 1: superadmin@powdercoating.com / SuperAdmin123!';
PRINT 'Login 2: admin@powdercoating.com / Admin123!';
PRINT '================================================';
GO
