-- Migration: Add generic_task_id column to time_entries
-- Date: 2025-12-20
-- Description: Adds generic_task_id column to time_entries table to link time entries with tasks
--              Replaces the old task_id column with generic_task_id for consistency

-- Drop old task_id column if it exists
DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM information_schema.columns 
               WHERE table_name = 'time_entries' AND column_name = 'task_id') THEN
        ALTER TABLE time_entries DROP COLUMN task_id;
    END IF;
END $$;

-- Add generic_task_id column
ALTER TABLE time_entries 
    ADD COLUMN IF NOT EXISTS generic_task_id UUID DEFAULT NULL REFERENCES generic_tasks(task_id) ON DELETE SET NULL;

-- Create index for performance
CREATE INDEX IF NOT EXISTS idx_time_entries_generic_task ON time_entries (generic_task_id);

-- Add comment
COMMENT ON COLUMN time_entries.generic_task_id IS 'Reference to generic_tasks table - links time entry to a task';
