-- Seed: Default Role Module Access based on spec Section 7
-- Fixed: removed sort_order reference

-- ============================================
-- PROFESSIONAL roles (PHOTOGRAPHER, MAKEUP_ARTIST, TUTOR, DEVELOPER, VIDEO_EDITOR, GRAPHIC_DESIGNER, SOCIAL_MEDIA_MANAGER, FITNESS_TRAINER, CATERING_SERVICES)
-- Enabled: dashboard_home, profile, portfolio, services, marketplace, leads, my_responses, wallet, credits, verification, help_center, settings, switch_services, explore_nxtgauge
-- ============================================

INSERT INTO role_module_access (role_id, module_id, is_enabled, is_sidebar_visible, sort_order)
SELECT r.id, m.id, true, true, 0
FROM roles r
CROSS JOIN modules m
WHERE r.audience = 'EXTERNAL'
AND r.key IN ('PHOTOGRAPHER', 'MAKEUP_ARTIST', 'TUTOR', 'DEVELOPER', 'VIDEO_EDITOR', 'GRAPHIC_DESIGNER', 'SOCIAL_MEDIA_MANAGER', 'FITNESS_TRAINER', 'CATERING_SERVICES')
AND m.module_key IN ('dashboard_home', 'profile', 'portfolio', 'services', 'marketplace', 'leads', 'my_responses', 'wallet', 'credits', 'verification', 'help_center', 'settings', 'switch_services', 'explore_nxtgauge')
ON CONFLICT DO NOTHING;

-- ============================================
-- COMPANY role
-- Enabled: dashboard_home, profile, jobs, applications, shortlisted_candidates, credits, verification, help_center, settings, switch_services, explore_nxtgauge
-- ============================================

INSERT INTO role_module_access (role_id, module_id, is_enabled, is_sidebar_visible, sort_order)
SELECT r.id, m.id, true, true, 0
FROM roles r
CROSS JOIN modules m
WHERE r.key = 'COMPANY'
AND m.module_key IN ('dashboard_home', 'profile', 'jobs', 'applications', 'shortlisted_candidates', 'credits', 'verification', 'help_center', 'settings', 'switch_services', 'explore_nxtgauge')
ON CONFLICT DO NOTHING;

-- ============================================
-- JOB_SEEKER role
-- Enabled: dashboard_home, profile, portfolio, browse_jobs, my_applications, saved_jobs, verification, help_center, settings, switch_services, explore_nxtgauge
-- ============================================

INSERT INTO role_module_access (role_id, module_id, is_enabled, is_sidebar_visible, sort_order)
SELECT r.id, m.id, true, true, 0
FROM roles r
CROSS JOIN modules m
WHERE r.key = 'JOB_SEEKER'
AND m.module_key IN ('dashboard_home', 'profile', 'portfolio', 'browse_jobs', 'my_applications', 'saved_jobs', 'verification', 'help_center', 'settings', 'switch_services', 'explore_nxtgauge')
ON CONFLICT DO NOTHING;

-- ============================================
-- CUSTOMER role
-- Enabled: dashboard_home, profile, requirements, received_responses, shortlisted_responses, credits, verification, help_center, settings, switch_services, explore_nxtgauge
-- ============================================

INSERT INTO role_module_access (role_id, module_id, is_enabled, is_sidebar_visible, sort_order)
SELECT r.id, m.id, true, true, 0
FROM roles r
CROSS JOIN modules m
WHERE r.key = 'CUSTOMER'
AND m.module_key IN ('dashboard_home', 'profile', 'requirements', 'received_responses', 'shortlisted_responses', 'credits', 'verification', 'help_center', 'settings', 'switch_services', 'explore_nxtgauge')
ON CONFLICT DO NOTHING;

-- ============================================
-- SEED: Default Role Module Permissions (basic CRUD)
-- All external roles get view/list/create/update on their enabled modules
-- ============================================

INSERT INTO role_module_permissions (role_id, module_id, can_view, can_list, can_create, can_update, can_delete)
SELECT rma.role_id, rma.module_id, true, true, true, true, false
FROM role_module_access rma
JOIN roles r ON r.id = rma.role_id
WHERE r.audience = 'EXTERNAL'
ON CONFLICT DO NOTHING;

-- ============================================
-- SEED: Module Variants for profile and portfolio
-- ============================================

-- Profile variants for PROFESSIONAL roles
INSERT INTO module_variants (module_id, variant_key, variant_name, role_code, persona_type, schema_key, ui_template_key)
SELECT m.id, 'profile.' || LOWER(r.key), 'Profile - ' || r.name, r.key, 'PROFESSIONAL', 'profile_' || LOWER(r.key), 'profile_professional'
FROM modules m
CROSS JOIN roles r
WHERE m.module_key = 'profile'
AND r.persona_type = 'PROFESSIONAL'
ON CONFLICT DO NOTHING;

-- Portfolio variants for PROFESSIONAL roles
INSERT INTO module_variants (module_id, variant_key, variant_name, role_code, persona_type, schema_key, ui_template_key)
SELECT m.id, 'portfolio.' || LOWER(r.key), 'Portfolio - ' || r.name, r.key, 'PROFESSIONAL', 'portfolio_' || LOWER(r.key), 'portfolio_professional'
FROM modules m
CROSS JOIN roles r
WHERE m.module_key = 'portfolio'
AND r.persona_type = 'PROFESSIONAL'
ON CONFLICT DO NOTHING;

-- Profile variant for COMPANY
INSERT INTO module_variants (module_id, variant_key, variant_name, role_code, schema_key, ui_template_key)
SELECT m.id, 'profile.company', 'Profile - Company', 'COMPANY', 'profile_company', 'profile_company'
FROM modules m
WHERE m.module_key = 'profile'
ON CONFLICT DO NOTHING;

-- Profile variant for JOB_SEEKER
INSERT INTO module_variants (module_id, variant_key, variant_name, role_code, schema_key, ui_template_key)
SELECT m.id, 'profile.job_seeker', 'Profile - Job Seeker', 'JOB_SEEKER', 'profile_job_seeker', 'profile_job_seeker'
FROM modules m
WHERE m.module_key = 'profile'
ON CONFLICT DO NOTHING;

-- Profile variant for CUSTOMER
INSERT INTO module_variants (module_id, variant_key, variant_name, role_code, schema_key, ui_template_key)
SELECT m.id, 'profile.customer', 'Profile - Customer', 'CUSTOMER', 'profile_customer', 'profile_customer'
FROM modules m
WHERE m.module_key = 'profile'
ON CONFLICT DO NOTHING;

-- ============================================
-- SEED: Role Module Variant Mappings
-- ============================================

INSERT INTO role_module_variant_mapping (role_id, module_id, module_variant_id)
SELECT r.id, mv.module_id, mv.id
FROM module_variants mv
JOIN roles r ON r.key = mv.role_code
ON CONFLICT DO NOTHING;
