-- Migration 013 — Agent config fields + ventana WhatsApp + estado needs_owner
-- Aplica sobre migraciones 000–012.

-- Extiende vale_config con todos los campos del agente
ALTER TABLE vale_config
    ADD COLUMN knowledge TEXT NULL AFTER hours_json,
    ADD COLUMN responder_en_idioma_del_cliente TINYINT(1) NOT NULL DEFAULT 1 AFTER knowledge,
    ADD COLUMN reglas_escalado TEXT NULL AFTER responder_en_idioma_del_cliente,
    ADD COLUMN booking_activo TINYINT(1) NOT NULL DEFAULT 0 AFTER reglas_escalado,
    ADD COLUMN duracion_slot INT UNSIGNED NOT NULL DEFAULT 30 AFTER booking_activo,
    ADD COLUMN calendar_id VARCHAR(255) NULL AFTER duracion_slot;

-- Ventana de 24h de WhatsApp: cuando vence, solo se puede responder con plantilla
ALTER TABLE conversations
    ADD COLUMN window_expires_at DATETIME NULL AFTER handled_by;

-- Nuevo estado: el agente escaló la conv y el dueño debe retomar manualmente
ALTER TABLE conversations
    MODIFY COLUMN status ENUM('open','needs_owner','booked','lost','closed') NOT NULL DEFAULT 'open';
