-- Seed widgets into existing role_sidebar_configs for all EXTERNAL roles.
-- Run this SQL directly against your database:
--   psql $DATABASE_URL -f seeds/seed_widgets.sql
--
-- Or run individual sections below.

-- ── PHOTOGRAPHER ───────────────────────────────────────────────────────────────
UPDATE role_sidebar_configs
SET config_json = jsonb_set(
    COALESCE(config_json, '{}'::jsonb),
    '{widgets}',
    '["open_leads", "my_requests", "accepted_requests", "tracecoins", "portfolio", "profile_status"]'::jsonb,
    true
)
WHERE audience = 'EXTERNAL'
  AND is_active = true
  AND EXISTS (SELECT 1 FROM roles r WHERE r.id = role_sidebar_configs.role_id AND r.key = 'PHOTOGRAPHER');

-- ── MAKEUP ARTIST ──────────────────────────────────────────────────────────────
UPDATE role_sidebar_configs
SET config_json = jsonb_set(
    COALESCE(config_json, '{}'::jsonb),
    '{widgets}',
    '["open_leads", "my_requests", "accepted_requests", "tracecoins", "portfolio", "profile_status"]'::jsonb,
    true
)
WHERE audience = 'EXTERNAL'
  AND is_active = true
  AND EXISTS (SELECT 1 FROM roles r WHERE r.id = role_sidebar_configs.role_id AND r.key = 'MAKEUP_ARTIST');

-- ── TUTOR ─────────────────────────────────────────────────────────────────────
UPDATE role_sidebar_configs
SET config_json = jsonb_set(
    COALESCE(config_json, '{}'::jsonb),
    '{widgets}',
    '["open_leads", "my_requests", "accepted_requests", "tracecoins", "portfolio", "profile_status"]'::jsonb,
    true
)
WHERE audience = 'EXTERNAL'
  AND is_active = true
  AND EXISTS (SELECT 1 FROM roles r WHERE r.id = role_sidebar_configs.role_id AND r.key = 'TUTOR');

-- ── DEVELOPER ─────────────────────────────────────────────────────────────────
UPDATE role_sidebar_configs
SET config_json = jsonb_set(
    COALESCE(config_json, '{}'::jsonb),
    '{widgets}',
    '["open_leads", "my_requests", "accepted_requests", "tracecoins", "portfolio", "profile_status"]'::jsonb,
    true
)
WHERE audience = 'EXTERNAL'
  AND is_active = true
  AND EXISTS (SELECT 1 FROM roles r WHERE r.id = role_sidebar_configs.role_id AND r.key = 'DEVELOPER');

-- ── VIDEO EDITOR ──────────────────────────────────────────────────────────────
UPDATE role_sidebar_configs
SET config_json = jsonb_set(
    COALESCE(config_json, '{}'::jsonb),
    '{widgets}',
    '["open_leads", "my_requests", "accepted_requests", "tracecoins", "portfolio", "profile_status"]'::jsonb,
    true
)
WHERE audience = 'EXTERNAL'
  AND is_active = true
  AND EXISTS (SELECT 1 FROM roles r WHERE r.id = role_sidebar_configs.role_id AND r.key = 'VIDEO_EDITOR');

-- ── GRAPHIC DESIGNER ─────────────────────────────────────────────────────────
UPDATE role_sidebar_configs
SET config_json = jsonb_set(
    COALESCE(config_json, '{}'::jsonb),
    '{widgets}',
    '["open_leads", "my_requests", "accepted_requests", "tracecoins", "portfolio", "profile_status"]'::jsonb,
    true
)
WHERE audience = 'EXTERNAL'
  AND is_active = true
  AND EXISTS (SELECT 1 FROM roles r WHERE r.id = role_sidebar_configs.role_id AND r.key = 'GRAPHIC_DESIGNER');

-- ── SOCIAL MEDIA MANAGER ─────────────────────────────────────────────────────
UPDATE role_sidebar_configs
SET config_json = jsonb_set(
    COALESCE(config_json, '{}'::jsonb),
    '{widgets}',
    '["open_leads", "my_requests", "accepted_requests", "tracecoins", "portfolio", "profile_status"]'::jsonb,
    true
)
WHERE audience = 'EXTERNAL'
  AND is_active = true
  AND EXISTS (SELECT 1 FROM roles r WHERE r.id = role_sidebar_configs.role_id AND r.key = 'SOCIAL_MEDIA_MANAGER');

-- ── FITNESS TRAINER ──────────────────────────────────────────────────────────
UPDATE role_sidebar_configs
SET config_json = jsonb_set(
    COALESCE(config_json, '{}'::jsonb),
    '{widgets}',
    '["open_leads", "my_requests", "accepted_requests", "tracecoins", "portfolio", "profile_status"]'::jsonb,
    true
)
WHERE audience = 'EXTERNAL'
  AND is_active = true
  AND EXISTS (SELECT 1 FROM roles r WHERE r.id = role_sidebar_configs.role_id AND r.key = 'FITNESS_TRAINER');

-- ── CATERING SERVICES ─────────────────────────────────────────────────────────
UPDATE role_sidebar_configs
SET config_json = jsonb_set(
    COALESCE(config_json, '{}'::jsonb),
    '{widgets}',
    '["open_leads", "my_requests", "accepted_requests", "tracecoins", "portfolio", "profile_status"]'::jsonb,
    true
)
WHERE audience = 'EXTERNAL'
  AND is_active = true
  AND EXISTS (SELECT 1 FROM roles r WHERE r.id = role_sidebar_configs.role_id AND r.key = 'CATERING_SERVICES');

-- ── UGC CONTENT CREATOR ──────────────────────────────────────────────────────
UPDATE role_sidebar_configs
SET config_json = jsonb_set(
    COALESCE(config_json, '{}'::jsonb),
    '{widgets}',
    '["open_leads", "my_requests", "accepted_requests", "tracecoins", "portfolio", "profile_status"]'::jsonb,
    true
)
WHERE audience = 'EXTERNAL'
  AND is_active = true
  AND EXISTS (SELECT 1 FROM roles r WHERE r.id = role_sidebar_configs.role_id AND r.key = 'UGC_CONTENT_CREATOR');

-- ── COMPANY ────────────────────────────────────────────────────────────────────
UPDATE role_sidebar_configs
SET config_json = jsonb_set(
    COALESCE(config_json, '{}'::jsonb),
    '{widgets}',
    '["total_jobs", "active_jobs", "pending_jobs", "applications_received", "shortlisted_candidates", "credits"]'::jsonb,
    true
)
WHERE audience = 'EXTERNAL'
  AND is_active = true
  AND EXISTS (SELECT 1 FROM roles r WHERE r.id = role_sidebar_configs.role_id AND r.key = 'COMPANY');

-- ── JOB SEEKER ────────────────────────────────────────────────────────────────
UPDATE role_sidebar_configs
SET config_json = jsonb_set(
    COALESCE(config_json, '{}'::jsonb),
    '{widgets}',
    '["available_jobs", "my_applications", "shortlisted", "saved_jobs", "profile_status", "portfolio"]'::jsonb,
    true
)
WHERE audience = 'EXTERNAL'
  AND is_active = true
  AND EXISTS (SELECT 1 FROM roles r WHERE r.id = role_sidebar_configs.role_id AND r.key = 'JOB_SEEKER');

-- ── CUSTOMER ─────────────────────────────────────────────────────────────────
UPDATE role_sidebar_configs
SET config_json = jsonb_set(
    COALESCE(config_json, '{}'::jsonb),
    '{widgets}',
    '["total_requirements", "open_requirements", "closed_requirements", "responses_received", "shortlisted_responses", "credits"]'::jsonb,
    true
)
WHERE audience = 'EXTERNAL'
  AND is_active = true
  AND EXISTS (SELECT 1 FROM roles r WHERE r.id = role_sidebar_configs.role_id AND r.key = 'CUSTOMER');

-- Verify the updates
SELECT r.key AS role, sc.config_json->'widgets' AS widgets
FROM role_sidebar_configs sc
JOIN roles r ON r.id = sc.role_id
WHERE sc.audience = 'EXTERNAL' AND sc.is_active = true;
