CREATE TABLE IF NOT EXISTS wa_fcm_tokens (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    agent_id BIGINT UNSIGNED NOT NULL,
    token_hash CHAR(64) NOT NULL,
    device_token TEXT NOT NULL,
    platform ENUM('ANDROID','IOS') NOT NULL DEFAULT 'ANDROID',
    device_name VARCHAR(150) NULL,
    enabled TINYINT(1) NOT NULL DEFAULT 1,
    last_seen_at DATETIME NULL,
    last_success_at DATETIME NULL,
    last_error TEXT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uq_fcm_token_hash (token_hash),
    KEY idx_fcm_agent_enabled (agent_id, enabled),
    CONSTRAINT fk_fcm_token_agent FOREIGN KEY (agent_id) REFERENCES wa_agents(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
