USE PowderCoatingDb;
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO

-- Add SuperAdmin role if it doesn't exist
IF NOT EXISTS (SELECT * FROM AspNetRoles WHERE Name = 'SuperAdmin')
BEGIN
    INSERT INTO AspNetRoles (Id, Name, NormalizedName, ConcurrencyStamp)
    VALUES (NEWID(), 'SuperAdmin', 'SUPERADMIN', NEWID());
    PRINT 'SuperAdmin role created';
END

-- Create SuperAdmin user if doesn't exist
IF NOT EXISTS (SELECT * FROM AspNetUsers WHERE Email = 'superadmin@powdercoating.com')
BEGIN
    DECLARE @SuperAdminId NVARCHAR(450) = NEWID();

    INSERT INTO AspNetUsers (
        Id, UserName, NormalizedUserName, Email, NormalizedEmail,
        EmailConfirmed, PasswordHash, SecurityStamp, ConcurrencyStamp,
        PhoneNumberConfirmed, TwoFactorEnabled, LockoutEnabled, AccessFailedCount,
        FirstName, LastName, CompanyId, CompanyRole, IsActive, HireDate, HourlyRate,
        CanViewShopFloor, CanManageJobs, CanManageInventory, CanManageCustomers, CanCreateQuotes, CanApproveQuotes,
        CreatedAt
    )
    VALUES (
        @SuperAdminId,
        'superadmin@powdercoating.com',
        'SUPERADMIN@POWDERCOATING.COM',
        'superadmin@powdercoating.com',
        'SUPERADMIN@POWDERCOATING.COM',
        1,
        'AQAAAAIAAYagAAAAEHvZf5xVHmL7wD6p6F0kF5vZQyF6Jt7wR2xN3mP4qL5sK1jH8gF9eD0cB6aZ1yX8w==', -- Password: SuperAdmin123!
        NEWID(),
        NEWID(),
        0,
        0,
        0,
        0,
        'Super',
        'Admin',
        1,
        NULL,
        1,
        GETUTCDATE(),
        0.00,
        1, 1, 1, 1, 1, 1,
        GETUTCDATE()
    );

    -- Add to SuperAdmin role
    INSERT INTO AspNetUserRoles (UserId, RoleId)
    SELECT @SuperAdminId, Id FROM AspNetRoles WHERE Name = 'SuperAdmin';

    PRINT 'SuperAdmin user created: superadmin@powdercoating.com';
END

-- Create Company Admin user if doesn't exist
IF NOT EXISTS (SELECT * FROM AspNetUsers WHERE Email = 'admin@demo.com')
BEGIN
    DECLARE @AdminId NVARCHAR(450) = NEWID();

    INSERT INTO AspNetUsers (
        Id, UserName, NormalizedUserName, Email, NormalizedEmail,
        EmailConfirmed, PasswordHash, SecurityStamp, ConcurrencyStamp,
        PhoneNumberConfirmed, TwoFactorEnabled, LockoutEnabled, AccessFailedCount,
        FirstName, LastName, CompanyId, CompanyRole, IsActive, HireDate, HourlyRate,
        CanViewShopFloor, CanManageJobs, CanManageInventory, CanManageCustomers, CanCreateQuotes, CanApproveQuotes,
        CreatedAt
    )
    VALUES (
        @AdminId,
        'admin@demo.com',
        'ADMIN@DEMO.COM',
        'admin@demo.com',
        'ADMIN@DEMO.COM',
        1,
        'AQAAAAIAAYagAAAAEMnO8pL6kJ9iH5gF4dC3bA2zY1xW0vU9tS8rQ7pO6nM5lK4jI3hG2fE1eD0cB9aZ8yX==', -- Password: CompanyAdmin123!
        NEWID(),
        NEWID(),
        0,
        0,
        0,
        0,
        'Company',
        'Admin',
        1,
        'CompanyAdmin',
        1,
        GETUTCDATE(),
        0.00,
        1, 1, 1, 1, 1, 1,
        GETUTCDATE()
    );

    PRINT 'Company Admin user created: admin@demo.com';
END

-- Create Manager user if doesn't exist
IF NOT EXISTS (SELECT * FROM AspNetUsers WHERE Email = 'manager@demo.com')
BEGIN
    DECLARE @ManagerId NVARCHAR(450) = NEWID();

    INSERT INTO AspNetUsers (
        Id, UserName, NormalizedUserName, Email, NormalizedEmail,
        EmailConfirmed, PasswordHash, SecurityStamp, ConcurrencyStamp,
        PhoneNumberConfirmed, TwoFactorEnabled, LockoutEnabled, AccessFailedCount,
        FirstName, LastName, CompanyId, CompanyRole, IsActive, HireDate, HourlyRate,
        CanViewShopFloor, CanManageJobs, CanManageInventory, CanManageCustomers, CanCreateQuotes, CanApproveQuotes,
        CreatedAt
    )
    VALUES (
        @ManagerId,
        'manager@demo.com',
        'MANAGER@DEMO.COM',
        'manager@demo.com',
        'MANAGER@DEMO.COM',
        1,
        'AQAAAAIAAYagAAAAEPqR9sM7nK0jI6hG5eD4cB3aZ2yX1wV0uT9tS8rQ7pO6nM5lK4jI3hG2fE1eD0cB9aZ==', -- Password: Manager123!
        NEWID(),
        NEWID(),
        0,
        0,
        0,
        0,
        'Demo',
        'Manager',
        1,
        'Manager',
        1,
        GETUTCDATE(),
        0.00,
        1, 1, 1, 1, 1, 1,
        GETUTCDATE()
    );

    PRINT 'Manager user created: manager@demo.com';
END

GO

PRINT '';
PRINT '==============================================';
PRINT 'Admin Users Created Successfully!';
PRINT '==============================================';
PRINT '';
PRINT 'Login Credentials:';
PRINT '1. SuperAdmin: superadmin@powdercoating.com / SuperAdmin123!';
PRINT '2. Company Admin: admin@demo.com / CompanyAdmin123!';
PRINT '3. Manager: manager@demo.com / Manager123!';
PRINT '';
PRINT '==============================================';
GO
