-- ============================================================
-- Supabase SQL: Remove Duplicate Courses
-- ============================================================
-- Logic:
--   1. Group courses by EXACT title
--   2. Within each group, KEEP the row with the oldest created_at
--   3. DELETE all other duplicates
--
-- Run this in Supabase SQL Editor.
-- ============================================================

-- STEP 1: Preview duplicates (DRY RUN — won't delete anything)
-- Uncomment and run this first to see what will be removed:

-- SELECT id, title, created_at
-- FROM courses
-- WHERE id NOT IN (
--     SELECT DISTINCT ON (title) id
--     FROM courses
--     ORDER BY title, created_at ASC
-- )
-- ORDER BY title, created_at;

-- STEP 2: Delete duplicates (KEEP oldest record per title)

DELETE FROM courses
WHERE id NOT IN (
    SELECT DISTINCT ON (title) id
    FROM courses
    ORDER BY title, created_at ASC
);

-- STEP 3: Verify — should return exactly 26 unique courses
SELECT count(*) AS total_courses FROM courses;
SELECT id, title, created_at FROM courses ORDER BY created_at ASC;
