-- Fix script for enterprise demo tenants created with old naming
-- This fixes tenants that have:
-- 1. Wrong owner_id (María instead of Carlos)
-- 2. business_model = 'enterprise_parent' instead of 'enterprise'
-- 3. Missing TenantMember records

-- Transaction to ensure atomicity
BEGIN;

-- Carlos's user ID (correct enterprise owner)
-- María's user ID (wrong - used for professional): c1a2b3c4-d5e6-47a8-b9c0-d1e2f3a4b5c6
-- Carlos's user ID (correct - for enterprise): d2e3f4a5-b6c7-48d9-e0f1-a2b3c4d5e6f7

-- 1. Update tenant to have correct owner and business model
UPDATE tenants
SET
    owner_id = 'd2e3f4a5-b6c7-48d9-e0f1-a2b3c4d5e6f7',
    business_model = CASE
        WHEN business_model = 'enterprise_parent' THEN 'enterprise'
        WHEN business_model = 'enterprise_chain' THEN 'enterprise'
        ELSE business_model
    END
WHERE id = '3fe07312-b325-4b40-97dd-c8d1c0a67ec7';

-- 2. Create TenantMember record for Carlos (owner)
INSERT INTO tenant_members (
    id,
    tenant_id,
    user_id,
    role,
    permissions,
    is_active,
    invited_by,
    invited_at,
    joined_at,
    created_at
) VALUES (
    gen_random_uuid(),
    '3fe07312-b325-4b40-97dd-c8d1c0a67ec7',
    'd2e3f4a5-b6c7-48d9-e0f1-a2b3c4d5e6f7',  -- Carlos
    'owner',
    '["read", "write", "admin", "delete"]',
    true,
    'd2e3f4a5-b6c7-48d9-e0f1-a2b3c4d5e6f7',  -- Self-invited
    NOW(),
    NOW(),
    NOW()
) ON CONFLICT DO NOTHING;

-- 3. Create TenantMember records for enterprise staff
-- Production Manager
INSERT INTO tenant_members (id, tenant_id, user_id, role, permissions, is_active, invited_by, invited_at, joined_at, created_at)
VALUES (gen_random_uuid(), '3fe07312-b325-4b40-97dd-c8d1c0a67ec7', '50000000-0000-0000-0000-000000000011', 'production_manager', '["read", "write"]', true, 'd2e3f4a5-b6c7-48d9-e0f1-a2b3c4d5e6f7', NOW(), NOW(), NOW())
ON CONFLICT DO NOTHING;

-- Quality Control
INSERT INTO tenant_members (id, tenant_id, user_id, role, permissions, is_active, invited_by, invited_at, joined_at, created_at)
VALUES (gen_random_uuid(), '3fe07312-b325-4b40-97dd-c8d1c0a67ec7', '50000000-0000-0000-0000-000000000012', 'quality_control', '["read", "write"]', true, 'd2e3f4a5-b6c7-48d9-e0f1-a2b3c4d5e6f7', NOW(), NOW(), NOW())
ON CONFLICT DO NOTHING;

-- Logistics
INSERT INTO tenant_members (id, tenant_id, user_id, role, permissions, is_active, invited_by, invited_at, joined_at, created_at)
VALUES (gen_random_uuid(), '3fe07312-b325-4b40-97dd-c8d1c0a67ec7', '50000000-0000-0000-0000-000000000013', 'logistics', '["read", "write"]', true, 'd2e3f4a5-b6c7-48d9-e0f1-a2b3c4d5e6f7', NOW(), NOW(), NOW())
ON CONFLICT DO NOTHING;

-- Sales
INSERT INTO tenant_members (id, tenant_id, user_id, role, permissions, is_active, invited_by, invited_at, joined_at, created_at)
VALUES (gen_random_uuid(), '3fe07312-b325-4b40-97dd-c8d1c0a67ec7', '50000000-0000-0000-0000-000000000014', 'sales', '["read", "write"]', true, 'd2e3f4a5-b6c7-48d9-e0f1-a2b3c4d5e6f7', NOW(), NOW(), NOW())
ON CONFLICT DO NOTHING;

-- Procurement
INSERT INTO tenant_members (id, tenant_id, user_id, role, permissions, is_active, invited_by, invited_at, joined_at, created_at)
VALUES (gen_random_uuid(), '3fe07312-b325-4b40-97dd-c8d1c0a67ec7', '50000000-0000-0000-0000-000000000015', 'procurement', '["read", "write"]', true, 'd2e3f4a5-b6c7-48d9-e0f1-a2b3c4d5e6f7', NOW(), NOW(), NOW())
ON CONFLICT DO NOTHING;

-- Maintenance
INSERT INTO tenant_members (id, tenant_id, user_id, role, permissions, is_active, invited_by, invited_at, joined_at, created_at)
VALUES (gen_random_uuid(), '3fe07312-b325-4b40-97dd-c8d1c0a67ec7', '50000000-0000-0000-0000-000000000016', 'maintenance', '["read", "write"]', true, 'd2e3f4a5-b6c7-48d9-e0f1-a2b3c4d5e6f7', NOW(), NOW(), NOW())
ON CONFLICT DO NOTHING;

-- Verify the fix
SELECT
    'Tenant' as type,
    id::text as id,
    name,
    business_model,
    owner_id::text as owner_id
FROM tenants
WHERE id = '3fe07312-b325-4b40-97dd-c8d1c0a67ec7'

UNION ALL

SELECT
    'Member' as type,
    id::text,
    user_id::text as name,
    role as business_model,
    invited_by::text as owner_id
FROM tenant_members
WHERE tenant_id = '3fe07312-b325-4b40-97dd-c8d1c0a67ec7'
ORDER BY type DESC;

COMMIT;
