-- Repair database URLs to use custom domains
-- This handles various URL formats and ensures consistency

-- 1. Fix image URLs that are just filenames (no domain)
UPDATE posts 
SET image_url = 'https://img.sojorn.net/' || image_url 
WHERE image_url IS NOT NULL 
  AND image_url != '' 
  AND image_url NOT LIKE 'http%'
  AND (image_url LIKE '%.jpg' OR image_url LIKE '%.jpeg' OR image_url LIKE '%.png' OR image_url LIKE '%.gif');

-- 2. Fix video URLs that are just filenames (no domain)
UPDATE posts 
SET video_url = 'https://quips.sojorn.net/' || video_url 
WHERE video_url IS NOT NULL 
  AND video_url != '' 
  AND video_url NOT LIKE 'http%'
  AND (video_url LIKE '%.mp4' OR video_url LIKE '%.mov' OR video_url LIKE '%.webm');

-- 3. Fix thumbnail URLs that are just filenames
UPDATE posts 
SET thumbnail_url = 'https://img.sojorn.net/' || thumbnail_url 
WHERE thumbnail_url IS NOT NULL 
  AND thumbnail_url != '' 
  AND thumbnail_url NOT LIKE 'http%'
  AND (thumbnail_url LIKE '%.jpg' OR thumbnail_url LIKE '%.jpeg' OR thumbnail_url LIKE '%.png');

-- 4. Fix profile avatars that are just filenames
UPDATE profiles 
SET avatar_url = 'https://img.sojorn.net/' || avatar_url 
WHERE avatar_url IS NOT NULL 
  AND avatar_url != '' 
  AND avatar_url NOT LIKE 'http%'
  AND (avatar_url LIKE '%.jpg' OR avatar_url LIKE '%.jpeg' OR avatar_url LIKE '%.png');

-- 5. Fix profile covers that are just filenames
UPDATE profiles 
SET cover_url = 'https://img.sojorn.net/' || cover_url 
WHERE cover_url IS NOT NULL 
  AND cover_url != '' 
  AND cover_url NOT LIKE 'http%'
  AND (cover_url LIKE '%.jpg' OR cover_url LIKE '%.jpeg' OR cover_url LIKE '%.png');

-- Verification queries
SELECT 'Posts with image URLs' as check_type, count(*) as count FROM posts WHERE image_url IS NOT NULL;
SELECT 'Posts with video URLs' as check_type, count(*) as count FROM posts WHERE video_url IS NOT NULL;
SELECT 'Posts with thumbnail URLs' as check_type, count(*) as count FROM posts WHERE thumbnail_url IS NOT NULL;
SELECT 'Profiles with avatars' as check_type, count(*) as count FROM profiles WHERE avatar_url IS NOT NULL;
SELECT 'Profiles with covers' as check_type, count(*) as count FROM profiles WHERE cover_url IS NOT NULL;

-- Show any remaining non-standard URLs
SELECT 'Non-standard image URLs' as type, image_url FROM posts 
WHERE image_url IS NOT NULL 
  AND image_url NOT LIKE 'https://img.sojorn.net/%' 
  AND image_url NOT LIKE 'https://quips.sojorn.net/%'
LIMIT 5;

SELECT 'Non-standard video URLs' as type, video_url FROM posts 
WHERE video_url IS NOT NULL 
  AND video_url NOT LIKE 'https://img.sojorn.net/%' 
  AND video_url NOT LIKE 'https://quips.sojorn.net/%'
LIMIT 5;
