-- Update flyer URLs from example.com to environment-specific URLs
--
-- This script should be run after determining the correct base URL for the environment:
-- - Dev container: https://localhost (NOT 127.0.0.1 - avoids SSL mixed-origin issues)
-- - Test environment: https://flyer-crawler-test.projectium.com
-- - Production: https://flyer-crawler.projectium.com

-- For dev container (run in dev database):
-- Uses 'localhost' instead of '127.0.0.1' to match how users access the site.
-- This avoids ERR_CERT_AUTHORITY_INVALID errors when images are loaded from a
-- different origin than the page.
UPDATE flyers
SET
    image_url = REPLACE(image_url, 'example.com', 'localhost'),
    icon_url = REPLACE(icon_url, 'example.com', 'localhost')
WHERE
    image_url LIKE '%example.com%'
    OR icon_url LIKE '%example.com%';

-- Also fix any existing 127.0.0.1 URLs to use localhost:
UPDATE flyers
SET
    image_url = REPLACE(image_url, '127.0.0.1', 'localhost'),
    icon_url = REPLACE(icon_url, '127.0.0.1', 'localhost')
WHERE
    image_url LIKE '%127.0.0.1%'
    OR icon_url LIKE '%127.0.0.1%';

-- For test environment (run in test database):
-- UPDATE flyers
-- SET
--     image_url = REPLACE(image_url, 'example.com', 'flyer-crawler-test.projectium.com'),
--     icon_url = REPLACE(icon_url, 'example.com', 'flyer-crawler-test.projectium.com')
-- WHERE
--     image_url LIKE '%example.com%'
--     OR icon_url LIKE '%example.com%';

-- For production (run in production database):
-- UPDATE flyers
-- SET
--     image_url = REPLACE(image_url, 'example.com', 'flyer-crawler.projectium.com'),
--     icon_url = REPLACE(icon_url, 'example.com', 'flyer-crawler.projectium.com')
-- WHERE
--     image_url LIKE '%example.com%'
--     OR icon_url LIKE '%example.com%';

-- Verify the changes:
SELECT flyer_id, image_url, icon_url
FROM flyers
WHERE image_url LIKE '%localhost%'
   OR icon_url LIKE '%localhost%'
   OR image_url LIKE '%flyer-crawler%'
   OR icon_url LIKE '%flyer-crawler%';
