-- CAF SLA tracking migration
-- Run once per environment

CREATE TABLE IF NOT EXISTS caf_sla_config (
    id                  INT AUTO_INCREMENT PRIMARY KEY,
    company_id          INT NOT NULL DEFAULT 0,
    location_id         INT NOT NULL DEFAULT 0,
    verify_sla_hours    INT NOT NULL DEFAULT 24,   -- SUBMITTED → VERIFIED
    assess_sla_hours    INT NOT NULL DEFAULT 48,   -- VERIFIED → CREDIT ASSESSMENT & RISK REVIEWED
    approve_sla_hours   INT NOT NULL DEFAULT 24,   -- CREDIT ASSESSMENT → APPROVED/REJECTED
    breach_notify_email VARCHAR(255) NOT NULL DEFAULT '',
    updated_by          VARCHAR(60),
    updated_at          DATETIME,
    UNIQUE KEY uq_caf_sla (company_id, location_id)
);

ALTER TABLE caf_master
    ADD COLUMN IF NOT EXISTS sla_due_at   DATETIME     NULL,
    ADD COLUMN IF NOT EXISTS sla_breached TINYINT(1)   NOT NULL DEFAULT 0;
