-- Migration: Add mailbox statuses table
CREATE TABLE IF NOT EXISTS mailbox_statuses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    color VARCHAR(20) DEFAULT '#000000',
    is_default BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Insert initial statuses if they don't exist
INSERT IGNORE INTO mailbox_statuses (id, name, color, is_default) VALUES 
(1, 'received', '#6c757d', TRUE),
(2, 'in_progress', '#0d6efd', FALSE),
(3, 'closed', '#198754', FALSE);

-- Add status_id to mailbox
ALTER TABLE mailbox ADD COLUMN IF NOT EXISTS status_id INT;

-- Update status_id based on existing ENUM values
UPDATE mailbox SET status_id = 1 WHERE status = 'received' AND status_id IS NULL;
UPDATE mailbox SET status_id = 2 WHERE status = 'in_progress' AND status_id IS NULL;
UPDATE mailbox SET status_id = 3 WHERE status = 'closed' AND status_id IS NULL;

-- Set default status_id for any NULLs
UPDATE mailbox SET status_id = 1 WHERE status_id IS NULL;
