-- Fix inconsistent days based on fecha_inicio (Source of Truth)
-- ISODOW returns 1 (Monday) - 7 (Sunday)
-- cat_dias mapping assumed: Lunes(1)..Domingo(7) or similar.
-- Let's use name matching to be safe.

UPDATE clases
SET fk_id_cat_dia = (
    CASE EXTRACT(ISODOW FROM fecha_inicio)
        WHEN 1 THEN (SELECT id_cat_dia FROM cat_dias WHERE nombre = 'Lunes')
        WHEN 2 THEN (SELECT id_cat_dia FROM cat_dias WHERE nombre = 'Martes')
        WHEN 3 THEN (SELECT id_cat_dia FROM cat_dias WHERE nombre = 'Miércoles')
        WHEN 4 THEN (SELECT id_cat_dia FROM cat_dias WHERE nombre = 'Jueves')
        WHEN 5 THEN (SELECT id_cat_dia FROM cat_dias WHERE nombre = 'Viernes')
        WHEN 6 THEN (SELECT id_cat_dia FROM cat_dias WHERE nombre = 'Sábado')
        WHEN 7 THEN (SELECT id_cat_dia FROM cat_dias WHERE nombre = 'Domingo')
    END
)
WHERE fecha_inicio IS NOT NULL;
