-- Add 'cancelled' to task_status enum (if it exists) and add cancellation_reason column
DO $$
BEGIN
  -- Add enum value if task_status enum exists
  IF EXISTS (SELECT 1 FROM pg_type WHERE typname = 'task_status') THEN
    IF NOT EXISTS (
      SELECT 1
      FROM pg_enum
      JOIN pg_type ON pg_enum.enumtypid = pg_type.oid
      WHERE pg_type.typname = 'task_status' AND pg_enum.enumlabel = 'cancelled'
    ) THEN
      ALTER TYPE task_status ADD VALUE 'cancelled';
    END IF;
  END IF;

  -- Add cancellation_reason column if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_name='tasks' AND column_name='cancellation_reason'
  ) THEN
    ALTER TABLE public.tasks ADD COLUMN cancellation_reason text;
  END IF;
END
$$;
