-- Add categorization column
ALTER TABLE feeds ADD COLUMN categorization JSON NOT NULL DEFAULT '[]';

-- Add CHECK constraint
CREATE TRIGGER validate_feed_categorization
AFTER INSERT ON feeds
BEGIN
    SELECT CASE 
        WHEN NOT (
            json_valid(NEW.categorization) 
            AND json_type(NEW.categorization) = 'array'
            AND (
                SELECT COUNT(*) 
                FROM json_each(NEW.categorization) 
                WHERE json_type(value) != 'text'
            ) = 0
        )
        THEN RAISE(ROLLBACK, 'categorization must be an array of strings')
    END;
END;

CREATE TRIGGER validate_feed_categorization_update
AFTER UPDATE OF categorization ON feeds
BEGIN
    SELECT CASE 
        WHEN NOT (
            json_valid(NEW.categorization) 
            AND json_type(NEW.categorization) = 'array'
            AND (
                SELECT COUNT(*) 
                FROM json_each(NEW.categorization) 
                WHERE json_type(value) != 'text'
            ) = 0
        )
        THEN RAISE(ROLLBACK, 'categorization must be an array of strings')
    END;
END; 
