-- ==============================================
-- ComplyCore: Clean Dev Seed with Valid UUIDs
-- ==============================================

-- Clean tables (safe for dev only)
DELETE FROM auth_audit_log;
DELETE FROM evaluations;
DELETE FROM api_clients;
DELETE FROM users;
DELETE FROM tenants;

-- ---------------------
-- TENANTS
-- ---------------------
INSERT INTO tenants (id, name, cage_code, sam_uid, primary_contact_name, primary_contact_email)
VALUES
  ('11111111-1111-1111-1111-111111111111', 'ACME Defense Solutions', '5X9L2', 'JHE92SLMNTY4', 'Jane Smith', 'jane@acme.com'),
  ('22222222-2222-2222-2222-222222222222', 'Orion Cyber Systems', '9X4T1', 'AER74Q9MDK33', 'Kyle Brooks', 'kbrooks@orioncyber.io');

-- ---------------------
-- USERS
-- ---------------------
-- ACME users
INSERT INTO users (id, tenant_id, role, first_name, last_name, email, phone_office, job_title)
VALUES
  ('00000000-0000-0000-0000-000000000001', '11111111-1111-1111-1111-111111111111', 'client_admin', 'Jane', 'Smith', 'jane@acme.com', '555-1000', 'IT Manager'),
  ('00000000-0000-0000-0000-000000000002', '11111111-1111-1111-1111-111111111111', 'client_user', 'Mark', 'Lee', 'mark@acme.com', '555-2001', 'System Analyst');

-- Orion users
INSERT INTO users (id, tenant_id, role, first_name, last_name, email, phone_office, job_title)
VALUES
  ('00000000-0000-0000-0000-000000000003', '22222222-2222-2222-2222-222222222222', 'client_admin', 'Kyle', 'Brooks', 'kbrooks@orioncyber.io', '555-2002', 'Security Lead'),
  ('00000000-0000-0000-0000-000000000004', '22222222-2222-2222-2222-222222222222', 'client_user', 'Tina', 'Ramos', 'tina@orioncyber.io', '555-2003', 'Compliance Analyst');

-- Global users
INSERT INTO users (id, tenant_id, role, first_name, last_name, email, phone_office, job_title)
VALUES
  ('00000000-0000-0000-0000-000000000005', '11111111-1111-1111-1111-111111111111', 'reviewer', 'Emily', 'Nguyen', 'emily@complycore.com', '555-9001', 'CMMC Reviewer'),
  ('00000000-0000-0000-0000-000000000006', '11111111-1111-1111-1111-111111111111', 'superadmin', 'Michael', 'Kell', 'admin@complycore.com', '555-9999', 'Platform Owner');

-- ---------------------
-- SERVICE ACCOUNTS
-- ---------------------
INSERT INTO api_clients (
  id, tenant_id, name, client_id, client_secret, scopes, description, created_by
) VALUES
  ('99999999-1111-1111-1111-111111111111', '11111111-1111-1111-1111-111111111111',
   'ACME Upload Bot', 'acme-bot-1', 'acme-secret-1', ARRAY['upload', 'evaluate']::api_scope[],
   'Auto uploader for ACME', '00000000-0000-0000-0000-000000000001'),

  ('99999999-2222-2222-2222-222222222222', '22222222-2222-2222-2222-222222222222',
   'Orion CI/CD Service', 'orion-bot', 'orion-secret-token', ARRAY['upload', 'evaluate']::api_scope[],
   'Orion automation client', '00000000-0000-0000-0000-000000000003');

-- ---------------------
-- EVALUATIONS
-- ---------------------
-- Predefined UUIDs for project consistency
DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM evaluations WHERE id = '44444444-aaaa-aaaa-aaaa-aaaaaaaaaaaa') THEN
    INSERT INTO evaluations (id, tenant_id, project_id, control_id, status, notes, created_by)
    VALUES
      -- ACME
      ('44444444-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '11111111-1111-1111-1111-111111111111',
       '55555555-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'AC.1.001', 'implemented',
       'Policy approved.', '00000000-0000-0000-0000-000000000002'),

      ('44444444-bbbb-bbbb-bbbb-bbbbbbbbbbbb', '11111111-1111-1111-1111-111111111111',
       '55555555-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'AC.2.007', 'partial',
       'Procedure referenced but not enforced.', '00000000-0000-0000-0000-000000000002'),

      ('44444444-cccc-cccc-cccc-cccccccccccc', '11111111-1111-1111-1111-111111111111',
       '55555555-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'CM.2.063', 'missing',
       'No documented process.', '00000000-0000-0000-0000-000000000002'),

      -- Orion
      ('44444444-dddd-dddd-dddd-dddddddddddd', '22222222-2222-2222-2222-222222222222',
       '55555555-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'IA.1.076', 'implemented',
       'MFA enforced.', '00000000-0000-0000-0000-000000000004'),

      ('44444444-eeee-eeee-eeee-eeeeeeeeeeee', '22222222-2222-2222-2222-222222222222',
       '55555555-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'AU.2.042', 'missing',
       'No audit logs collected.', '00000000-0000-0000-0000-000000000004');
  END IF;
END $$;

-- ---------------------
-- AUDIT LOG
-- ---------------------
INSERT INTO auth_audit_log (
  id, actor_id, tenant_id, action, target_table, target_id, ip_address, user_agent, result
) VALUES
  (gen_random_uuid(), '00000000-0000-0000-0000-000000000001', '11111111-1111-1111-1111-111111111111',
   'login_success', NULL, NULL, '192.168.1.101', 'Chrome', 'success'),

  (gen_random_uuid(), '99999999-1111-1111-1111-111111111111', '11111111-1111-1111-1111-111111111111',
   'upload_evaluation', 'evaluations', '44444444-bbbb-bbbb-bbbb-bbbbbbbbbbbb', '10.0.1.15', 'bot/1.0', 'success'),

  (gen_random_uuid(), '00000000-0000-0000-0000-000000000005', '22222222-2222-2222-2222-222222222222',
   'view_evaluation', 'evaluations', '44444444-eeee-eeee-eeee-eeeeeeeeeeee', '192.168.5.25', 'Firefox', 'success');
