-- ============================================================================
-- ERP GENERIC - SEED DATA: USERS (Development)
-- ============================================================================
-- Description: Development users for testing
-- Password for all users: Test1234 (bcrypt hash)
-- ============================================================================

-- Password hash for "Test1234" using bcrypt (generated with bcryptjs, 10 rounds)
-- Hash: $2a$10$EbJ0czCHXi8LatF2e7NfkeA/RLMn2R8w1ShDnHVUtQRnosEokense
-- Note: You should regenerate this in production

-- Super Admin (is_superuser=true, assigned to Demo tenant)
INSERT INTO auth.users (id, tenant_id, email, password_hash, full_name, status, is_superuser, email_verified_at, created_at)
VALUES (
    '0bb44df3-ec99-4306-85e9-50c34dd7d27a',
    '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
    'superadmin@erp-generic.local',
    '$2a$10$EbJ0czCHXi8LatF2e7NfkeA/RLMn2R8w1ShDnHVUtQRnosEokense',
    'Super Admin',
    'active',
    true,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
) ON CONFLICT (id) DO NOTHING;

-- Assign super_admin role
INSERT INTO auth.user_roles (user_id, role_id, assigned_at)
VALUES (
    '0bb44df3-ec99-4306-85e9-50c34dd7d27a',
    '5e29aadd-1d9f-4280-a38b-fefe7cdece5a',
    CURRENT_TIMESTAMP
) ON CONFLICT DO NOTHING;

-- ===========================================
-- DEMO COMPANY USERS
-- ===========================================

-- Admin user
INSERT INTO auth.users (id, tenant_id, email, password_hash, full_name, status, email_verified_at, created_at)
VALUES (
    'e6f9a1fd-2a56-496c-9dc5-f603e1a910dd',
    '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
    'admin@demo-company.mx',
    '$2a$10$EbJ0czCHXi8LatF2e7NfkeA/RLMn2R8w1ShDnHVUtQRnosEokense',
    'Carlos Administrador',
    'active',
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
) ON CONFLICT (id) DO NOTHING;

INSERT INTO auth.user_roles (user_id, role_id, assigned_at)
VALUES ('e6f9a1fd-2a56-496c-9dc5-f603e1a910dd', 'fed1cfa2-8ea1-4d86-bfef-b3dcc08801c2', CURRENT_TIMESTAMP)
ON CONFLICT DO NOTHING;

INSERT INTO auth.user_companies (user_id, company_id, is_default, assigned_at)
VALUES ('e6f9a1fd-2a56-496c-9dc5-f603e1a910dd', '50fa9b29-504f-4c45-8f8a-3d129cfc6095', true, CURRENT_TIMESTAMP)
ON CONFLICT DO NOTHING;

-- Manager user
INSERT INTO auth.users (id, tenant_id, email, password_hash, full_name, status, email_verified_at, created_at)
VALUES (
    'c8013936-53ad-4c6a-8f50-d7c7be1da9de',
    '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
    'manager@demo-company.mx',
    '$2a$10$EbJ0czCHXi8LatF2e7NfkeA/RLMn2R8w1ShDnHVUtQRnosEokense',
    'María Gerente',
    'active',
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
) ON CONFLICT (id) DO NOTHING;

INSERT INTO auth.user_roles (user_id, role_id, assigned_at)
VALUES ('c8013936-53ad-4c6a-8f50-d7c7be1da9de', '1a35fbf0-a282-487d-95ef-13b3f702e8d6', CURRENT_TIMESTAMP)
ON CONFLICT DO NOTHING;

INSERT INTO auth.user_companies (user_id, company_id, is_default, assigned_at)
VALUES ('c8013936-53ad-4c6a-8f50-d7c7be1da9de', '50fa9b29-504f-4c45-8f8a-3d129cfc6095', true, CURRENT_TIMESTAMP)
ON CONFLICT DO NOTHING;

-- Accountant user
INSERT INTO auth.users (id, tenant_id, email, password_hash, full_name, status, email_verified_at, created_at)
VALUES (
    '1110b920-a7ab-4303-aa9e-4b2fafe44f84',
    '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
    'contador@demo-company.mx',
    '$2a$10$EbJ0czCHXi8LatF2e7NfkeA/RLMn2R8w1ShDnHVUtQRnosEokense',
    'Juan Contador',
    'active',
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
) ON CONFLICT (id) DO NOTHING;

INSERT INTO auth.user_roles (user_id, role_id, assigned_at)
VALUES ('1110b920-a7ab-4303-aa9e-4b2fafe44f84', 'c91f1a60-bd0d-40d3-91b8-36c226ce3d29', CURRENT_TIMESTAMP)
ON CONFLICT DO NOTHING;

INSERT INTO auth.user_companies (user_id, company_id, is_default, assigned_at)
VALUES ('1110b920-a7ab-4303-aa9e-4b2fafe44f84', '50fa9b29-504f-4c45-8f8a-3d129cfc6095', true, CURRENT_TIMESTAMP)
ON CONFLICT DO NOTHING;

-- Sales user
INSERT INTO auth.users (id, tenant_id, email, password_hash, full_name, status, email_verified_at, created_at)
VALUES (
    '607fc4d8-374c-4693-b601-81f522a857ab',
    '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
    'ventas@demo-company.mx',
    '$2a$10$EbJ0czCHXi8LatF2e7NfkeA/RLMn2R8w1ShDnHVUtQRnosEokense',
    'Ana Ventas',
    'active',
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
) ON CONFLICT (id) DO NOTHING;

INSERT INTO auth.user_roles (user_id, role_id, assigned_at)
VALUES ('607fc4d8-374c-4693-b601-81f522a857ab', '493568ed-972f-472f-9ac1-236a32438936', CURRENT_TIMESTAMP)
ON CONFLICT DO NOTHING;

INSERT INTO auth.user_companies (user_id, company_id, is_default, assigned_at)
VALUES ('607fc4d8-374c-4693-b601-81f522a857ab', '50fa9b29-504f-4c45-8f8a-3d129cfc6095', true, CURRENT_TIMESTAMP)
ON CONFLICT DO NOTHING;

-- Warehouse user
INSERT INTO auth.users (id, tenant_id, email, password_hash, full_name, status, email_verified_at, created_at)
VALUES (
    '7c7f132b-4551-4864-bafd-36147e626bb7',
    '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
    'almacen@demo-company.mx',
    '$2a$10$EbJ0czCHXi8LatF2e7NfkeA/RLMn2R8w1ShDnHVUtQRnosEokense',
    'Pedro Almacén',
    'active',
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
) ON CONFLICT (id) DO NOTHING;

INSERT INTO auth.user_roles (user_id, role_id, assigned_at)
VALUES ('7c7f132b-4551-4864-bafd-36147e626bb7', '0a86a34a-7fd6-47e2-9e0c-4c547c6af9f1', CURRENT_TIMESTAMP)
ON CONFLICT DO NOTHING;

INSERT INTO auth.user_companies (user_id, company_id, is_default, assigned_at)
VALUES ('7c7f132b-4551-4864-bafd-36147e626bb7', '50fa9b29-504f-4c45-8f8a-3d129cfc6095', true, CURRENT_TIMESTAMP)
ON CONFLICT DO NOTHING;

-- Output confirmation
DO $$
BEGIN
    RAISE NOTICE 'Users seed data loaded: 6 users created';
    RAISE NOTICE 'Default password for all users: Test1234';
END $$;
