<?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' => 'WayChat',
            'integration_mode' => 'waychat',
            'webhook_url' => '',
            'auth_token' => '',
            'company_auth_token' => '',
            'webhook_token' => '',
            'waychat_base_url' => 'https://waychat.app.br',
            'waychat_connection_type' => 'cloud',
            'waychat_send_mode' => 'message',
            'waychat_template_name' => '',
            'waychat_template_language' => 'pt_BR',
            'waychat_create_deal' => 0,
            'waychat_sector_name' => 'Prefeitura',
            'waychat_column_name' => 'Nova Ocorrência',
            'waychat_status_on_send' => 'waiting',
            '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}, recebemos sua solicitação.\n\nProtocolo: {protocolo}\nServiço: {servico}\nAcompanhe: {link_consulta}",
            'template_status' => "Olá {nome}, sua solicitação {protocolo} foi atualizada para {status}.\n\nServiço: {servico}\nAcompanhe: {link_consulta}",
            'template_sla' => "Olá {nome}, sua solicitação {protocolo} segue em acompanhamento.\nSituação de SLA: {sla_label}.\nServiço: {servico}.",
            'updated_em' => null,
        ];
    }

    public function save(array $data): int
    {
        $current = $this->get();
        $payload = [
            'provider_name' => $data['provider_name'],
            'integration_mode' => $data['integration_mode'],
            'webhook_url' => $data['webhook_url'],
            'auth_token' => $data['auth_token'],
            'company_auth_token' => $data['company_auth_token'],
            'webhook_token' => $data['webhook_token'],
            'waychat_base_url' => $data['waychat_base_url'],
            'waychat_connection_type' => $data['waychat_connection_type'],
            'waychat_send_mode' => $data['waychat_send_mode'],
            'waychat_template_name' => $data['waychat_template_name'],
            'waychat_template_language' => $data['waychat_template_language'],
            'waychat_create_deal' => (int) $data['waychat_create_deal'],
            'waychat_sector_name' => $data['waychat_sector_name'],
            'waychat_column_name' => $data['waychat_column_name'],
            'waychat_status_on_send' => $data['waychat_status_on_send'],
            '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'],
        ];

        if (!empty($current['id'])) {
            $sql = "UPDATE whatsapp_config SET
                        provider_name = :provider_name,
                        integration_mode = :integration_mode,
                        webhook_url = :webhook_url,
                        auth_token = :auth_token,
                        company_auth_token = :company_auth_token,
                        webhook_token = :webhook_token,
                        waychat_base_url = :waychat_base_url,
                        waychat_connection_type = :waychat_connection_type,
                        waychat_send_mode = :waychat_send_mode,
                        waychat_template_name = :waychat_template_name,
                        waychat_template_language = :waychat_template_language,
                        waychat_create_deal = :waychat_create_deal,
                        waychat_sector_name = :waychat_sector_name,
                        waychat_column_name = :waychat_column_name,
                        waychat_status_on_send = :waychat_status_on_send,
                        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);
            $payload['id'] = (int) $current['id'];
            $stmt->execute($payload);
            return (int) $current['id'];
        }

        $sql = "INSERT INTO whatsapp_config (
                    provider_name, integration_mode, webhook_url, auth_token, company_auth_token, webhook_token,
                    waychat_base_url, waychat_connection_type, waychat_send_mode, waychat_template_name, waychat_template_language,
                    waychat_create_deal, waychat_sector_name, waychat_column_name, waychat_status_on_send,
                    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, :integration_mode, :webhook_url, :auth_token, :company_auth_token, :webhook_token,
                    :waychat_base_url, :waychat_connection_type, :waychat_send_mode, :waychat_template_name, :waychat_template_language,
                    :waychat_create_deal, :waychat_sector_name, :waychat_column_name, :waychat_status_on_send,
                    :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($payload);

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