-- Seed data for External Role Management Module System
-- Phase 3 seed

-- ============================================
-- SEED: Persona Types
-- ============================================
INSERT INTO persona_types (code, name, description) VALUES
('PROFESSIONAL', 'Professional', 'Freelance professionals offering services'),
('COMPANY', 'Company', 'Business accounts posting jobs'),
('JOB_SEEKER', 'Job Seeker', 'Individuals seeking employment'),
('CUSTOMER', 'Customer', 'Customers seeking services')
ON CONFLICT (code) DO NOTHING;

-- ============================================
-- SEED: Modules (23 total)
-- ============================================

-- Core Shared Modules
INSERT INTO modules (module_key, module_name, category, description, default_route, default_sidebar_label, icon_key, is_core) VALUES
('dashboard_home', 'Dashboard', 'core', 'Dashboard home page with KPIs and widgets', '/dashboard', 'My Dashboard', 'layout-dashboard', true),
('profile', 'Profile', 'core', 'User profile management', '/dashboard/profile', 'My Profile', 'user', true),
('verification', 'Verification', 'core', 'Verification status and documents', '/dashboard/verification', 'Verification', 'shield-check', true),
('help_center', 'Help Center', 'core', 'Help and support', '/dashboard/help', 'Help Center', 'help-circle', true),
('settings', 'Settings', 'core', 'Account settings', '/dashboard/settings', 'Settings', 'settings', true),
('switch_services', 'Switch Services', 'core', 'Switch to different roles', '/dashboard/switch', 'Switch Services', 'repeat', true),
('explore_nxtgauge', 'Explore Nxtgauge', 'core', 'Register for additional roles', '/dashboard/explore', 'Explore Nxtgauge', 'compass', true)
ON CONFLICT (module_key) DO NOTHING;

-- Content and Identity Modules
INSERT INTO modules (module_key, module_name, category, description, default_route, default_sidebar_label, icon_key, is_core) VALUES
('portfolio', 'Portfolio', 'content', 'Portfolio showcase', '/dashboard/portfolio', 'My Portfolio', 'image', false),
('services', 'Services', 'content', 'Services offered', '/dashboard/services', 'My Services', 'briefcase', false)
ON CONFLICT (module_key) DO NOTHING;

-- Marketplace and Discovery Modules
INSERT INTO modules (module_key, module_name, category, description, default_route, default_sidebar_label, icon_key, is_core) VALUES
('marketplace', 'Marketplace', 'marketplace', 'Service marketplace', '/dashboard/marketplace', 'Marketplace', 'store', false),
('browse_jobs', 'Browse Jobs', 'marketplace', 'Browse available jobs', '/dashboard/jobs', 'Browse Jobs', 'search', false),
('saved_jobs', 'Saved Jobs', 'marketplace', 'Saved job listings', '/dashboard/saved-jobs', 'Saved Jobs', 'bookmark', false)
ON CONFLICT (module_key) DO NOTHING;

-- Work and Response Modules
INSERT INTO modules (module_key, module_name, category, description, default_route, default_sidebar_label, icon_key, is_core) VALUES
('jobs', 'Jobs', 'work', 'Job postings management', '/dashboard/jobs', 'Jobs', 'briefcase', false),
('applications', 'Applications', 'work', 'Job applications received', '/dashboard/applications', 'Applications', 'file-text', false),
('my_applications', 'My Applications', 'work', 'My job applications', '/dashboard/my-applications', 'My Applications', 'send', false),
('requirements', 'Requirements', 'work', 'Customer requirements', '/dashboard/requirements', 'Requirements', 'list', false),
('leads', 'Leads', 'work', 'Leads and inquiries', '/dashboard/leads', 'Leads', 'zap', false),
('my_responses', 'My Responses', 'work', 'My responses to requirements', '/dashboard/responses', 'My Responses', 'message-circle', false),
('received_responses', 'Received Responses', 'work', 'Responses to my requirements', '/dashboard/received-responses', 'Received Responses', 'inbox', false),
('shortlisted_candidates', 'Shortlisted Candidates', 'work', 'Shortlisted candidates', '/dashboard/shortlisted', 'Shortlisted Candidates', 'users', false),
('shortlisted_responses', 'Shortlisted Responses', 'work', 'Shortlisted responses', '/dashboard/shortlisted-responses', 'Shortlisted Responses', 'star', false)
ON CONFLICT (module_key) DO NOTHING;

-- Financial Modules
INSERT INTO modules (module_key, module_name, category, description, default_route, default_sidebar_label, icon_key, is_core) VALUES
('wallet', 'Wallet', 'financial', 'Wallet and payments', '/dashboard/wallet', 'Wallet', 'wallet', false),
('credits', 'Credits', 'financial', 'Credits management', '/dashboard/credits', 'Credits', 'credit-card', false)
ON CONFLICT (module_key) DO NOTHING;

-- ============================================
-- SEED: Update external roles with persona_type
-- ============================================
UPDATE roles SET persona_type = 'COMPANY' WHERE key = 'COMPANY';
UPDATE roles SET persona_type = 'JOB_SEEKER' WHERE key = 'JOB_SEEKER';
UPDATE roles SET persona_type = 'CUSTOMER' WHERE key = 'CUSTOMER';
UPDATE roles SET persona_type = 'PROFESSIONAL' WHERE key IN ('PHOTOGRAPHER', 'MAKEUP_ARTIST', 'TUTOR', 'DEVELOPER', 'VIDEO_EDITOR', 'GRAPHIC_DESIGNER', 'SOCIAL_MEDIA_MANAGER', 'FITNESS_TRAINER', 'CATERING_SERVICES');

-- ============================================
-- SEED: Module Actions (generic CRUD + domain)
-- ============================================
DO $$
DECLARE
    mod_record RECORD;
BEGIN
    FOR mod_record IN SELECT id, module_key FROM modules LOOP
        -- Generic CRUD actions
        INSERT INTO module_actions (module_id, action_key, action_name, description)
        VALUES
            (mod_record.id, 'view', 'View', 'View ' || mod_record.module_key)
        ON CONFLICT DO NOTHING;

        INSERT INTO module_actions (module_id, action_key, action_name, description)
        VALUES
            (mod_record.id, 'list', 'List', 'List ' || mod_record.module_key)
        ON CONFLICT DO NOTHING;

        INSERT INTO module_actions (module_id, action_key, action_name, description)
        VALUES
            (mod_record.id, 'create', 'Create', 'Create ' || mod_record.module_key)
        ON CONFLICT DO NOTHING;

        INSERT INTO module_actions (module_id, action_key, action_name, description)
        VALUES
            (mod_record.id, 'update', 'Update', 'Update ' || mod_record.module_key)
        ON CONFLICT DO NOTHING;

        INSERT INTO module_actions (module_id, action_key, action_name, description)
        VALUES
            (mod_record.id, 'delete', 'Delete', 'Delete ' || mod_record.module_key)
        ON CONFLICT DO NOTHING;
    END LOOP;
END $$;

-- Domain-specific actions
INSERT INTO module_actions (module_id, action_key, action_name, description)
SELECT m.id, 'publish', 'Publish', 'Publish content'
FROM modules m WHERE m.module_key IN ('portfolio', 'jobs', 'services')
ON CONFLICT DO NOTHING;

INSERT INTO module_actions (module_id, action_key, action_name, description)
SELECT m.id, 'submit', 'Submit', 'Submit for review'
FROM modules m WHERE m.module_key IN ('verification', 'applications', 'requirements')
ON CONFLICT DO NOTHING;

INSERT INTO module_actions (module_id, action_key, action_name, description)
SELECT m.id, 'shortlist', 'Shortlist', 'Shortlist item'
FROM modules m WHERE m.module_key IN ('shortlisted_candidates', 'shortlisted_responses')
ON CONFLICT DO NOTHING;

INSERT INTO module_actions (module_id, action_key, action_name, description)
SELECT m.id, 'respond', 'Respond', 'Respond to requirement'
FROM modules m WHERE m.module_key IN ('my_responses', 'received_responses')
ON CONFLICT DO NOTHING;

INSERT INTO module_actions (module_id, action_key, action_name, description)
SELECT m.id, 'buy_credits', 'Buy Credits', 'Purchase credits'
FROM modules m WHERE m.module_key = 'credits'
ON CONFLICT DO NOTHING;

INSERT INTO module_actions (module_id, action_key, action_name, description)
SELECT m.id, 'request_payout', 'Request Payout', 'Request wallet payout'
FROM modules m WHERE m.module_key = 'wallet'
ON CONFLICT DO NOTHING;

INSERT INTO module_actions (module_id, action_key, action_name, description)
SELECT m.id, 'switch', 'Switch', 'Switch to this service'
FROM modules m WHERE m.module_key = 'switch_services'
ON CONFLICT DO NOTHING;

INSERT INTO module_actions (module_id, action_key, action_name, description)
SELECT m.id, 'start_onboarding', 'Start Onboarding', 'Start new role onboarding'
FROM modules m WHERE m.module_key = 'explore_nxtgauge'
ON CONFLICT DO NOTHING;

INSERT INTO module_actions (module_id, action_key, action_name, description)
SELECT m.id, 'resubmit', 'Resubmit', 'Resubmit for verification'
FROM modules m WHERE m.module_key = 'verification'
ON CONFLICT DO NOTHING;
