-- Migration: Move dates from registracie to kurzy table

-- Step 1: Add new columns to kurzy table
ALTER TABLE kurzy ADD COLUMN IF NOT EXISTS datum_od DATE;
ALTER TABLE kurzy ADD COLUMN IF NOT EXISTS datum_do DATE;

-- Step 2: Migrate existing data - copy most recent registration dates to each course
UPDATE kurzy k
SET datum_od = r.datum_od, datum_do = r.datum_do
FROM (
    SELECT DISTINCT ON (kurz_id) kurz_id, datum_od, datum_do
    FROM registracie
    WHERE datum_od IS NOT NULL OR datum_do IS NOT NULL
    ORDER BY kurz_id, created_at DESC
) r
WHERE k.id = r.kurz_id;

-- Step 3: Drop columns from registracie table
ALTER TABLE registracie DROP COLUMN IF EXISTS datum_od;
ALTER TABLE registracie DROP COLUMN IF EXISTS datum_do;
ALTER TABLE registracie DROP COLUMN IF EXISTS pocet_ucastnikov;
