-- Add tarique@creatoactive.com to CRM and Corporate for full grid access (CRM, Invest, Learn, Corporate)
-- Run this in the Supabase SQL Editor (same project used by Portal/CRM/Corporate)

-- 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);

-- Corporate: corp_representatives (requires corp_master to exist)
INSERT INTO corp_master (corporate_name, corporate_pan, status)
SELECT 'Saventh Internal', 'SAAAA0000A', 'Approved'
WHERE NOT 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@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);
