-- Migration 009: Simplify status workflow
-- Removes "Resolved" status and adds "Pending" status
-- Keeps only: Open, Pending, In Progress, Closed

-- First, update any existing tickets with "Resolved" status to "Closed"
UPDATE tickets SET status = 'Closed' WHERE status = 'Resolved';

-- Delete all existing transitions with "Resolved"
DELETE FROM status_transitions WHERE from_status = 'Resolved' OR to_status = 'Resolved';

-- Clear all existing transitions to rebuild clean workflow
DELETE FROM status_transitions;

-- Define new simplified workflow with Pending status
-- OPEN transitions
INSERT INTO status_transitions (from_status, to_status, requires_comment, requires_admin) VALUES
    ('Open', 'Pending', FALSE, FALSE),           -- Waiting on external dependency
    ('Open', 'In Progress', FALSE, FALSE),       -- Start work
    ('Open', 'Closed', TRUE, FALSE);             -- Close without work (duplicate, won't fix, etc.)

-- PENDING transitions
INSERT INTO status_transitions (from_status, to_status, requires_comment, requires_admin) VALUES
    ('Pending', 'Open', FALSE, FALSE),           -- Unblock and reopen
    ('Pending', 'In Progress', FALSE, FALSE),    -- Start work while pending
    ('Pending', 'Closed', TRUE, FALSE);          -- Close while pending

-- IN PROGRESS transitions
INSERT INTO status_transitions (from_status, to_status, requires_comment, requires_admin) VALUES
    ('In Progress', 'Open', FALSE, FALSE),       -- Stop work, back to queue
    ('In Progress', 'Pending', FALSE, FALSE),    -- Blocked by external dependency
    ('In Progress', 'Closed', TRUE, FALSE);      -- Complete and close

-- CLOSED transitions
INSERT INTO status_transitions (from_status, to_status, requires_comment, requires_admin) VALUES
    ('Closed', 'Open', TRUE, FALSE),             -- Reopen (requires explanation)
    ('Closed', 'In Progress', FALSE, FALSE);     -- Reopen and start work immediately

-- Verify new transitions
SELECT 'New Status Transitions:' as info;
SELECT from_status, to_status, requires_comment, requires_admin
FROM status_transitions
WHERE is_active = TRUE
ORDER BY from_status, to_status;
