-- Migration 008: Add status workflow management
-- Creates status_transitions table for workflow validation

-- Table to define allowed status transitions
CREATE TABLE status_transitions (
    transition_id INT AUTO_INCREMENT PRIMARY KEY,
    from_status VARCHAR(50) NOT NULL,
    to_status VARCHAR(50) NOT NULL,
    requires_comment BOOLEAN DEFAULT FALSE,
    requires_admin BOOLEAN DEFAULT FALSE,
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY unique_transition (from_status, to_status),
    INDEX idx_from_status (from_status)
);

-- Insert default transitions
INSERT INTO status_transitions (from_status, to_status, requires_comment) VALUES
    ('Open', 'In Progress', FALSE),
    ('Open', 'Closed', TRUE),
    ('In Progress', 'Open', FALSE),
    ('In Progress', 'Closed', TRUE),
    ('Closed', 'Open', TRUE),
    ('Closed', 'In Progress', FALSE);

-- Add new status "Resolved"
INSERT INTO status_transitions (from_status, to_status, requires_comment) VALUES
    ('In Progress', 'Resolved', FALSE),
    ('Resolved', 'Closed', FALSE),
    ('Resolved', 'In Progress', TRUE),
    ('Open', 'Resolved', FALSE);
