<?php

declare(strict_types=1);

class WhatsAppConfig extends Model
{
    public function get(): array
    {
        $stmt = $this->db->query("SELECT * FROM whatsapp_config ORDER BY id ASC LIMIT 1");
        $row = $stmt->fetch();

        if ($row) {
            return $row;
        }

        return [
            'id' => 0,
            'provider_name' => 'Webhook genérico',
            'webhook_url' => '',
            'auth_token' => '',
            'webhook_token' => '',
            'telefone_remetente' => '',
            'ativo' => 0,
            'auto_notify_new' => 1,
            'auto_notify_status' => 1,
            'auto_notify_sla' => 1,
            'auto_create_from_webhook' => 1,
            'template_nova_ocorrencia' => 'Olá {nome}, sua solicitação foi registrada com o protocolo {protocolo}. Serviço: {servico}. Acompanhe em {link_consulta}',
            'template_status' => 'Olá {nome}, o protocolo {protocolo} foi atualizado para {status}. Serviço: {servico}.',
            'template_sla' => 'Alerta: o protocolo {protocolo} está {sla_label}. Serviço: {servico}.',
            'updated_em' => null,
        ];
    }

    public function save(array $data): int
    {
        $current = $this->get();

        if (!empty($current['id'])) {
            $sql = "UPDATE whatsapp_config SET
                        provider_name = :provider_name,
                        webhook_url = :webhook_url,
                        auth_token = :auth_token,
                        webhook_token = :webhook_token,
                        telefone_remetente = :telefone_remetente,
                        ativo = :ativo,
                        auto_notify_new = :auto_notify_new,
                        auto_notify_status = :auto_notify_status,
                        auto_notify_sla = :auto_notify_sla,
                        auto_create_from_webhook = :auto_create_from_webhook,
                        template_nova_ocorrencia = :template_nova_ocorrencia,
                        template_status = :template_status,
                        template_sla = :template_sla,
                        updated_em = NOW()
                    WHERE id = :id";
            $stmt = $this->db->prepare($sql);
            $stmt->execute([
                'id' => (int) $current['id'],
                'provider_name' => $data['provider_name'],
                'webhook_url' => $data['webhook_url'],
                'auth_token' => $data['auth_token'],
                'webhook_token' => $data['webhook_token'],
                'telefone_remetente' => $data['telefone_remetente'],
                'ativo' => (int) $data['ativo'],
                'auto_notify_new' => (int) $data['auto_notify_new'],
                'auto_notify_status' => (int) $data['auto_notify_status'],
                'auto_notify_sla' => (int) $data['auto_notify_sla'],
                'auto_create_from_webhook' => (int) $data['auto_create_from_webhook'],
                'template_nova_ocorrencia' => $data['template_nova_ocorrencia'],
                'template_status' => $data['template_status'],
                'template_sla' => $data['template_sla'],
            ]);
            return (int) $current['id'];
        }

        $sql = "INSERT INTO whatsapp_config (
                    provider_name, webhook_url, auth_token, webhook_token, telefone_remetente, ativo,
                    auto_notify_new, auto_notify_status, auto_notify_sla, auto_create_from_webhook,
                    template_nova_ocorrencia, template_status, template_sla, updated_em
                ) VALUES (
                    :provider_name, :webhook_url, :auth_token, :webhook_token, :telefone_remetente, :ativo,
                    :auto_notify_new, :auto_notify_status, :auto_notify_sla, :auto_create_from_webhook,
                    :template_nova_ocorrencia, :template_status, :template_sla, NOW()
                )";
        $stmt = $this->db->prepare($sql);
        $stmt->execute([
            'provider_name' => $data['provider_name'],
            'webhook_url' => $data['webhook_url'],
            'auth_token' => $data['auth_token'],
            'webhook_token' => $data['webhook_token'],
            'telefone_remetente' => $data['telefone_remetente'],
            'ativo' => (int) $data['ativo'],
            'auto_notify_new' => (int) $data['auto_notify_new'],
            'auto_notify_status' => (int) $data['auto_notify_status'],
            'auto_notify_sla' => (int) $data['auto_notify_sla'],
            'auto_create_from_webhook' => (int) $data['auto_create_from_webhook'],
            'template_nova_ocorrencia' => $data['template_nova_ocorrencia'],
            'template_status' => $data['template_status'],
            'template_sla' => $data['template_sla'],
        ]);

        return (int) $this->db->lastInsertId();
    }
}
