<?php
// include/emails-cadastros/candidato.php

if (!defined('BASE_PATH')) {
    define('BASE_PATH', realpath(__DIR__ . '/../../')); 
}

require_once BASE_PATH . '/PHPMailer-master/src/Exception.php';
require_once BASE_PATH . '/PHPMailer-master/src/PHPMailer.php';
require_once BASE_PATH . '/PHPMailer-master/src/SMTP.php';
require_once BASE_PATH . '/env.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception as PHPMailerException;
use PHPMailer\PHPMailer\SMTP;

function configurarMailerCandidato() {
    $mail = new PHPMailer(true);
    try {
        $mail->isSMTP();
        $mail->Host       = $_ENV['SMTP_HOST'] ?? '';
        $mail->SMTPAuth   = filter_var(($_ENV['SMTP_AUTH'] ?? true), FILTER_VALIDATE_BOOLEAN);
        $mail->Username   = $_ENV['SMTP_USERNAME'] ?? '';
        $mail->Password   = $_ENV['SMTP_PASSWORD'] ?? '';
        $smtp_secure_env  = strtolower($_ENV['SMTP_SECURE'] ?? 'ssl');
        $mail->SMTPSecure = ($smtp_secure_env === 'tls') ? PHPMailer::ENCRYPTION_STARTTLS : PHPMailer::ENCRYPTION_SMTPS;
        $mail->Port       = (int)($_ENV['SMTP_PORT'] ?? 465);
        $mail->CharSet    = 'UTF-8';
        $fromName = str_replace(['"', "'"], '', ($_ENV['SMTP_FROM_NAME'] ?? 'Selflix Vagas'));
        $mail->setFrom($_ENV['SMTP_FROM_EMAIL'] ?? '', $fromName);
        $mail->SMTPDebug = SMTP::DEBUG_OFF; 
        return $mail;
    } catch (PHPMailerException $e) {
        return null;
    }
}

function processarTemplateCandidato($pdo, $slug, $dados) {
    $stmt = $pdo->prepare("SELECT assunto, corpo FROM EmailTemplates WHERE slug = :slug");
    $stmt->execute([':slug' => $slug]);
    $template = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$template) return null;

    $assunto = $template['assunto'];
    $corpo = $template['corpo'];

    foreach ($dados as $chave => $valor) {
        $placeholder = "{{" . $chave . "}}";
        $assunto = str_replace($placeholder, $valor, $assunto);
        $corpo = str_replace($placeholder, $valor, $corpo);
    }
    return ['assunto' => $assunto, 'corpo' => $corpo];
}

function enviarEmailBoasVindasCandidato($email, $dados) {
    global $pdo;
    $mail = configurarMailerCandidato();
    $conteudo = processarTemplateCandidato($pdo, 'confirmacao_candidato', $dados);
    if (!$mail || !$conteudo) return false;
    try {
        $mail->addAddress($email);
        $mail->isHTML(true);
        $mail->Subject = $conteudo['assunto'];
        $mail->Body    = $conteudo['corpo'];
        $mail->AltBody = strip_tags($conteudo['corpo']);
        return $mail->send();
    } catch (Exception $e) { return false; }
}

function enviarEmailNotificacaoAdminNovoCandidato($dados) {
    global $pdo;
    $email_admin = $_ENV['SMTP_FROM_EMAIL'] ?? '';
    $mail = configurarMailerCandidato();
    $conteudo = processarTemplateCandidato($pdo, 'notificacao_admin_novo_candidato', $dados);
    if (!$mail || !$conteudo) return false;
    try {
        $mail->addAddress($email_admin);
        $mail->isHTML(true);
        $mail->Subject = $conteudo['assunto'];
        $mail->Body    = $conteudo['corpo'];
        $mail->AltBody = strip_tags($conteudo['corpo']);
        return $mail->send();
    } catch (Exception $e) { return false; }
}