<?php
require_once 'db.php';
header('Content-Type: application/json');
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

try {
    $mailer = new PHPMailer(true);
    $mailer->isSMTP();
    $mailer->Host = 'smtp.us.appsuite.cloud'; 
    $mailer->SMTPAuth = true;
    $mailer->SMTPSecure = 'ssl';
    $mailer->Username = 'no-reply@imators.systems';
    $mailer->Password = 'imators.managements4455*#@';
    $mailer->Port = 465;
    
    // Options SSL supplémentaires
    $mailer->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    
    $mailer->SMTPDebug = SMTP::DEBUG_SERVER;
    $mailer->Debugoutput = function($str, $level) {
        error_log("SMTP Debug: $str");
    };
    
    // Test de connexion avec timeout
    $mailer->Timeout = 30;
    $mailer->smtpConnect();
    
    echo json_encode([
        'success' => true,
        'message' => 'Email server is connected and ready'
    ]);
} catch (Exception $e) {
    error_log("SMTP Error: " . $e->getMessage());
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'message' => 'Load failed'
    ]);
}