-- Nxtgauge Seed Script
-- Run: psql $DATABASE_URL -f scripts/seed.sql
-- Safe to re-run (uses INSERT ... ON CONFLICT DO UPDATE for configs)

-- ── 1. Roles ─────────────────────────────────────────────────────────────────

-- Internal roles
INSERT INTO roles (key, name, audience) VALUES
  ('SUPER_ADMIN',           'Super Admin',            'INTERNAL'),
  ('ADMIN',                 'Admin',                  'INTERNAL'),
  ('SUPPORT',               'Support Agent',          'INTERNAL')
ON CONFLICT (key) DO NOTHING;

-- Internal role extensions
INSERT INTO internal_roles (role_id, description)
  SELECT id, 'Full system administrator with all permissions' FROM roles WHERE key = 'SUPER_ADMIN'
  ON CONFLICT (role_id) DO NOTHING;
INSERT INTO internal_roles (role_id, description, can_approve_requests, can_manage_system_settings)
  SELECT id, 'Standard administrator', true, true FROM roles WHERE key = 'ADMIN'
  ON CONFLICT (role_id) DO NOTHING;
INSERT INTO internal_roles (role_id, description)
  SELECT id, 'Customer support agent' FROM roles WHERE key = 'SUPPORT'
  ON CONFLICT (role_id) DO NOTHING;

-- External roles
INSERT INTO roles (key, name, audience) VALUES
  ('COMPANY',               'Company',                'EXTERNAL'),
  ('JOB_SEEKER',            'Job Seeker',             'EXTERNAL'),
  ('CUSTOMER',              'Customer',               'EXTERNAL'),
  ('PHOTOGRAPHER',          'Photographer',           'EXTERNAL'),
  ('MAKEUP_ARTIST',         'Makeup Artist',          'EXTERNAL'),
  ('TUTOR',                 'Tutor',                  'EXTERNAL'),
  ('DEVELOPER',             'Developer',              'EXTERNAL'),
  ('VIDEO_EDITOR',          'Video Editor',           'EXTERNAL'),
  ('GRAPHIC_DESIGNER',      'Graphic Designer',       'EXTERNAL'),
  ('SOCIAL_MEDIA_MANAGER',  'Social Media Manager',   'EXTERNAL'),
  ('FITNESS_TRAINER',       'Fitness Trainer',        'EXTERNAL'),
  ('CATERING_SERVICES',     'Catering Services',      'EXTERNAL')
ON CONFLICT (key) DO NOTHING;

-- External role extensions
INSERT INTO external_roles (role_id)
  SELECT id FROM roles WHERE audience = 'EXTERNAL'
ON CONFLICT (role_id) DO NOTHING;

-- ── 2. Super Admin User ──────────────────────────────────────────────────────
-- Default password: Admin@nxtgauge1 (bcrypt hash)
-- CHANGE THIS PASSWORD IMMEDIATELY AFTER FIRST LOGIN

DO $$
DECLARE
  super_admin_role_id UUID;
  admin_user_id UUID;
BEGIN
  SELECT id INTO super_admin_role_id FROM roles WHERE key = 'SUPER_ADMIN';

  INSERT INTO users (email, password_hash, status, role_id, first_name, last_name, email_verified)
  VALUES (
    'admin@nxtgauge.com',
    '$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TiGniB9GSmJBGp0K7RqUi/4hY/Ii',
    'ACTIVE',
    super_admin_role_id,
    'Super',
    'Admin',
    true
  )
  ON CONFLICT (email) DO NOTHING
  RETURNING id INTO admin_user_id;

  IF admin_user_id IS NOT NULL THEN
    INSERT INTO user_roles (user_id, role_id, status, approved_at)
    VALUES (admin_user_id, super_admin_role_id, 'APPROVED', NOW())
    ON CONFLICT (user_id, role_id) DO NOTHING;
  END IF;
END $$;

-- ── 3. Onboarding Configs ─────────────────────────────────────────────────────
-- Each role gets its own INSERT for clarity and maintainability.
-- Fields use "id" (not "key") to match the frontend field binding.
-- All document uploads accept PDF + images.
-- City fields are readOnly and default to "Chennai, India".
-- version = 2 to force updates on re-run.

-- CUSTOMER (basic + documents; no portfolio)
-- Field ids match src/lib/profile-fields-config.ts's `default` BASIC_FIELDS/DOC_FIELDS
-- (CUSTOMER has no dedicated entry there, so it falls back to `default`).
INSERT INTO onboarding_configs (role_id, schema_json, version, is_active)
SELECT id, $json${
  "portfolioModel": "none",
  "steps": [
    {
      "id": "step_1_basic",
      "title": "Basic Information",
      "type": "basic",
      "fields": [
        {"id": "first_name", "label": "First Name", "type": "text", "required": true, "lockAfterApproval": true},
        {"id": "last_name",  "label": "Last Name",  "type": "text", "required": true, "lockAfterApproval": true},
        {"id": "phone",      "label": "Mobile Number", "type": "tel", "required": true},
        {"id": "gender",     "label": "Gender", "type": "select", "required": false,
          "options": [{"label":"Male","value":"Male"},{"label":"Female","value":"Female"},{"label":"Other","value":"Other"},{"label":"Prefer not to say","value":"Prefer not to say"}]},
        {"id": "location",   "label": "City", "type": "text", "required": true},
        {"id": "state",      "label": "State", "type": "text", "required": true},
        {"id": "pin_code",   "label": "PIN Code", "type": "text", "required": false},
        {"id": "address",    "label": "Address", "type": "textarea", "required": false}
      ]
    },
    {
      "id": "step_2_documents",
      "title": "Documents",
      "type": "documents",
      "fields": [
        {"id": "aadhar_doc", "label": "Aadhar / Government ID", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}
      ]
    },
    {
      "id": "step_3_review",
      "title": "Review and Submit",
      "type": "review",
      "fields": []
    }
  ]
}$json$::jsonb, 2, true
FROM roles WHERE key = 'CUSTOMER'
ON CONFLICT (role_id) WHERE is_active DO UPDATE SET schema_json = EXCLUDED.schema_json, version = EXCLUDED.version;

-- COMPANY (basic + documents; no portfolio)
INSERT INTO onboarding_configs (role_id, schema_json, version, is_active)
SELECT id, $json${
  "portfolioModel": "none",
  "steps": [
    {
      "id": "step_1_basic",
      "title": "Basic Information",
      "type": "basic",
      "fields": [
        {"id": "company_name",  "label": "Company Name", "type": "text", "required": true, "lockAfterApproval": true},
        {"id": "company_email", "label": "Company Email", "type": "email", "required": true},
        {"id": "company_phone", "label": "Company Phone", "type": "tel", "required": false},
        {"id": "website",       "label": "Website URL", "type": "url", "required": false},
        {"id": "location",      "label": "City", "type": "text", "required": true},
        {"id": "state",         "label": "State", "type": "text", "required": true},
        {"id": "pin_code",      "label": "PIN Code", "type": "text", "required": false},
        {"id": "address",       "label": "Registered Address", "type": "textarea", "required": false},
        {"id": "gst_number",    "label": "GST Number (optional)", "type": "text", "required": false, "lockAfterApproval": true}
      ]
    },
    {
      "id": "step_2_documents",
      "title": "Documents",
      "type": "documents",
      "fields": [
        {"id": "registration_doc", "label": "Company Registration Certificate", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"},
        {"id": "gst_doc",          "label": "GST Certificate (optional)", "type": "file", "required": false, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}
      ]
    },
    {
      "id": "step_3_review",
      "title": "Review and Submit",
      "type": "review",
      "fields": []
    }
  ]
}$json$::jsonb, 2, true
FROM roles WHERE key = 'COMPANY'
ON CONFLICT (role_id) WHERE is_active DO UPDATE SET schema_json = EXCLUDED.schema_json, version = EXCLUDED.version;

-- JOB_SEEKER (basic + documents + portfolio, portfolio saved into custom_data.job_seeker_portfolio)
INSERT INTO onboarding_configs (role_id, schema_json, version, is_active)
SELECT id, $json${
  "portfolioModel": "custom_data",
  "steps": [
    {
      "id": "step_1_basic",
      "title": "Basic Information",
      "type": "basic",
      "fields": [
        {"id": "first_name", "label": "First Name", "type": "text", "required": true, "lockAfterApproval": true},
        {"id": "last_name",  "label": "Last Name",  "type": "text", "required": true, "lockAfterApproval": true},
        {"id": "phone",      "label": "Mobile Number", "type": "tel", "required": true},
        {"id": "gender",     "label": "Gender", "type": "select", "required": false,
          "options": [{"label":"Male","value":"Male"},{"label":"Female","value":"Female"},{"label":"Other","value":"Other"},{"label":"Prefer not to say","value":"Prefer not to say"}]},
        {"id": "location",   "label": "City", "type": "text", "required": true},
        {"id": "state",      "label": "State", "type": "text", "required": true},
        {"id": "pin_code",   "label": "PIN Code", "type": "text", "required": false},
        {"id": "address",    "label": "Address", "type": "textarea", "required": false}
      ]
    },
    {
      "id": "step_2_documents",
      "title": "Documents",
      "type": "documents",
      "fields": [
        {"id": "aadhar_doc", "label": "Aadhar / Government ID", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}
      ]
    },
    {
      "id": "step_3_portfolio",
      "title": "My Portfolio",
      "type": "portfolio",
      "fields": [
        {"id": "headline",       "label": "Professional Headline", "type": "text", "required": true},
        {"id": "summary",        "label": "Career Summary", "type": "textarea", "required": true},
        {"id": "education",      "label": "Education", "type": "textarea", "required": true},
        {"id": "workExperience", "label": "Work Experience", "type": "textarea", "required": true},
        {"id": "skills",         "label": "Skills", "type": "textarea", "required": true}
      ]
    },
    {
      "id": "step_4_review",
      "title": "Review and Submit",
      "type": "review",
      "fields": []
    }
  ]
}$json$::jsonb, 2, true
FROM roles WHERE key = 'JOB_SEEKER'
ON CONFLICT (role_id) WHERE is_active DO UPDATE SET schema_json = EXCLUDED.schema_json, version = EXCLUDED.version;
-- PHOTOGRAPHER (basic + documents + portfolio)
INSERT INTO onboarding_configs (role_id, schema_json, version, is_active)
SELECT id, $json${"portfolioModel": "professional", "steps": [{"id": "step_1_basic", "title": "Basic Information", "type": "basic", "fields": [{"id": "first_name", "label": "First Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "last_name", "label": "Last Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "email", "label": "Email Address", "type": "email", "required": true}, {"id": "phone", "label": "Mobile Number", "type": "tel", "required": true}, {"id": "gender", "label": "Gender", "type": "select", "required": false, "options": [{"label": "Male", "value": "Male"}, {"label": "Female", "value": "Female"}, {"label": "Other", "value": "Other"}, {"label": "Prefer not to say", "value": "Prefer not to say"}]}, {"id": "address_line_1", "label": "Address Line 1", "type": "text", "required": true}, {"id": "address_line_2", "label": "Address Line 2 (Optional)", "type": "text", "required": false}, {"id": "city", "label": "City", "type": "text", "required": true}, {"id": "area", "label": "Area", "type": "text", "required": true}, {"id": "state", "label": "State", "type": "text", "required": true}, {"id": "pin_code", "label": "PIN Code", "type": "text", "required": false}]}, {"id": "step_2_documents", "title": "Documents", "type": "documents", "fields": [{"id": "aadhar_doc", "label": "Identity Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "address_proof", "label": "Address Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "portfolio_ownership_proof", "label": "Portfolio Ownership Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}]}, {"id": "step_3_portfolio", "title": "My Portfolio", "type": "portfolio", "fields": [{"id": "about", "label": "About", "type": "textarea", "required": true}, {"id": "services_offered", "label": "Services & Pricing", "type": "textarea", "required": true}, {"id": "experience", "label": "Experience / Tools", "type": "textarea", "required": true}, {"id": "faqs", "label": "FAQs", "type": "textarea", "required": false}]}, {"id": "step_4_review", "title": "Review and Submit", "type": "review", "fields": []}]}$json$::jsonb, 2, true
FROM roles WHERE key = 'PHOTOGRAPHER'
ON CONFLICT (role_id) WHERE is_active DO UPDATE SET schema_json = EXCLUDED.schema_json, version = EXCLUDED.version;

-- MAKEUP_ARTIST (basic + documents + portfolio)
INSERT INTO onboarding_configs (role_id, schema_json, version, is_active)
SELECT id, $json${"portfolioModel": "professional", "steps": [{"id": "step_1_basic", "title": "Basic Information", "type": "basic", "fields": [{"id": "first_name", "label": "First Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "last_name", "label": "Last Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "email", "label": "Email Address", "type": "email", "required": true}, {"id": "phone", "label": "Mobile Number", "type": "tel", "required": true}, {"id": "gender", "label": "Gender", "type": "select", "required": false, "options": [{"label": "Male", "value": "Male"}, {"label": "Female", "value": "Female"}, {"label": "Other", "value": "Other"}, {"label": "Prefer not to say", "value": "Prefer not to say"}]}, {"id": "address_line_1", "label": "Address Line 1", "type": "text", "required": true}, {"id": "address_line_2", "label": "Address Line 2 (Optional)", "type": "text", "required": false}, {"id": "city", "label": "City", "type": "text", "required": true}, {"id": "area", "label": "Area", "type": "text", "required": true}, {"id": "state", "label": "State", "type": "text", "required": true}, {"id": "pin_code", "label": "PIN Code", "type": "text", "required": false}]}, {"id": "step_2_documents", "title": "Documents", "type": "documents", "fields": [{"id": "aadhar_doc", "label": "Identity Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "address_proof", "label": "Address Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "professional_certifications", "label": "Professional Certifications", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}]}, {"id": "step_3_portfolio", "title": "My Portfolio", "type": "portfolio", "fields": [{"id": "about", "label": "About", "type": "textarea", "required": true}, {"id": "services_offered", "label": "Services & Pricing", "type": "textarea", "required": true}, {"id": "experience", "label": "Experience / Tools", "type": "textarea", "required": true}, {"id": "faqs", "label": "FAQs", "type": "textarea", "required": false}]}, {"id": "step_4_review", "title": "Review and Submit", "type": "review", "fields": []}]}$json$::jsonb, 2, true
FROM roles WHERE key = 'MAKEUP_ARTIST'
ON CONFLICT (role_id) WHERE is_active DO UPDATE SET schema_json = EXCLUDED.schema_json, version = EXCLUDED.version;

-- DEVELOPER (basic + documents + portfolio)
INSERT INTO onboarding_configs (role_id, schema_json, version, is_active)
SELECT id, $json${"portfolioModel": "professional", "steps": [{"id": "step_1_basic", "title": "Basic Information", "type": "basic", "fields": [{"id": "first_name", "label": "First Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "last_name", "label": "Last Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "email", "label": "Email Address", "type": "email", "required": true}, {"id": "phone", "label": "Mobile Number", "type": "tel", "required": true}, {"id": "gender", "label": "Gender", "type": "select", "required": false, "options": [{"label": "Male", "value": "Male"}, {"label": "Female", "value": "Female"}, {"label": "Other", "value": "Other"}, {"label": "Prefer not to say", "value": "Prefer not to say"}]}, {"id": "address_line_1", "label": "Address Line 1", "type": "text", "required": true}, {"id": "address_line_2", "label": "Address Line 2 (Optional)", "type": "text", "required": false}, {"id": "city", "label": "City", "type": "text", "required": true}, {"id": "area", "label": "Area", "type": "text", "required": true}, {"id": "state", "label": "State", "type": "text", "required": true}, {"id": "pin_code", "label": "PIN Code", "type": "text", "required": false}]}, {"id": "step_2_documents", "title": "Documents", "type": "documents", "fields": [{"id": "aadhar_doc", "label": "Identity Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "address_proof", "label": "Address Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "tax_document", "label": "Tax Document", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}]}, {"id": "step_3_portfolio", "title": "My Portfolio", "type": "portfolio", "fields": [{"id": "about", "label": "About", "type": "textarea", "required": true}, {"id": "services_offered", "label": "Services & Pricing", "type": "textarea", "required": true}, {"id": "experience", "label": "Experience / Tools", "type": "textarea", "required": true}, {"id": "faqs", "label": "FAQs", "type": "textarea", "required": false}]}, {"id": "step_4_review", "title": "Review and Submit", "type": "review", "fields": []}]}$json$::jsonb, 2, true
FROM roles WHERE key = 'DEVELOPER'
ON CONFLICT (role_id) WHERE is_active DO UPDATE SET schema_json = EXCLUDED.schema_json, version = EXCLUDED.version;

-- VIDEO_EDITOR (basic + documents + portfolio)
INSERT INTO onboarding_configs (role_id, schema_json, version, is_active)
SELECT id, $json${"portfolioModel": "professional", "steps": [{"id": "step_1_basic", "title": "Basic Information", "type": "basic", "fields": [{"id": "first_name", "label": "First Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "last_name", "label": "Last Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "email", "label": "Email Address", "type": "email", "required": true}, {"id": "phone", "label": "Mobile Number", "type": "tel", "required": true}, {"id": "gender", "label": "Gender", "type": "select", "required": false, "options": [{"label": "Male", "value": "Male"}, {"label": "Female", "value": "Female"}, {"label": "Other", "value": "Other"}, {"label": "Prefer not to say", "value": "Prefer not to say"}]}, {"id": "address_line_1", "label": "Address Line 1", "type": "text", "required": true}, {"id": "address_line_2", "label": "Address Line 2 (Optional)", "type": "text", "required": false}, {"id": "city", "label": "City", "type": "text", "required": true}, {"id": "area", "label": "Area", "type": "text", "required": true}, {"id": "state", "label": "State", "type": "text", "required": true}, {"id": "pin_code", "label": "PIN Code", "type": "text", "required": false}]}, {"id": "step_2_documents", "title": "Documents", "type": "documents", "fields": [{"id": "aadhar_doc", "label": "Identity Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "address_proof", "label": "Address Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "tax_document", "label": "Tax Document", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}]}, {"id": "step_3_portfolio", "title": "My Portfolio", "type": "portfolio", "fields": [{"id": "about", "label": "About", "type": "textarea", "required": true}, {"id": "services_offered", "label": "Services & Pricing", "type": "textarea", "required": true}, {"id": "experience", "label": "Experience / Tools", "type": "textarea", "required": true}, {"id": "faqs", "label": "FAQs", "type": "textarea", "required": false}]}, {"id": "step_4_review", "title": "Review and Submit", "type": "review", "fields": []}]}$json$::jsonb, 2, true
FROM roles WHERE key = 'VIDEO_EDITOR'
ON CONFLICT (role_id) WHERE is_active DO UPDATE SET schema_json = EXCLUDED.schema_json, version = EXCLUDED.version;

-- GRAPHIC_DESIGNER (basic + documents + portfolio)
INSERT INTO onboarding_configs (role_id, schema_json, version, is_active)
SELECT id, $json${"portfolioModel": "professional", "steps": [{"id": "step_1_basic", "title": "Basic Information", "type": "basic", "fields": [{"id": "first_name", "label": "First Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "last_name", "label": "Last Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "email", "label": "Email Address", "type": "email", "required": true}, {"id": "phone", "label": "Mobile Number", "type": "tel", "required": true}, {"id": "gender", "label": "Gender", "type": "select", "required": false, "options": [{"label": "Male", "value": "Male"}, {"label": "Female", "value": "Female"}, {"label": "Other", "value": "Other"}, {"label": "Prefer not to say", "value": "Prefer not to say"}]}, {"id": "address_line_1", "label": "Address Line 1", "type": "text", "required": true}, {"id": "address_line_2", "label": "Address Line 2 (Optional)", "type": "text", "required": false}, {"id": "city", "label": "City", "type": "text", "required": true}, {"id": "area", "label": "Area", "type": "text", "required": true}, {"id": "state", "label": "State", "type": "text", "required": true}, {"id": "pin_code", "label": "PIN Code", "type": "text", "required": false}]}, {"id": "step_2_documents", "title": "Documents", "type": "documents", "fields": [{"id": "aadhar_doc", "label": "Identity Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "address_proof", "label": "Address Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "tax_document", "label": "Tax Document", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}]}, {"id": "step_3_portfolio", "title": "My Portfolio", "type": "portfolio", "fields": [{"id": "about", "label": "About", "type": "textarea", "required": true}, {"id": "services_offered", "label": "Services & Pricing", "type": "textarea", "required": true}, {"id": "experience", "label": "Experience / Tools", "type": "textarea", "required": true}, {"id": "faqs", "label": "FAQs", "type": "textarea", "required": false}]}, {"id": "step_4_review", "title": "Review and Submit", "type": "review", "fields": []}]}$json$::jsonb, 2, true
FROM roles WHERE key = 'GRAPHIC_DESIGNER'
ON CONFLICT (role_id) WHERE is_active DO UPDATE SET schema_json = EXCLUDED.schema_json, version = EXCLUDED.version;

-- SOCIAL_MEDIA_MANAGER (basic + documents + portfolio)
INSERT INTO onboarding_configs (role_id, schema_json, version, is_active)
SELECT id, $json${"portfolioModel": "professional", "steps": [{"id": "step_1_basic", "title": "Basic Information", "type": "basic", "fields": [{"id": "first_name", "label": "First Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "last_name", "label": "Last Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "email", "label": "Email Address", "type": "email", "required": true}, {"id": "phone", "label": "Mobile Number", "type": "tel", "required": true}, {"id": "gender", "label": "Gender", "type": "select", "required": false, "options": [{"label": "Male", "value": "Male"}, {"label": "Female", "value": "Female"}, {"label": "Other", "value": "Other"}, {"label": "Prefer not to say", "value": "Prefer not to say"}]}, {"id": "address_line_1", "label": "Address Line 1", "type": "text", "required": true}, {"id": "address_line_2", "label": "Address Line 2 (Optional)", "type": "text", "required": false}, {"id": "city", "label": "City", "type": "text", "required": true}, {"id": "area", "label": "Area", "type": "text", "required": true}, {"id": "state", "label": "State", "type": "text", "required": true}, {"id": "pin_code", "label": "PIN Code", "type": "text", "required": false}]}, {"id": "step_2_documents", "title": "Documents", "type": "documents", "fields": [{"id": "aadhar_doc", "label": "Identity Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "address_proof", "label": "Address Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "tax_document", "label": "Tax Document", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}]}, {"id": "step_3_portfolio", "title": "My Portfolio", "type": "portfolio", "fields": [{"id": "about", "label": "About", "type": "textarea", "required": true}, {"id": "services_offered", "label": "Services & Pricing", "type": "textarea", "required": true}, {"id": "experience", "label": "Experience / Tools", "type": "textarea", "required": true}, {"id": "faqs", "label": "FAQs", "type": "textarea", "required": false}]}, {"id": "step_4_review", "title": "Review and Submit", "type": "review", "fields": []}]}$json$::jsonb, 2, true
FROM roles WHERE key = 'SOCIAL_MEDIA_MANAGER'
ON CONFLICT (role_id) WHERE is_active DO UPDATE SET schema_json = EXCLUDED.schema_json, version = EXCLUDED.version;

-- TUTOR (basic + documents + portfolio)
INSERT INTO onboarding_configs (role_id, schema_json, version, is_active)
SELECT id, $json${"portfolioModel": "professional", "steps": [{"id": "step_1_basic", "title": "Basic Information", "type": "basic", "fields": [{"id": "first_name", "label": "First Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "last_name", "label": "Last Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "phone", "label": "Mobile Number", "type": "tel", "required": true}, {"id": "location", "label": "City", "type": "text", "required": true}, {"id": "state", "label": "State", "type": "text", "required": true}, {"id": "subjects", "label": "Subjects Taught (comma separated)", "type": "text", "required": false}, {"id": "experience_years", "label": "Years of Experience", "type": "number", "required": false}, {"id": "bio", "label": "Short Bio", "type": "textarea", "required": false}]}, {"id": "step_2_documents", "title": "Documents", "type": "documents", "fields": [{"id": "aadhar_doc", "label": "Identity Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "address_proof", "label": "Address Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "qualification_proof", "label": "Qualification Proof", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}]}, {"id": "step_3_portfolio", "title": "My Portfolio", "type": "portfolio", "fields": [{"id": "about", "label": "About", "type": "textarea", "required": true}, {"id": "services_offered", "label": "Services & Pricing", "type": "textarea", "required": true}, {"id": "experience", "label": "Experience / Tools", "type": "textarea", "required": true}, {"id": "faqs", "label": "FAQs", "type": "textarea", "required": false}]}, {"id": "step_4_review", "title": "Review and Submit", "type": "review", "fields": []}]}$json$::jsonb, 2, true
FROM roles WHERE key = 'TUTOR'
ON CONFLICT (role_id) WHERE is_active DO UPDATE SET schema_json = EXCLUDED.schema_json, version = EXCLUDED.version;

-- FITNESS_TRAINER (basic + documents + portfolio)
INSERT INTO onboarding_configs (role_id, schema_json, version, is_active)
SELECT id, $json${"portfolioModel": "professional", "steps": [{"id": "step_1_basic", "title": "Basic Information", "type": "basic", "fields": [{"id": "first_name", "label": "First Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "last_name", "label": "Last Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "phone", "label": "Mobile Number", "type": "tel", "required": true}, {"id": "location", "label": "City", "type": "text", "required": true}, {"id": "state", "label": "State", "type": "text", "required": true}, {"id": "training_type", "label": "Training Type", "type": "select", "required": false, "options": [{"label": "Personal Training", "value": "Personal Training"}, {"label": "Group Fitness", "value": "Group Fitness"}, {"label": "Yoga", "value": "Yoga"}, {"label": "CrossFit", "value": "CrossFit"}, {"label": "Zumba", "value": "Zumba"}, {"label": "Pilates", "value": "Pilates"}, {"label": "Other", "value": "Other"}]}, {"id": "experience_years", "label": "Years of Experience", "type": "number", "required": false}, {"id": "bio", "label": "Short Bio", "type": "textarea", "required": false}]}, {"id": "step_2_documents", "title": "Documents", "type": "documents", "fields": [{"id": "aadhar_doc", "label": "Aadhar / Government ID", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "certification_doc", "label": "Fitness Certification", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}]}, {"id": "step_3_portfolio", "title": "My Portfolio", "type": "portfolio", "fields": [{"id": "about", "label": "About", "type": "textarea", "required": true}, {"id": "services_offered", "label": "Services & Pricing", "type": "textarea", "required": true}, {"id": "experience", "label": "Experience / Tools", "type": "textarea", "required": true}, {"id": "faqs", "label": "FAQs", "type": "textarea", "required": false}]}, {"id": "step_4_review", "title": "Review and Submit", "type": "review", "fields": []}]}$json$::jsonb, 2, true
FROM roles WHERE key = 'FITNESS_TRAINER'
ON CONFLICT (role_id) WHERE is_active DO UPDATE SET schema_json = EXCLUDED.schema_json, version = EXCLUDED.version;

-- CATERING_SERVICES (basic + documents + portfolio)
INSERT INTO onboarding_configs (role_id, schema_json, version, is_active)
SELECT id, $json${"portfolioModel": "professional", "steps": [{"id": "step_1_basic", "title": "Basic Information", "type": "basic", "fields": [{"id": "business_name", "label": "Business Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "owner_name", "label": "Owner Name", "type": "text", "required": true, "lockAfterApproval": true}, {"id": "phone", "label": "Contact Number", "type": "tel", "required": true}, {"id": "location", "label": "City", "type": "text", "required": true}, {"id": "state", "label": "State", "type": "text", "required": true}, {"id": "cuisine_types", "label": "Cuisine Types (comma separated)", "type": "text", "required": false}, {"id": "bio", "label": "About Your Service", "type": "textarea", "required": false}]}, {"id": "step_2_documents", "title": "Documents", "type": "documents", "fields": [{"id": "aadhar_doc", "label": "Aadhar / Government ID", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}, {"id": "fssai_license", "label": "FSSAI License", "type": "file", "required": true, "lockAfterApproval": true, "accept": "application/pdf,image/jpeg,image/jpg,image/png", "maxSizeMB": 10, "helperText": "JPG, PNG or PDF - Max 10MB"}]}, {"id": "step_3_portfolio", "title": "My Portfolio", "type": "portfolio", "fields": [{"id": "about", "label": "About", "type": "textarea", "required": true}, {"id": "services_offered", "label": "Services & Pricing", "type": "textarea", "required": true}, {"id": "experience", "label": "Experience / Tools", "type": "textarea", "required": true}, {"id": "faqs", "label": "FAQs", "type": "textarea", "required": false}]}, {"id": "step_4_review", "title": "Review and Submit", "type": "review", "fields": []}]}$json$::jsonb, 2, true
FROM roles WHERE key = 'CATERING_SERVICES'
ON CONFLICT (role_id) WHERE is_active DO UPDATE SET schema_json = EXCLUDED.schema_json, version = EXCLUDED.version;

-- ── 4. Default Dashboard Configs ─────────────────────────────────────────────

INSERT INTO role_sidebar_configs (role_id, audience, config_json, version, is_active)
SELECT r.id,
  'EXTERNAL',
  jsonb_build_object(
    'nav', CASE r.key
      WHEN 'COMPANY' THEN '[
        {"key": "jobs", "label": "My Jobs", "path": "/dashboard/jobs", "icon": "briefcase"},
        {"key": "applications", "label": "Applications", "path": "/dashboard/applications", "icon": "users"},
        {"key": "profile", "label": "Company Profile", "path": "/dashboard/profile", "icon": "building"}
      ]'::jsonb
      WHEN 'JOB_SEEKER' THEN '[
        {"key": "browse_jobs", "label": "Browse Jobs", "path": "/dashboard/jobs", "icon": "search"},
        {"key": "my_applications", "label": "My Applications", "path": "/dashboard/applications", "icon": "file-text"},
        {"key": "profile", "label": "My Profile", "path": "/dashboard/profile", "icon": "user"}
      ]'::jsonb
      WHEN 'CUSTOMER' THEN '[
        {"key": "requirements", "label": "My Requirements", "path": "/dashboard/requirements", "icon": "list"},
        {"key": "profile", "label": "My Profile", "path": "/dashboard/profile", "icon": "user"}
      ]'::jsonb
      ELSE '[
        {"key": "marketplace", "label": "Marketplace", "path": "/dashboard/marketplace", "icon": "store"},
        {"key": "leads", "label": "My Leads", "path": "/dashboard/leads", "icon": "zap"},
        {"key": "portfolio", "label": "Portfolio", "path": "/dashboard/portfolio", "icon": "image"},
        {"key": "services", "label": "Services", "path": "/dashboard/services", "icon": "list"},
        {"key": "wallet", "label": "Wallet", "path": "/dashboard/wallet", "icon": "wallet"},
        {"key": "profile", "label": "My Profile", "path": "/dashboard/profile", "icon": "user"}
      ]'::jsonb
    END,
    'enabled_modules', CASE r.key
      WHEN 'COMPANY' THEN '["jobs", "applications", "profile"]'::jsonb
      WHEN 'JOB_SEEKER' THEN '["browse_jobs", "my_applications", "profile"]'::jsonb
      WHEN 'CUSTOMER' THEN '["requirements", "profile"]'::jsonb
      ELSE '["marketplace", "leads", "portfolio", "services", "wallet", "profile"]'::jsonb
    END,
    'widgets', CASE r.key
      WHEN 'COMPANY' THEN '["total_jobs", "active_jobs", "pending_jobs", "applications_received", "shortlisted_candidates", "credits"]'::jsonb
      WHEN 'JOB_SEEKER' THEN '["available_jobs", "my_applications", "shortlisted", "saved_jobs", "profile_status", "portfolio"]'::jsonb
      WHEN 'CUSTOMER' THEN '["total_requirements", "open_requirements", "closed_requirements", "responses_received", "shortlisted_responses", "credits"]'::jsonb
      ELSE '["open_leads", "my_requests", "accepted_requests", "tracecoins", "portfolio", "profile_status"]'::jsonb
    END,
    'tabs', CASE r.key
      WHEN 'COMPANY' THEN '["overview"]'::jsonb
      WHEN 'JOB_SEEKER' THEN '["overview"]'::jsonb
      WHEN 'CUSTOMER' THEN '["overview"]'::jsonb
      ELSE '["overview"]'::jsonb
    END
  ),
  1,
  true
FROM roles r
WHERE r.audience = 'EXTERNAL'
ON CONFLICT (role_id, audience) WHERE is_active DO UPDATE SET
  config_json = EXCLUDED.config_json,
  version = role_sidebar_configs.version + 1;

-- ── Done ──────────────────────────────────────────────────────────────────────
SELECT 'Seed completed successfully.' AS status;
