-- Full access users: CRM (users) + Corporate (corp_representatives)
-- All 4 products (CRM, Invest, Learn, Corporate) accessible when logged in
-- Learn and Investor: always shown to authenticated; CRM and Corporate require DB entries

-- CRM: users table
INSERT INTO users (role, full_name, email)
SELECT ur.id, 'Tarique Khan', 'tarique@creatoactive.com' FROM user_roles ur WHERE ur.name = 'Admin' LIMIT 1
ON CONFLICT (email) DO UPDATE SET full_name = EXCLUDED.full_name, role = (SELECT id FROM user_roles WHERE name = 'Admin' LIMIT 1);
INSERT INTO users (role, full_name, email)
SELECT ur.id, 'Tarique Khan', 'tarique@saventh.com' FROM user_roles ur WHERE ur.name = 'Admin' LIMIT 1
ON CONFLICT (email) DO UPDATE SET full_name = EXCLUDED.full_name, role = (SELECT id FROM user_roles WHERE name = 'Admin' LIMIT 1);
INSERT INTO users (role, full_name, email)
SELECT ur.id, 'Kuldeep', 'kuldeep@saventh.com' FROM user_roles ur WHERE ur.name = 'Admin' LIMIT 1
ON CONFLICT (email) DO UPDATE SET full_name = EXCLUDED.full_name, role = (SELECT id FROM user_roles WHERE name = 'Admin' LIMIT 1);
INSERT INTO users (role, full_name, email)
SELECT ur.id, 'Vishal', 'vishal@saventh.com' FROM user_roles ur WHERE ur.name = 'Admin' LIMIT 1
ON CONFLICT (email) DO UPDATE SET full_name = EXCLUDED.full_name, role = (SELECT id FROM user_roles WHERE name = 'Admin' LIMIT 1);
INSERT INTO users (role, full_name, email)
SELECT ur.id, 'Prateek', 'prateek@saventh.com' FROM user_roles ur WHERE ur.name = 'Admin' LIMIT 1
ON CONFLICT (email) DO UPDATE SET full_name = EXCLUDED.full_name, role = (SELECT id FROM user_roles WHERE name = 'Admin' LIMIT 1);
INSERT INTO users (role, full_name, email)
SELECT ur.id, 'Prateek', 'prateeek@saventh.com' FROM user_roles ur WHERE ur.name = 'Admin' LIMIT 1
ON CONFLICT (email) DO UPDATE SET full_name = EXCLUDED.full_name, role = (SELECT id FROM user_roles WHERE name = 'Admin' LIMIT 1);
INSERT INTO users (role, full_name, email)
SELECT ur.id, 'Mujahid', 'mujahid@creatoactive.com' FROM user_roles ur WHERE ur.name = 'Admin' LIMIT 1
ON CONFLICT (email) DO UPDATE SET full_name = EXCLUDED.full_name, role = (SELECT id FROM user_roles WHERE name = 'Admin' LIMIT 1);
INSERT INTO users (role, full_name, email)
SELECT ur.id, 'Zeeshan', 'zeeshan@creatoactive.com' FROM user_roles ur WHERE ur.name = 'Admin' LIMIT 1
ON CONFLICT (email) DO UPDATE SET full_name = EXCLUDED.full_name, role = (SELECT id FROM user_roles WHERE name = 'Admin' LIMIT 1);

-- Corporate: corp_master (if missing)
INSERT INTO corp_master (corporate_name, corporate_pan, status)
SELECT 'Saventh Internal', 'SAAAA0000A', 'Approved'
WHERE NOT EXISTS (SELECT 1 FROM corp_master LIMIT 1);

-- Corporate: corp_representatives
INSERT INTO corp_representatives (corp_id, name, email, role)
SELECT (SELECT id FROM corp_master ORDER BY created_at LIMIT 1), 'Tarique Khan', 'tarique@creatoactive.com', 'representative'
WHERE NOT EXISTS (SELECT 1 FROM corp_representatives WHERE email = 'tarique@creatoactive.com') AND EXISTS (SELECT 1 FROM corp_master LIMIT 1);
INSERT INTO corp_representatives (corp_id, name, email, role)
SELECT (SELECT id FROM corp_master ORDER BY created_at LIMIT 1), 'Tarique Khan', 'tarique@saventh.com', 'representative'
WHERE NOT EXISTS (SELECT 1 FROM corp_representatives WHERE email = 'tarique@saventh.com') AND EXISTS (SELECT 1 FROM corp_master LIMIT 1);
INSERT INTO corp_representatives (corp_id, name, email, role)
SELECT (SELECT id FROM corp_master ORDER BY created_at LIMIT 1), 'Kuldeep', 'kuldeep@saventh.com', 'representative'
WHERE NOT EXISTS (SELECT 1 FROM corp_representatives WHERE email = 'kuldeep@saventh.com') AND EXISTS (SELECT 1 FROM corp_master LIMIT 1);
INSERT INTO corp_representatives (corp_id, name, email, role)
SELECT (SELECT id FROM corp_master ORDER BY created_at LIMIT 1), 'Vishal', 'vishal@saventh.com', 'representative'
WHERE NOT EXISTS (SELECT 1 FROM corp_representatives WHERE email = 'vishal@saventh.com') AND EXISTS (SELECT 1 FROM corp_master LIMIT 1);
INSERT INTO corp_representatives (corp_id, name, email, role)
SELECT (SELECT id FROM corp_master ORDER BY created_at LIMIT 1), 'Prateek', 'prateek@saventh.com', 'representative'
WHERE NOT EXISTS (SELECT 1 FROM corp_representatives WHERE email = 'prateek@saventh.com') AND EXISTS (SELECT 1 FROM corp_master LIMIT 1);
INSERT INTO corp_representatives (corp_id, name, email, role)
SELECT (SELECT id FROM corp_master ORDER BY created_at LIMIT 1), 'Prateek', 'prateeek@saventh.com', 'representative'
WHERE NOT EXISTS (SELECT 1 FROM corp_representatives WHERE email = 'prateeek@saventh.com') AND EXISTS (SELECT 1 FROM corp_master LIMIT 1);
INSERT INTO corp_representatives (corp_id, name, email, role)
SELECT (SELECT id FROM corp_master ORDER BY created_at LIMIT 1), 'Mujahid', 'mujahid@creatoactive.com', 'representative'
WHERE NOT EXISTS (SELECT 1 FROM corp_representatives WHERE email = 'mujahid@creatoactive.com') AND EXISTS (SELECT 1 FROM corp_master LIMIT 1);
INSERT INTO corp_representatives (corp_id, name, email, role)
SELECT (SELECT id FROM corp_master ORDER BY created_at LIMIT 1), 'Zeeshan', 'zeeshan@creatoactive.com', 'representative'
WHERE NOT EXISTS (SELECT 1 FROM corp_representatives WHERE email = 'zeeshan@creatoactive.com') AND EXISTS (SELECT 1 FROM corp_master LIMIT 1);
