<?php
/**
 * Email Functions
 * Path: includes/email-functions.php
 */

require_once __DIR__ . '/email-config.php';

// Load PHPMailer (files direkt në includes/PHPMailer/)
if(file_exists(__DIR__ . '/PHPMailer/PHPMailer.php')) {
    require_once __DIR__ . '/PHPMailer/PHPMailer.php';
    require_once __DIR__ . '/PHPMailer/SMTP.php';
    require_once __DIR__ . '/PHPMailer/Exception.php';
}

/**
 * Send email using PHPMailer
 */
function sendEmail($to_email, $to_name, $subject, $html_content, $plain_content = null) {
    if (!EMAIL_ENABLED) {
        return true;
    }
    
    if(!class_exists('PHPMailer\PHPMailer\PHPMailer')) {
        error_log("PHPMailer not found");
        return false;
    }
    
    try {
        $mail = new PHPMailer\PHPMailer\PHPMailer(true);
        $mail->SMTPDebug = 0;
        $mail->isSMTP();
        $mail->Host       = SMTP_HOST;
        $mail->SMTPAuth   = SMTP_AUTH;
        $mail->Username   = SMTP_USERNAME;
        $mail->Password   = SMTP_PASSWORD;
        $mail->SMTPSecure = SMTP_SECURE;
        $mail->Port       = SMTP_PORT;
        $mail->CharSet = 'UTF-8';
        
        $mail->setFrom(SENDER_EMAIL, SENDER_NAME);
        $mail->addAddress($to_email, $to_name);
        
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $html_content;
        $mail->AltBody = $plain_content ?: strip_tags($html_content);
        
        $mail->send();
        error_log("Email sent to: {$to_email}");
        return true;
        
    } catch (Exception $e) {
        error_log("Email error: " . ($mail->ErrorInfo ?? $e->getMessage()));
        return false;
    }
}

/**
 * Send booking confirmation to customer
 */
function sendCustomerBookingEmail($booking_data, $car_data, $price_data) {
    if (!EMAIL_CUSTOMER_ON_BOOKING) {
        return true;
    }
    
    return sendEmail(
        $booking_data['customer_email'],
        $booking_data['customer_name'],
        SENDER_NAME . ' - Booking Confirmation #' . $booking_data['reservation_code'],
        getCustomerBookingTemplate($booking_data, $car_data, $price_data),
        getCustomerBookingText($booking_data, $car_data, $price_data)
    );
}

/**
 * Send notification to admin
 */
function sendAdminBookingNotification($booking_data, $car_data, $price_data) {
    if (!EMAIL_ADMIN_ON_BOOKING) {
        return true;
    }
    
    return sendEmail(
        ADMIN_EMAIL,
        ADMIN_NAME,
        '🔔 New Reservation - ' . $booking_data['reservation_code'],
        getAdminNotificationTemplate($booking_data, $car_data, $price_data),
        getAdminNotificationText($booking_data, $car_data, $price_data)
    );
}

/**
 * Send both emails
 */
function sendBookingEmails($booking_data, $car_data, $price_data) {
    $customer = sendCustomerBookingEmail($booking_data, $car_data, $price_data);
    $admin = sendAdminBookingNotification($booking_data, $car_data, $price_data);
    return ($customer && $admin);
}

/**
 * Test SMTP connection
 */
function testSmtpConnection() {
    if (!EMAIL_ENABLED) {
        return ['success' => false, 'message' => 'Email sending is disabled'];
    }
    
    if(!class_exists('PHPMailer\PHPMailer\PHPMailer')) {
        return ['success' => false, 'message' => 'PHPMailer not found'];
    }
    
    try {
        $mail = new PHPMailer\PHPMailer\PHPMailer(true);
        $mail->SMTPDebug = 0;
        $mail->isSMTP();
        $mail->Host       = SMTP_HOST;
        $mail->SMTPAuth   = SMTP_AUTH;
        $mail->Username   = SMTP_USERNAME;
        $mail->Password   = SMTP_PASSWORD;
        $mail->SMTPSecure = SMTP_SECURE;
        $mail->Port       = SMTP_PORT;
        $mail->CharSet = 'UTF-8';
        
        $mail->setFrom(SENDER_EMAIL, SENDER_NAME);
        $mail->addAddress(ADMIN_EMAIL, ADMIN_NAME);
        $mail->Subject = 'Test Email from ESC Rental';
        $mail->isHTML(true);
        $mail->Body = '<h2>SMTP Test Successful!</h2><p>Your email configuration is working correctly.</p>';
        $mail->AltBody = 'SMTP Test Successful!';
        
        $mail->send();
        return ['success' => true, 'message' => 'Test email sent successfully to ' . ADMIN_EMAIL];
        
    } catch (Exception $e) {
        return ['success' => false, 'message' => 'SMTP Error: ' . ($mail->ErrorInfo ?? $e->getMessage())];
    }
}
?>