-- =====================================================
-- COMPLETE DATABASE SEED SCRIPT
-- Run this single script to seed all data
-- Safe to re-run (idempotent)
-- Uses SELECT-based FK resolution for safety
-- =====================================================

BEGIN;

-- =====================================================
-- 1. SUBSCRIPTION PLANS (no dependencies)
-- =====================================================
INSERT INTO subscription_plans (id, slug, name, description, price_monthly, price_yearly, features, ad_free, offline_listening, hifi_quality, max_devices, is_active)
VALUES 
  ('a0000000-0000-0000-0000-000000000001', 'free', 'Free', 'Basic access with ads', 0, 0, '["Shuffle play", "Limited skips", "Standard audio quality"]'::jsonb, false, false, false, 1, true),
  ('a0000000-0000-0000-0000-000000000002', 'premium', 'Premium', 'Ad-free music experience', 999, 9990, '["Ad-free listening", "Unlimited skips", "High quality audio", "Offline mode", "2 devices"]'::jsonb, true, true, true, 2, true),
  ('a0000000-0000-0000-0000-000000000003', 'family', 'Family', 'Premium for up to 6 accounts', 1499, 14990, '["Everything in Premium", "Up to 6 accounts", "Family Mix playlist", "6 devices"]'::jsonb, true, true, true, 6, true)
ON CONFLICT (slug) DO UPDATE SET
  name = EXCLUDED.name,
  description = EXCLUDED.description,
  price_monthly = EXCLUDED.price_monthly,
  price_yearly = EXCLUDED.price_yearly,
  features = EXCLUDED.features;

-- =====================================================
-- 2. GENRES (no dependencies)
-- =====================================================
INSERT INTO genres (id, slug, name, description, color, is_featured, track_count)
VALUES
  ('b0000000-0000-0000-0000-000000000001', 'pop', 'Pop', 'Popular music for everyone', '#FF6B9D', true, 0),
  ('b0000000-0000-0000-0000-000000000002', 'hip-hop', 'Hip Hop', 'Beats and rhymes', '#9B59B6', true, 0),
  ('b0000000-0000-0000-0000-000000000003', 'rock', 'Rock', 'Guitar-driven music', '#E74C3C', true, 0),
  ('b0000000-0000-0000-0000-000000000004', 'electronic', 'Electronic', 'Electronic dance music', '#3498DB', true, 0),
  ('b0000000-0000-0000-0000-000000000005', 'rnb', 'R&B', 'Rhythm and blues', '#F39C12', true, 0),
  ('b0000000-0000-0000-0000-000000000006', 'jazz', 'Jazz', 'Smooth jazz vibes', '#1ABC9C', false, 0),
  ('b0000000-0000-0000-0000-000000000007', 'classical', 'Classical', 'Orchestral masterpieces', '#8E44AD', false, 0),
  ('b0000000-0000-0000-0000-000000000008', 'country', 'Country', 'Country and western', '#D35400', false, 0)
ON CONFLICT (slug) DO UPDATE SET
  name = EXCLUDED.name,
  description = EXCLUDED.description,
  color = EXCLUDED.color;

-- =====================================================
-- 3. CATEGORIES (no dependencies)
-- =====================================================
INSERT INTO categories (id, slug, name, description, color, is_visible, sort_order)
VALUES
  ('c0000000-0000-0000-0000-000000000001', 'new-releases', 'New Releases', 'Fresh music just dropped', '#FF6B6B', true, 1),
  ('c0000000-0000-0000-0000-000000000002', 'trending', 'Trending', 'Whats hot right now', '#4ECDC4', true, 2),
  ('c0000000-0000-0000-0000-000000000003', 'top-charts', 'Top Charts', 'Most streamed tracks', '#45B7D1', true, 3),
  ('c0000000-0000-0000-0000-000000000004', 'mood', 'Mood', 'Music for every mood', '#96CEB4', true, 4),
  ('c0000000-0000-0000-0000-000000000005', 'workout', 'Workout', 'Energy for your exercise', '#FFEAA7', true, 5),
  ('c0000000-0000-0000-0000-000000000006', 'chill', 'Chill', 'Relax and unwind', '#DDA0DD', true, 6)
ON CONFLICT (slug) DO UPDATE SET
  name = EXCLUDED.name,
  description = EXCLUDED.description,
  color = EXCLUDED.color;

-- =====================================================
-- 4. SYSTEM SETTINGS (no dependencies)
-- =====================================================
INSERT INTO system_settings (id, key, value, description)
VALUES
  ('d0000000-0000-0000-0000-000000000001', 'site_name', 'MusicApp', 'Application name'),
  ('d0000000-0000-0000-0000-000000000002', 'maintenance_mode', 'false', 'Enable maintenance mode'),
  ('d0000000-0000-0000-0000-000000000003', 'max_upload_size_mb', '50', 'Maximum upload size in MB'),
  ('d0000000-0000-0000-0000-000000000004', 'default_audio_quality', 'high', 'Default streaming quality'),
  ('d0000000-0000-0000-0000-000000000005', 'royalty_rate_per_stream', '0.004', 'Default royalty per stream in USD')
ON CONFLICT (key) DO UPDATE SET
  value = EXCLUDED.value,
  description = EXCLUDED.description;

-- =====================================================
-- 5. ROYALTY RATES (no dependencies)
-- =====================================================
INSERT INTO royalty_rates (id, name, subscription_type, rate_per_stream, effective_from, is_active)
VALUES
  ('e0000000-0000-0000-0000-000000000001', 'Free Tier Rate', 'free', 0.001, '2024-01-01', true),
  ('e0000000-0000-0000-0000-000000000002', 'Premium Rate', 'premium', 0.004, '2024-01-01', true),
  ('e0000000-0000-0000-0000-000000000003', 'Family Rate', 'family', 0.004, '2024-01-01', true)
ON CONFLICT (id) DO NOTHING;

-- =====================================================
-- 6. USERS (no dependencies)
-- =====================================================
INSERT INTO users (id, email, password_hash, name, role, subscription_type, is_active, is_verified, country, language)
VALUES
  ('10000000-0000-0000-0000-000000000001', 'admin@musicapp.com', '$2b$10$rQZ5xK1HqKjHqKjHqKjHqOeY1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1', 'Admin User', 'admin', 'premium', true, true, 'US', 'en'),
  ('10000000-0000-0000-0000-000000000002', 'artist@musicapp.com', '$2b$10$rQZ5xK1HqKjHqKjHqKjHqOeY1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1', 'Demo Artist', 'artist', 'premium', true, true, 'US', 'en'),
  ('10000000-0000-0000-0000-000000000003', 'user@musicapp.com', '$2b$10$rQZ5xK1HqKjHqKjHqKjHqOeY1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1', 'Demo User', 'user', 'free', true, false, 'US', 'en'),
  ('10000000-0000-0000-0000-000000000004', 'luna@musicapp.com', '$2b$10$rQZ5xK1HqKjHqKjHqKjHqOeY1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1', 'Luna Star', 'artist', 'premium', true, true, 'UK', 'en'),
  ('10000000-0000-0000-0000-000000000005', 'beats@musicapp.com', '$2b$10$rQZ5xK1HqKjHqKjHqKjHqOeY1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1Y1', 'Beat Master', 'artist', 'premium', true, true, 'CA', 'en')
ON CONFLICT (email) DO UPDATE SET
  name = EXCLUDED.name,
  role = EXCLUDED.role,
  subscription_type = EXCLUDED.subscription_type,
  is_active = EXCLUDED.is_active,
  is_verified = EXCLUDED.is_verified;

-- =====================================================
-- 7. USER SETTINGS (depends on users)
-- Use INSERT ... SELECT to resolve FK from users table
-- =====================================================
INSERT INTO user_settings (user_id, audio_quality, download_quality, autoplay, crossfade_seconds, normalize_volume, explicit_content, private_session, notifications_enabled, email_notifications)
SELECT u.id, 'high', 'high', true, 0, true, true, false, true, true
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (user_id) DO UPDATE SET audio_quality = EXCLUDED.audio_quality;

INSERT INTO user_settings (user_id, audio_quality, download_quality, autoplay, crossfade_seconds, normalize_volume, explicit_content, private_session, notifications_enabled, email_notifications)
SELECT u.id, 'high', 'high', true, 2, true, true, false, true, true
FROM users u WHERE u.email = 'artist@musicapp.com'
ON CONFLICT (user_id) DO UPDATE SET audio_quality = EXCLUDED.audio_quality;

INSERT INTO user_settings (user_id, audio_quality, download_quality, autoplay, crossfade_seconds, normalize_volume, explicit_content, private_session, notifications_enabled, email_notifications)
SELECT u.id, 'normal', 'normal', true, 0, true, false, false, true, false
FROM users u WHERE u.email = 'user@musicapp.com'
ON CONFLICT (user_id) DO UPDATE SET audio_quality = EXCLUDED.audio_quality;

-- =====================================================
-- 8. ARTISTS (depends on users)
-- Use INSERT ... SELECT to resolve FK from users table
-- =====================================================
INSERT INTO artists (user_id, name, bio, genres, country, is_verified, total_streams, total_followers, monthly_listeners)
SELECT u.id, 'Demo Artist', 'An amazing demo artist showcasing the platform capabilities.', ARRAY['pop', 'electronic'], 'US', true, 1250000, 45000, 12000
FROM users u WHERE u.email = 'artist@musicapp.com'
ON CONFLICT (user_id) DO UPDATE SET name = EXCLUDED.name, bio = EXCLUDED.bio, is_verified = EXCLUDED.is_verified;

INSERT INTO artists (user_id, name, bio, genres, country, is_verified, total_streams, total_followers, monthly_listeners)
SELECT u.id, 'Luna Star', 'Rising pop sensation from London with a unique sound.', ARRAY['pop', 'rnb'], 'UK', true, 890000, 32000, 8500
FROM users u WHERE u.email = 'luna@musicapp.com'
ON CONFLICT (user_id) DO UPDATE SET name = EXCLUDED.name, bio = EXCLUDED.bio, is_verified = EXCLUDED.is_verified;

INSERT INTO artists (user_id, name, bio, genres, country, is_verified, total_streams, total_followers, monthly_listeners)
SELECT u.id, 'Beat Master', 'Producer and DJ creating infectious electronic beats.', ARRAY['electronic', 'hip-hop'], 'CA', false, 456000, 18000, 5200
FROM users u WHERE u.email = 'beats@musicapp.com'
ON CONFLICT (user_id) DO UPDATE SET name = EXCLUDED.name, bio = EXCLUDED.bio, is_verified = EXCLUDED.is_verified;

-- =====================================================
-- 9. ALBUMS (depends on artists)
-- Use CTE to resolve artist FK via user email
-- =====================================================
WITH demo_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'artist@musicapp.com'
)
INSERT INTO albums (id, artist_id, title, release_date, cover_url, total_tracks, duration_ms, is_explicit, status, album_type, label, total_streams)
SELECT 
  '30000000-0000-0000-0000-000000000001',
  demo_artist.id,
  'First Light', '2024-01-15', '/placeholder.svg?height=300&width=300', 10, 2400000, false, 'published', 'album', 'MusicApp Records', 450000
FROM demo_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

WITH demo_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'artist@musicapp.com'
)
INSERT INTO albums (id, artist_id, title, release_date, cover_url, total_tracks, duration_ms, is_explicit, status, album_type, label, total_streams)
SELECT 
  '30000000-0000-0000-0000-000000000002',
  demo_artist.id,
  'Night Vibes', '2024-06-20', '/placeholder.svg?height=300&width=300', 8, 1920000, true, 'published', 'album', 'MusicApp Records', 320000
FROM demo_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

WITH luna_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'luna@musicapp.com'
)
INSERT INTO albums (id, artist_id, title, release_date, cover_url, total_tracks, duration_ms, is_explicit, status, album_type, label, total_streams)
SELECT 
  '30000000-0000-0000-0000-000000000003',
  luna_artist.id,
  'Starlight', '2024-03-10', '/placeholder.svg?height=300&width=300', 12, 2880000, false, 'published', 'album', 'Luna Music', 280000
FROM luna_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

WITH beat_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'beats@musicapp.com'
)
INSERT INTO albums (id, artist_id, title, release_date, cover_url, total_tracks, duration_ms, is_explicit, status, album_type, label, total_streams)
SELECT 
  '30000000-0000-0000-0000-000000000004',
  beat_artist.id,
  'Bass Drop', '2024-05-01', '/placeholder.svg?height=300&width=300', 6, 1440000, true, 'published', 'ep', 'Beat House', 150000
FROM beat_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

-- =====================================================
-- 10. TRACKS (depends on artists and albums)
-- Use CTE to resolve artist FK via user email
-- =====================================================
WITH demo_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'artist@musicapp.com'
)
INSERT INTO tracks (id, artist_id, album_id, title, duration_ms, track_number, is_explicit, status, is_playable, total_streams, cover_url)
SELECT '40000000-0000-0000-0000-000000000001', demo_artist.id, '30000000-0000-0000-0000-000000000001', 'Morning Glory', 234000, 1, false, 'published', true, 125000, '/placeholder.svg?height=300&width=300' FROM demo_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

WITH demo_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'artist@musicapp.com'
)
INSERT INTO tracks (id, artist_id, album_id, title, duration_ms, track_number, is_explicit, status, is_playable, total_streams, cover_url)
SELECT '40000000-0000-0000-0000-000000000002', demo_artist.id, '30000000-0000-0000-0000-000000000001', 'Golden Hour', 198000, 2, false, 'published', true, 98000, '/placeholder.svg?height=300&width=300' FROM demo_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

WITH demo_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'artist@musicapp.com'
)
INSERT INTO tracks (id, artist_id, album_id, title, duration_ms, track_number, is_explicit, status, is_playable, total_streams, cover_url)
SELECT '40000000-0000-0000-0000-000000000003', demo_artist.id, '30000000-0000-0000-0000-000000000001', 'Rise Up', 267000, 3, false, 'published', true, 87000, '/placeholder.svg?height=300&width=300' FROM demo_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

WITH demo_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'artist@musicapp.com'
)
INSERT INTO tracks (id, artist_id, album_id, title, duration_ms, track_number, is_explicit, status, is_playable, total_streams, cover_url)
SELECT '40000000-0000-0000-0000-000000000004', demo_artist.id, '30000000-0000-0000-0000-000000000001', 'New Dawn', 245000, 4, false, 'published', true, 72000, '/placeholder.svg?height=300&width=300' FROM demo_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

WITH demo_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'artist@musicapp.com'
)
INSERT INTO tracks (id, artist_id, album_id, title, duration_ms, track_number, is_explicit, status, is_playable, total_streams, cover_url)
SELECT '40000000-0000-0000-0000-000000000005', demo_artist.id, '30000000-0000-0000-0000-000000000002', 'Midnight Drive', 289000, 1, true, 'published', true, 156000, '/placeholder.svg?height=300&width=300' FROM demo_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

WITH demo_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'artist@musicapp.com'
)
INSERT INTO tracks (id, artist_id, album_id, title, duration_ms, track_number, is_explicit, status, is_playable, total_streams, cover_url)
SELECT '40000000-0000-0000-0000-000000000006', demo_artist.id, '30000000-0000-0000-0000-000000000002', 'Neon Lights', 223000, 2, false, 'published', true, 134000, '/placeholder.svg?height=300&width=300' FROM demo_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

WITH demo_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'artist@musicapp.com'
)
INSERT INTO tracks (id, artist_id, album_id, title, duration_ms, track_number, is_explicit, status, is_playable, total_streams, cover_url)
SELECT '40000000-0000-0000-0000-000000000007', demo_artist.id, '30000000-0000-0000-0000-000000000002', 'After Hours', 256000, 3, true, 'published', true, 112000, '/placeholder.svg?height=300&width=300' FROM demo_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

WITH luna_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'luna@musicapp.com'
)
INSERT INTO tracks (id, artist_id, album_id, title, duration_ms, track_number, is_explicit, status, is_playable, total_streams, cover_url)
SELECT '40000000-0000-0000-0000-000000000008', luna_artist.id, '30000000-0000-0000-0000-000000000003', 'Shooting Star', 212000, 1, false, 'published', true, 89000, '/placeholder.svg?height=300&width=300' FROM luna_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

WITH luna_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'luna@musicapp.com'
)
INSERT INTO tracks (id, artist_id, album_id, title, duration_ms, track_number, is_explicit, status, is_playable, total_streams, cover_url)
SELECT '40000000-0000-0000-0000-000000000009', luna_artist.id, '30000000-0000-0000-0000-000000000003', 'Constellation', 234000, 2, false, 'published', true, 76000, '/placeholder.svg?height=300&width=300' FROM luna_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

WITH luna_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'luna@musicapp.com'
)
INSERT INTO tracks (id, artist_id, album_id, title, duration_ms, track_number, is_explicit, status, is_playable, total_streams, cover_url)
SELECT '40000000-0000-0000-0000-000000000010', luna_artist.id, '30000000-0000-0000-0000-000000000003', 'Galaxy Dance', 267000, 3, false, 'published', true, 68000, '/placeholder.svg?height=300&width=300' FROM luna_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

WITH beat_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'beats@musicapp.com'
)
INSERT INTO tracks (id, artist_id, album_id, title, duration_ms, track_number, is_explicit, status, is_playable, total_streams, cover_url)
SELECT '40000000-0000-0000-0000-000000000011', beat_artist.id, '30000000-0000-0000-0000-000000000004', 'Drop It', 198000, 1, true, 'published', true, 67000, '/placeholder.svg?height=300&width=300' FROM beat_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

WITH beat_artist AS (
  SELECT a.id FROM artists a JOIN users u ON a.user_id = u.id WHERE u.email = 'beats@musicapp.com'
)
INSERT INTO tracks (id, artist_id, album_id, title, duration_ms, track_number, is_explicit, status, is_playable, total_streams, cover_url)
SELECT '40000000-0000-0000-0000-000000000012', beat_artist.id, '30000000-0000-0000-0000-000000000004', 'Electric Feel', 234000, 2, false, 'published', true, 54000, '/placeholder.svg?height=300&width=300' FROM beat_artist
ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, total_streams = EXCLUDED.total_streams;

-- =====================================================
-- 11. PLAYLISTS (depends on users)
-- Use INSERT ... SELECT to resolve FK from users table
-- =====================================================
INSERT INTO playlists (id, user_id, name, description, cover_url, is_public, is_editorial, total_tracks, total_duration_ms, total_followers)
SELECT '50000000-0000-0000-0000-000000000001', u.id, 'Top Hits 2024', 'The biggest songs of 2024', '/placeholder.svg?height=300&width=300', true, true, 5, 1200000, 15000
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, description = EXCLUDED.description;

INSERT INTO playlists (id, user_id, name, description, cover_url, is_public, is_editorial, total_tracks, total_duration_ms, total_followers)
SELECT '50000000-0000-0000-0000-000000000002', u.id, 'Chill Vibes', 'Relax and unwind with these tracks', '/placeholder.svg?height=300&width=300', true, true, 4, 960000, 8500
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, description = EXCLUDED.description;

INSERT INTO playlists (id, user_id, name, description, cover_url, is_public, is_editorial, total_tracks, total_duration_ms, total_followers)
SELECT '50000000-0000-0000-0000-000000000003', u.id, 'Workout Energy', 'Power through your workout', '/placeholder.svg?height=300&width=300', true, true, 3, 720000, 6200
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, description = EXCLUDED.description;

INSERT INTO playlists (id, user_id, name, description, cover_url, is_public, is_editorial, total_tracks, total_duration_ms, total_followers)
SELECT '50000000-0000-0000-0000-000000000004', u.id, 'My Favorites', 'Personal collection of favorite songs', '/placeholder.svg?height=300&width=300', false, false, 4, 900000, 0
FROM users u WHERE u.email = 'user@musicapp.com'
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, description = EXCLUDED.description;

-- =====================================================
-- 12. PLAYLIST TRACKS (depends on playlists and tracks)
-- Use INSERT ... SELECT to resolve FK from users table
-- =====================================================
INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000001', '50000000-0000-0000-0000-000000000001', '40000000-0000-0000-0000-000000000005', 1, u.id
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000002', '50000000-0000-0000-0000-000000000001', '40000000-0000-0000-0000-000000000001', 2, u.id
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000003', '50000000-0000-0000-0000-000000000001', '40000000-0000-0000-0000-000000000008', 3, u.id
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000004', '50000000-0000-0000-0000-000000000001', '40000000-0000-0000-0000-000000000011', 4, u.id
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000005', '50000000-0000-0000-0000-000000000001', '40000000-0000-0000-0000-000000000006', 5, u.id
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000006', '50000000-0000-0000-0000-000000000002', '40000000-0000-0000-0000-000000000002', 1, u.id
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000007', '50000000-0000-0000-0000-000000000002', '40000000-0000-0000-0000-000000000004', 2, u.id
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000008', '50000000-0000-0000-0000-000000000002', '40000000-0000-0000-0000-000000000009', 3, u.id
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000009', '50000000-0000-0000-0000-000000000002', '40000000-0000-0000-0000-000000000010', 4, u.id
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000010', '50000000-0000-0000-0000-000000000003', '40000000-0000-0000-0000-000000000011', 1, u.id
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000011', '50000000-0000-0000-0000-000000000003', '40000000-0000-0000-0000-000000000012', 2, u.id
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000012', '50000000-0000-0000-0000-000000000003', '40000000-0000-0000-0000-000000000005', 3, u.id
FROM users u WHERE u.email = 'admin@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000013', '50000000-0000-0000-0000-000000000004', '40000000-0000-0000-0000-000000000001', 1, u.id
FROM users u WHERE u.email = 'user@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000014', '50000000-0000-0000-0000-000000000004', '40000000-0000-0000-0000-000000000005', 2, u.id
FROM users u WHERE u.email = 'user@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000015', '50000000-0000-0000-0000-000000000004', '40000000-0000-0000-0000-000000000008', 3, u.id
FROM users u WHERE u.email = 'user@musicapp.com'
ON CONFLICT (id) DO NOTHING;

INSERT INTO playlist_tracks (id, playlist_id, track_id, position, added_by)
SELECT '60000000-0000-0000-0000-000000000016', '50000000-0000-0000-0000-000000000004', '40000000-0000-0000-0000-000000000012', 4, u.id
FROM users u WHERE u.email = 'user@musicapp.com'
ON CONFLICT (id) DO NOTHING;

-- =====================================================
-- 13. TRACK GENRES (depends on tracks and genres)
-- Use INSERT ... SELECT to resolve genre_id from slug instead of hardcoded IDs
-- =====================================================
INSERT INTO track_genres (track_id, genre_id)
SELECT '40000000-0000-0000-0000-000000000001', g.id FROM genres g WHERE g.slug = 'pop'
ON CONFLICT DO NOTHING;

INSERT INTO track_genres (track_id, genre_id)
SELECT '40000000-0000-0000-0000-000000000002', g.id FROM genres g WHERE g.slug = 'pop'
ON CONFLICT DO NOTHING;

INSERT INTO track_genres (track_id, genre_id)
SELECT '40000000-0000-0000-0000-000000000005', g.id FROM genres g WHERE g.slug = 'electronic'
ON CONFLICT DO NOTHING;

INSERT INTO track_genres (track_id, genre_id)
SELECT '40000000-0000-0000-0000-000000000006', g.id FROM genres g WHERE g.slug = 'electronic'
ON CONFLICT DO NOTHING;

INSERT INTO track_genres (track_id, genre_id)
SELECT '40000000-0000-0000-0000-000000000008', g.id FROM genres g WHERE g.slug = 'pop'
ON CONFLICT DO NOTHING;

INSERT INTO track_genres (track_id, genre_id)
SELECT '40000000-0000-0000-0000-000000000009', g.id FROM genres g WHERE g.slug = 'rnb'
ON CONFLICT DO NOTHING;

INSERT INTO track_genres (track_id, genre_id)
SELECT '40000000-0000-0000-0000-000000000011', g.id FROM genres g WHERE g.slug = 'electronic'
ON CONFLICT DO NOTHING;

INSERT INTO track_genres (track_id, genre_id)
SELECT '40000000-0000-0000-0000-000000000012', g.id FROM genres g WHERE g.slug = 'electronic'
ON CONFLICT DO NOTHING;

-- =====================================================
-- 14. ALBUM GENRES (depends on albums and genres)
-- Use INSERT ... SELECT to resolve genre_id from slug instead of hardcoded IDs
-- =====================================================
INSERT INTO album_genres (album_id, genre_id)
SELECT '30000000-0000-0000-0000-000000000001', g.id FROM genres g WHERE g.slug = 'pop'
ON CONFLICT DO NOTHING;

INSERT INTO album_genres (album_id, genre_id)
SELECT '30000000-0000-0000-0000-000000000002', g.id FROM genres g WHERE g.slug = 'electronic'
ON CONFLICT DO NOTHING;

INSERT INTO album_genres (album_id, genre_id)
SELECT '30000000-0000-0000-0000-000000000003', g.id FROM genres g WHERE g.slug = 'pop'
ON CONFLICT DO NOTHING;

INSERT INTO album_genres (album_id, genre_id)
SELECT '30000000-0000-0000-0000-000000000003', g.id FROM genres g WHERE g.slug = 'rnb'
ON CONFLICT DO NOTHING;

INSERT INTO album_genres (album_id, genre_id)
SELECT '30000000-0000-0000-0000-000000000004', g.id FROM genres g WHERE g.slug = 'electronic'
ON CONFLICT DO NOTHING;

-- =====================================================
-- 15. LIKED TRACKS (depends on users and tracks)
-- =====================================================
INSERT INTO liked_tracks (user_id, track_id)
SELECT u.id, '40000000-0000-0000-0000-000000000001'
FROM users u WHERE u.email = 'user@musicapp.com'
ON CONFLICT DO NOTHING;

INSERT INTO liked_tracks (user_id, track_id)
SELECT u.id, '40000000-0000-0000-0000-000000000005'
FROM users u WHERE u.email = 'user@musicapp.com'
ON CONFLICT DO NOTHING;

INSERT INTO liked_tracks (user_id, track_id)
SELECT u.id, '40000000-0000-0000-0000-000000000008'
FROM users u WHERE u.email = 'user@musicapp.com'
ON CONFLICT DO NOTHING;

-- =====================================================
-- 16. FOLLOWED ARTISTS (depends on users and artists)
-- NOTE: Run scripts/07-add-followed-artists-table.sql FIRST
-- to create the table if it doesn't exist
-- =====================================================
-- Wrapped in DO block to safely skip if table doesn't exist
DO $$
BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'followed_artists') THEN
    INSERT INTO followed_artists (user_id, artist_id)
    SELECT u.id, a.id
    FROM users u, artists a
    WHERE u.email = 'user@musicapp.com' 
    AND a.user_id = (SELECT id FROM users WHERE email = 'artist@musicapp.com')
    ON CONFLICT DO NOTHING;

    INSERT INTO followed_artists (user_id, artist_id)
    SELECT u.id, a.id
    FROM users u, artists a
    WHERE u.email = 'user@musicapp.com' 
    AND a.user_id = (SELECT id FROM users WHERE email = 'luna@musicapp.com')
    ON CONFLICT DO NOTHING;
  END IF;
END $$;

COMMIT;
