-- Phase 2.2: Backfill user_role_profiles from existing profile tables
-- Migration: 20260415010002

-- Backfill from photographer_profiles
INSERT INTO user_role_profiles (id, user_id, role_key, display_name, bio, location, status, verification_status, approval_status, rejection_reason, approved_at, created_at, updated_at)
SELECT 
    gen_random_uuid(),
    p.user_id,
    'photographer',
    COALESCE(p.display_name, ''),
    p.bio,
    p.location,
    COALESCE(p.status, 'ACTIVE'),
    CASE WHEN p.status = 'VERIFIED' THEN 'VERIFIED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    CASE WHEN p.status = 'APPROVED' THEN 'APPROVED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    p.rejection_reason,
    p.approved_at,
    p.created_at,
    COALESCE(p.updated_at, NOW())
FROM photographer_profiles p
WHERE NOT EXISTS (
    SELECT 1 FROM user_role_profiles urp WHERE urp.user_id = p.user_id AND urp.role_key = 'photographer'
);

-- Backfill from tutor_profiles
INSERT INTO user_role_profiles (id, user_id, role_key, display_name, bio, location, status, verification_status, approval_status, rejection_reason, approved_at, created_at, updated_at)
SELECT 
    gen_random_uuid(),
    p.user_id,
    'tutor',
    COALESCE(p.display_name, ''),
    p.bio,
    p.location,
    COALESCE(p.status, 'ACTIVE'),
    CASE WHEN p.status = 'VERIFIED' THEN 'VERIFIED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    CASE WHEN p.status = 'APPROVED' THEN 'APPROVED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    p.rejection_reason,
    p.approved_at,
    p.created_at,
    COALESCE(p.updated_at, NOW())
FROM tutor_profiles p
WHERE NOT EXISTS (
    SELECT 1 FROM user_role_profiles urp WHERE urp.user_id = p.user_id AND urp.role_key = 'tutor'
);

-- Backfill from makeup_artist_profiles
INSERT INTO user_role_profiles (id, user_id, role_key, display_name, bio, location, status, verification_status, approval_status, rejection_reason, approved_at, created_at, updated_at)
SELECT 
    gen_random_uuid(),
    p.user_id,
    'makeup_artist',
    COALESCE(p.display_name, ''),
    p.bio,
    p.location,
    COALESCE(p.status, 'ACTIVE'),
    CASE WHEN p.status = 'VERIFIED' THEN 'VERIFIED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    CASE WHEN p.status = 'APPROVED' THEN 'APPROVED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    p.rejection_reason,
    p.approved_at,
    p.created_at,
    COALESCE(p.updated_at, NOW())
FROM makeup_artist_profiles p
WHERE NOT EXISTS (
    SELECT 1 FROM user_role_profiles urp WHERE urp.user_id = p.user_id AND urp.role_key = 'makeup_artist'
);

-- Backfill from developer_profiles
INSERT INTO user_role_profiles (id, user_id, role_key, display_name, bio, location, status, verification_status, approval_status, rejection_reason, approved_at, created_at, updated_at)
SELECT 
    gen_random_uuid(),
    p.user_id,
    'developer',
    COALESCE(p.display_name, ''),
    p.bio,
    p.location,
    COALESCE(p.status, 'ACTIVE'),
    CASE WHEN p.status = 'VERIFIED' THEN 'VERIFIED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    CASE WHEN p.status = 'APPROVED' THEN 'APPROVED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    p.rejection_reason,
    p.approved_at,
    p.created_at,
    COALESCE(p.updated_at, NOW())
FROM developer_profiles p
WHERE NOT EXISTS (
    SELECT 1 FROM user_role_profiles urp WHERE urp.user_id = p.user_id AND urp.role_key = 'developer'
);

-- Backfill from video_editor_profiles
INSERT INTO user_role_profiles (id, user_id, role_key, display_name, bio, location, status, verification_status, approval_status, rejection_reason, approved_at, created_at, updated_at)
SELECT 
    gen_random_uuid(),
    p.user_id,
    'video_editor',
    COALESCE(p.display_name, ''),
    p.bio,
    p.location,
    COALESCE(p.status, 'ACTIVE'),
    CASE WHEN p.status = 'VERIFIED' THEN 'VERIFIED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    CASE WHEN p.status = 'APPROVED' THEN 'APPROVED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    p.rejection_reason,
    p.approved_at,
    p.created_at,
    COALESCE(p.updated_at, NOW())
FROM video_editor_profiles p
WHERE NOT EXISTS (
    SELECT 1 FROM user_role_profiles urp WHERE urp.user_id = p.user_id AND urp.role_key = 'video_editor'
);

-- Backfill from graphic_designer_profiles
INSERT INTO user_role_profiles (id, user_id, role_key, display_name, bio, location, status, verification_status, approval_status, rejection_reason, approved_at, created_at, updated_at)
SELECT 
    gen_random_uuid(),
    p.user_id,
    'graphic_designer',
    COALESCE(p.display_name, ''),
    p.bio,
    p.location,
    COALESCE(p.status, 'ACTIVE'),
    CASE WHEN p.status = 'VERIFIED' THEN 'VERIFIED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    CASE WHEN p.status = 'APPROVED' THEN 'APPROVED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    p.rejection_reason,
    p.approved_at,
    p.created_at,
    COALESCE(p.updated_at, NOW())
FROM graphic_designer_profiles p
WHERE NOT EXISTS (
    SELECT 1 FROM user_role_profiles urp WHERE urp.user_id = p.user_id AND urp.role_key = 'graphic_designer'
);

-- Backfill from social_media_manager_profiles
INSERT INTO user_role_profiles (id, user_id, role_key, display_name, bio, location, status, verification_status, approval_status, rejection_reason, approved_at, created_at, updated_at)
SELECT 
    gen_random_uuid(),
    p.user_id,
    'social_media_manager',
    COALESCE(p.display_name, ''),
    p.bio,
    p.location,
    COALESCE(p.status, 'ACTIVE'),
    CASE WHEN p.status = 'VERIFIED' THEN 'VERIFIED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    CASE WHEN p.status = 'APPROVED' THEN 'APPROVED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    p.rejection_reason,
    p.approved_at,
    p.created_at,
    COALESCE(p.updated_at, NOW())
FROM social_media_manager_profiles p
WHERE NOT EXISTS (
    SELECT 1 FROM user_role_profiles urp WHERE urp.user_id = p.user_id AND urp.role_key = 'social_media_manager'
);

-- Backfill from fitness_trainer_profiles
INSERT INTO user_role_profiles (id, user_id, role_key, display_name, bio, location, status, verification_status, approval_status, rejection_reason, approved_at, created_at, updated_at)
SELECT 
    gen_random_uuid(),
    p.user_id,
    'fitness_trainer',
    COALESCE(p.display_name, ''),
    p.bio,
    p.location,
    COALESCE(p.status, 'ACTIVE'),
    CASE WHEN p.status = 'VERIFIED' THEN 'VERIFIED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    CASE WHEN p.status = 'APPROVED' THEN 'APPROVED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    p.rejection_reason,
    p.approved_at,
    p.created_at,
    COALESCE(p.updated_at, NOW())
FROM fitness_trainer_profiles p
WHERE NOT EXISTS (
    SELECT 1 FROM user_role_profiles urp WHERE urp.user_id = p.user_id AND urp.role_key = 'fitness_trainer'
);

-- Backfill from catering_service_profiles
INSERT INTO user_role_profiles (id, user_id, role_key, display_name, bio, location, status, verification_status, approval_status, rejection_reason, approved_at, created_at, updated_at)
SELECT 
    gen_random_uuid(),
    p.user_id,
    'catering_service',
    COALESCE(p.business_name, ''),
    p.bio,
    p.location,
    COALESCE(p.status, 'ACTIVE'),
    CASE WHEN p.status = 'VERIFIED' THEN 'VERIFIED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    CASE WHEN p.status = 'APPROVED' THEN 'APPROVED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    p.rejection_reason,
    p.approved_at,
    p.created_at,
    COALESCE(p.updated_at, NOW())
FROM catering_service_profiles p
WHERE NOT EXISTS (
    SELECT 1 FROM user_role_profiles urp WHERE urp.user_id = p.user_id AND urp.role_key = 'catering_service'
);

-- Backfill from ugc_content_creator_profiles
INSERT INTO user_role_profiles (id, user_id, role_key, display_name, bio, location, status, verification_status, approval_status, rejection_reason, approved_at, created_at, updated_at)
SELECT 
    gen_random_uuid(),
    p.user_id,
    'ugc_content_creator',
    COALESCE(p.display_name, ''),
    p.bio,
    p.location,
    COALESCE(p.status, 'ACTIVE'),
    CASE WHEN p.status = 'VERIFIED' THEN 'VERIFIED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    CASE WHEN p.status = 'APPROVED' THEN 'APPROVED' WHEN p.status = 'REJECTED' THEN 'REJECTED' ELSE 'PENDING' END,
    p.rejection_reason,
    p.approved_at,
    p.created_at,
    COALESCE(p.updated_at, NOW())
FROM ugc_content_creator_profiles p
WHERE NOT EXISTS (
    SELECT 1 FROM user_role_profiles urp WHERE urp.user_id = p.user_id AND urp.role_key = 'ugc_content_creator'
);

-- Backfill from company_profiles
INSERT INTO user_role_profiles (id, user_id, role_key, display_name, bio, location, status, created_at, updated_at)
SELECT 
    gen_random_uuid(),
    cp.user_id,
    'company',
    cp.company_name,
    cp.bio,
    NULL,
    COALESCE(cp.status, 'ACTIVE'),
    cp.created_at,
    COALESCE(cp.updated_at, NOW())
FROM company_profiles cp
WHERE NOT EXISTS (
    SELECT 1 FROM user_role_profiles urp WHERE urp.user_id = cp.user_id AND urp.role_key = 'company'
);

-- Backfill from customer_profiles
INSERT INTO user_role_profiles (id, user_id, role_key, display_name, location, status, created_at, updated_at)
SELECT 
    gen_random_uuid(),
    cp.user_id,
    'customer',
    COALESCE(cp.full_name, ''),
    cp.city,
    COALESCE(cp.status, 'ACTIVE'),
    cp.created_at,
    COALESCE(cp.updated_at, NOW())
FROM customer_profiles cp
WHERE NOT EXISTS (
    SELECT 1 FROM user_role_profiles urp WHERE urp.user_id = cp.user_id AND urp.role_key = 'customer'
);

-- Backfill from job_seeker_profiles
INSERT INTO user_role_profiles (id, user_id, role_key, display_name, bio, location, status, created_at, updated_at)
SELECT 
    gen_random_uuid(),
    jsp.user_id,
    'candidate',
    COALESCE(jsp.full_name, ''),
    jsp.bio,
    jsp.location,
    COALESCE(jsp.status, 'ACTIVE'),
    jsp.created_at,
    COALESCE(jsp.updated_at, NOW())
FROM job_seeker_profiles jsp
WHERE NOT EXISTS (
    SELECT 1 FROM user_role_profiles urp WHERE urp.user_id = jsp.user_id AND urp.role_key = 'candidate'
);
