-- Add prateek@saventh.com to CRM and Corporate (login + app grid access)
-- Run in Supabase SQL Editor if migrations aren't applied

-- CRM: users table
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);

-- Corporate: ensure corp_master exists, then add rep
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),
  '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);
