-- Migration 023: Add ON UPDATE CASCADE to all projects foreign keys
-- Allows project key updates (rename) to cascade to all dependent tables
-- Fixes: CF-518 - task project rename command fails due to FK constraints

-- Fix task_sequences table
ALTER TABLE task_sequences
    DROP CONSTRAINT IF EXISTS task_sequences_project_fkey;

ALTER TABLE task_sequences
    ADD CONSTRAINT task_sequences_project_fkey
    FOREIGN KEY (project) REFERENCES projects(key)
    ON DELETE CASCADE
    ON UPDATE CASCADE;

-- Fix epic_sequences table
ALTER TABLE epic_sequences
    DROP CONSTRAINT IF EXISTS epic_sequences_project_fkey;

ALTER TABLE epic_sequences
    ADD CONSTRAINT epic_sequences_project_fkey
    FOREIGN KEY (project) REFERENCES projects(key)
    ON DELETE CASCADE
    ON UPDATE CASCADE;

-- Fix versions table
ALTER TABLE versions
    DROP CONSTRAINT IF EXISTS versions_project_fkey;

ALTER TABLE versions
    ADD CONSTRAINT versions_project_fkey
    FOREIGN KEY (project) REFERENCES projects(key)
    ON DELETE CASCADE
    ON UPDATE CASCADE;

-- Fix epics table
ALTER TABLE epics
    DROP CONSTRAINT IF EXISTS epics_project_fkey;

ALTER TABLE epics
    ADD CONSTRAINT epics_project_fkey
    FOREIGN KEY (project) REFERENCES projects(key)
    ON DELETE CASCADE
    ON UPDATE CASCADE;

-- Fix tasks table
ALTER TABLE tasks
    DROP CONSTRAINT IF EXISTS tasks_project_fkey;

ALTER TABLE tasks
    ADD CONSTRAINT tasks_project_fkey
    FOREIGN KEY (project) REFERENCES projects(key)
    ON DELETE CASCADE
    ON UPDATE CASCADE;

-- Fix project_locks table (from migration 004)
ALTER TABLE project_locks
    DROP CONSTRAINT IF EXISTS project_locks_project_fkey;

ALTER TABLE project_locks
    ADD CONSTRAINT project_locks_project_fkey
    FOREIGN KEY (project) REFERENCES projects(key)
    ON DELETE CASCADE
    ON UPDATE CASCADE;

-- Fix project_archives table (from migration 009)
ALTER TABLE project_archives
    DROP CONSTRAINT IF EXISTS project_archives_project_key_fkey;

ALTER TABLE project_archives
    ADD CONSTRAINT project_archives_project_key_fkey
    FOREIGN KEY (project_key) REFERENCES projects(key)
    ON DELETE CASCADE
    ON UPDATE CASCADE;

-- Fix sessions table (from migration 010)
-- Note: sessions.project uses ON DELETE SET NULL, so we use ON UPDATE CASCADE
ALTER TABLE sessions
    DROP CONSTRAINT IF EXISTS sessions_project_fkey;

ALTER TABLE sessions
    ADD CONSTRAINT sessions_project_fkey
    FOREIGN KEY (project) REFERENCES projects(key)
    ON DELETE SET NULL
    ON UPDATE CASCADE;

-- Fix session_sequences table (from migration 010)
ALTER TABLE session_sequences
    DROP CONSTRAINT IF EXISTS session_sequences_project_fkey;

ALTER TABLE session_sequences
    ADD CONSTRAINT session_sequences_project_fkey
    FOREIGN KEY (project) REFERENCES projects(key)
    ON DELETE CASCADE
    ON UPDATE CASCADE;

-- Fix project_documentation table (from migration 016)
ALTER TABLE project_documentation
    DROP CONSTRAINT IF EXISTS project_documentation_project_fkey;

ALTER TABLE project_documentation
    ADD CONSTRAINT project_documentation_project_fkey
    FOREIGN KEY (project) REFERENCES projects(key)
    ON DELETE CASCADE
    ON UPDATE CASCADE;

-- Record migration
INSERT INTO schema_migrations (version, applied_at)
VALUES ('023_fix_task_sequences_cascade', NOW())
ON CONFLICT DO NOTHING;
