-- PostgreSQL Path Fix for Windows
-- This script converts Linux paths stored in the database to Windows paths
-- Run this if you migrated from Linux or have incorrect paths in your database
\pset pager off

-- IMPORTANT: Backup your database before running this script!

-- Step 1: Check what paths exist in your database
SELECT 'ImageInfo Paths' AS table_name, COUNT(*) AS count, 
       COUNT(CASE WHEN "Path" LIKE '/var/lib/jellyfin/%' THEN 1 END) AS linux_paths,
       COUNT(CASE WHEN "Path" LIKE '/var/%' OR "Path" LIKE '/usr/%' THEN 1 END) AS all_linux_paths
FROM library."ImageInfos"
UNION ALL
SELECT 'BaseItems Paths' AS table_name, COUNT(*) AS count,
       COUNT(CASE WHEN "Path" LIKE '/var/lib/jellyfin/%' THEN 1 END) AS linux_paths,
       COUNT(CASE WHEN "Path" LIKE '/var/%' OR "Path" LIKE '/usr/%' THEN 1 END) AS all_linux_paths
FROM library."BaseItems";

-- Step 2: Show sample paths that will be updated
(SELECT '"ImageInfos"' AS table_name, "Id"::text, "Path" AS current_path
FROM library."ImageInfos"
WHERE "Path" LIKE '/var/lib/jellyfin/%' OR "Path" LIKE '/var/%' OR "Path" LIKE '/usr/%'
LIMIT 10)
UNION ALL
(SELECT '"BaseItems"' AS table_name, "Id"::text, "Path" AS current_path
FROM library."BaseItems"
WHERE "Path" LIKE '/var/lib/jellyfin/%' OR "Path" LIKE '/var/%' OR "Path" LIKE '/usr/%'
LIMIT 10);

-- Step 3: Preview the path conversion (does NOT modify data)
-- This shows you what the new paths will look like
SELECT 
    "Id",
    "Path" AS old_path,
    REGEXP_REPLACE(
        REGEXP_REPLACE(
            "Path",
            '^/var/lib/jellyfin',
            'C:/ProgramData/jellyfin/data',
            'g'
        ),
        '/',
        '\',
        'g'
    ) AS new_path
FROM library."ImageInfos"
WHERE "Path" LIKE '/var/lib/jellyfin/%'
LIMIT 5;

-- ===========================================================================
-- STOP HERE AND REVIEW THE OUTPUT ABOVE BEFORE PROCEEDING!
-- ===========================================================================
-- If the preview looks correct, uncomment and run the UPDATE statements below

/*
-- Step 4: Update ImageInfo paths
-- Convert: /var/lib/jellyfin -> C:/ProgramData/jellyfin/data
-- Convert: / -> \
BEGIN;

UPDATE library."ImageInfos"
SET "Path" = REGEXP_REPLACE(
    REGEXP_REPLACE(
        "Path",
        '^/var/lib/jellyfin',
        'C:/ProgramData/jellyfin/data',
        'g'
    ),
    '/',
    '\',
    'g'
)
WHERE "Path" LIKE '/var/lib/jellyfin/%';

-- Show how many rows were updated
SELECT 'Updated ImageInfos: ' || ROW_COUNT() AS result;

COMMIT;

-- Step 5: Update BaseItems paths (if needed)
BEGIN;

UPDATE library."BaseItems"
SET "Path" = REGEXP_REPLACE(
    REGEXP_REPLACE(
        "Path",
        '^/var/lib/jellyfin',
        'C:/ProgramData/jellyfin/data',
        'g'
    ),
    '/',
    '\',
    'g'
)
WHERE "Path" LIKE '/var/lib/jellyfin/%';

SELECT 'Updated BaseItems: ' || ROW_COUNT() AS result;

COMMIT;

-- Step 6: Update any other Linux paths (e.g., /usr/share/jellyfin)
BEGIN;

UPDATE library."BaseItems"
SET "Path" = REGEXP_REPLACE(
    REGEXP_REPLACE(
        "Path",
        '^/usr/share/jellyfin',
        'C:/Program Files/Jellyfin',
        'g'
    ),
    '/',
    '\',
    'g'
)
WHERE "Path" LIKE '/usr/share/jellyfin/%';

SELECT 'Updated BaseItems (usr/share): ' || ROW_COUNT() AS result;

COMMIT;

-- Step 7: Verify the changes
SELECT 'Verification - ImageInfos' AS check_type, 
       COUNT(*) AS total_rows,
       COUNT(CASE WHEN "Path" LIKE '/var/%' OR "Path" LIKE '/usr/%' THEN 1 END) AS remaining_linux_paths
FROM library."ImageInfos"
UNION ALL
SELECT 'Verification - BaseItems' AS check_type,
       COUNT(*) AS total_rows,
       COUNT(CASE WHEN "Path" LIKE '/var/%' OR "Path" LIKE '/usr/%' THEN 1 END) AS remaining_linux_paths
FROM library."BaseItems";

*/

-- ===========================================================================
-- IMPORTANT NOTES:
-- ===========================================================================
-- 1. This script assumes your Windows Jellyfin data is at C:/ProgramData/jellyfin/data
--    If it's different, update the path in the REGEXP_REPLACE statements
-- 
-- 2. The script converts forward slashes (/) to backslashes (\)
--    However, .NET's Path class accepts forward slashes on Windows too
--
-- 3. You may need to restart Jellyfin after running this script
--
-- 4. If you have custom metadata paths, you'll need to adjust the script accordingly
