-- migrations/001_migrate_ids_to_uuid_up.sql
-- Migration UP script for standardizing IDs to uuid.UUID

-- Enable uuid-ossp extension if not already enabled
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- Phase 1: Add new UUID primary key columns and populate them for existing data

-- HLSTranscodeQueue
ALTER TABLE hls_transcode_queue ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE hls_transcode_queue SET new_id = uuid_generate_v4() WHERE new_id IS NULL; -- Ensure all rows have UUID

-- BitrateAdaptationLog
ALTER TABLE bitrate_adaptation_logs ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE bitrate_adaptation_logs SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- Contest
ALTER TABLE contests ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE contests SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- ContestEntry
ALTER TABLE contest_entries ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE contest_entries SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- ContestJudge
ALTER TABLE contest_judges ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE contest_judges SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- ContestVote
ALTER TABLE contest_votes ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE contest_votes SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- ContestSponsor
ALTER TABLE contest_sponsors ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE contest_sponsors SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- ContestStems
ALTER TABLE contest_stems ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE contest_stems SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- ContestAnalytics
ALTER TABLE contest_analytics ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE contest_analytics SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- ContestBadge
ALTER TABLE contest_badges ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE contest_badges SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- FederatedIdentity
ALTER TABLE federated_identities ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE federated_identities SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- Equipment
ALTER TABLE equipment ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE equipment SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- HardwareSale
ALTER TABLE hardware_sales ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE hardware_sales SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- EquipmentTrade
ALTER TABLE equipment_trades ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE equipment_trades SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- HardwareOffer
ALTER TABLE hardware_offers ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE hardware_offers SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- MFAConfig
ALTER TABLE mfa_configs ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE mfa_configs SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- PlaybackAnalytics
ALTER TABLE playback_analytics ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE playback_analytics SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- RecoveryCode
ALTER TABLE recovery_codes ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE recovery_codes SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- RefreshToken
ALTER TABLE refresh_tokens ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE refresh_tokens SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- Role
ALTER TABLE roles ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE roles SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- Permission
ALTER TABLE permissions ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE permissions SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- UserRole
ALTER TABLE user_roles ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE user_roles SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- RoyaltyRecord
ALTER TABLE royalty_records ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE royalty_records SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- RoyaltyPayout
ALTER TABLE royalty_payouts ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE royalty_payouts SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- RoyaltyRate
ALTER TABLE royalty_rates ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE royalty_rates SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- CreatorRoyaltyRate
ALTER TABLE creator_royalty_rates ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE creator_royalty_rates SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- RoyaltyConfig
ALTER TABLE royalty_config ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE royalty_config SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- SellableContent
ALTER TABLE sellable_contents ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE sellable_contents SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- JuryMember
ALTER TABLE jury_members ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE jury_members SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- TrackHistory
ALTER TABLE track_history ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE track_history SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- TrackVersion
ALTER TABLE track_versions ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE track_versions SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- UserSettings
ALTER TABLE user_settings ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE user_settings SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- UserProfile
ALTER TABLE user_profiles ADD COLUMN new_id UUID DEFAULT uuid_generate_v4();
UPDATE user_profiles SET new_id = uuid_generate_v4() WHERE new_id IS NULL;

-- RolePermission (composite primary key)
-- These don't get a single 'id' column, but their components will be migrated later.
-- This step only adds new_id to tables with a single primary key.
