<?php
// mailer.php - Email Functions (PHP mail)
// File location: /bidwheels.net/includes/mailer.php

require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/functions.php';

/**
 * Send HTML email using PHP mail function
 */
function sendMail($to, $subject, $html_content) {
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= "From: " . NOREPLY_EMAIL . "\r\n";
    $headers .= "Reply-To: " . SUPPORT_EMAIL . "\r\n";
    $headers .= "X-Mailer: PHP/" . phpversion();
    
    return mail($to, $subject, $html_content, $headers);
}

/**
 * Get email template with logo
 */
function getEmailTemplate($content) {
    $logo = SITE_URL . '/bidwheel_logo.png';
    
    return '
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>' . SITE_NAME . '</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                margin: 0;
                padding: 0;
                background-color: #f4f4f4;
            }
            .container {
                max-width: 600px;
                margin: 20px auto;
                background-color: #ffffff;
                border-radius: 8px;
                overflow: hidden;
                box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            }
            .header {
                background-color: #1a1a2e;
                padding: 20px;
                text-align: center;
            }
            .header img {
                max-width: 150px;
                height: auto;
            }
            .content {
                padding: 30px;
                color: #333;
                line-height: 1.6;
            }
            .footer {
                background-color: #f4f4f4;
                padding: 15px;
                text-align: center;
                font-size: 12px;
                color: #666;
            }
            .btn {
                display: inline-block;
                padding: 10px 20px;
                background-color: #e63946;
                color: white;
                text-decoration: none;
                border-radius: 5px;
                margin-top: 15px;
            }
            .social {
                margin-top: 20px;
            }
            .social a {
                margin: 0 10px;
                color: #1a1a2e;
                text-decoration: none;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">
                <img src="' . $logo . '" alt="' . SITE_NAME . '">
                <h2 style="color: white; margin: 10px 0 0;">' . SITE_NAME . '</h2>
            </div>
            <div class="content">
                ' . $content . '
            </div>
            <div class="footer">
                <p>&copy; ' . date('Y') . ' ' . SITE_NAME . '. All rights reserved.</p>
                <p>Questions? Contact us at <a href="mailto:' . SUPPORT_EMAIL . '">' . SUPPORT_EMAIL . '</a></p>
            </div>
        </div>
    </body>
    </html>
    ';
}

/**
 * Send welcome email
 */
function sendWelcomeEmail($to, $name) {
    $content = '
        <h2>Welcome to ' . SITE_NAME . '!</h2>
        <p>Dear ' . htmlspecialchars($name) . ',</p>
        <p>Thank you for registering with ' . SITE_NAME . '. We are excited to have you on board!</p>
        <p>With our platform, you can:</p>
        <ul>
            <li>Bid on amazing vehicles</li>
            <li>Track your bids in real-time</li>
            <li>Get notified when you are outbid</li>
            <li>Discover exclusive deals</li>
        </ul>
        <p>Start exploring our listings and place your first bid today!</p>
        <a href="' . SITE_URL . '/product/index.php" class="btn">Browse Listings</a>
    ';
    
    return sendMail($to, 'Welcome to ' . SITE_NAME, getEmailTemplate($content));
}

/**
 * Send bid confirmation email
 */
function sendBidConfirmationEmail($to, $name, $product_name, $bid_amount, $product_id) {
    $content = '
        <h2>Bid Confirmation</h2>
        <p>Dear ' . htmlspecialchars($name) . ',</p>
        <p>You have successfully placed a bid of <strong>' . formatPrice($bid_amount) . '</strong> on:</p>
        <h3>' . htmlspecialchars($product_name) . '</h3>
        <p>Your bid has been registered. You will be notified if someone outbids you.</p>
        <a href="' . SITE_URL . '/product/detail.php?id=' . $product_id . '" class="btn">View Product</a>
    ';
    
    return sendMail($to, 'Bid Confirmation - ' . SITE_NAME, getEmailTemplate($content));
}

/**
 * Send outbid notification email
 */
function sendOutbidEmail($to, $name, $product_name, $new_highest_bid, $product_id) {
    $content = '
        <h2>You\'ve Been Outbid!</h2>
        <p>Dear ' . htmlspecialchars($name) . ',</p>
        <p>Someone has placed a higher bid on <strong>' . htmlspecialchars($product_name) . '</strong>.</p>
        <p>The current highest bid is now: <strong>' . formatPrice($new_highest_bid) . '</strong></p>
        <p>Don\'t miss out! Place a new bid to stay in the competition.</p>
        <a href="' . SITE_URL . '/product/detail.php?id=' . $product_id . '" class="btn">Place New Bid</a>
    ';
    
    return sendMail($to, 'You\'ve Been Outbid on ' . SITE_NAME, getEmailTemplate($content));
}

/**
 * Send payment confirmation email
 */
function sendPaymentConfirmationEmail($to, $name, $product_name, $bid_amount) {
    $content = '
        <h2>Payment Information Received</h2>
        <p>Dear ' . htmlspecialchars($name) . ',</p>
        <p>Thank you for your bid of <strong>' . formatPrice($bid_amount) . '</strong> on <strong>' . htmlspecialchars($product_name) . '</strong>.</p>
        <p>Your payment information has been recorded. Our team will review it and contact you within 24-48 hours.</p>
        <p>If you have any questions, please contact our support team at ' . SUPPORT_EMAIL . '</p>
    ';
    
    return sendMail($to, 'Payment Information Received - ' . SITE_NAME, getEmailTemplate($content));
}

/**
 * Log email to database (optional)
 */
function logEmail($to, $subject, $body, $status = 'sent') {
    global $conn;
    $to = mysqli_real_escape_string($conn, $to);
    $subject = mysqli_real_escape_string($conn, $subject);
    $body = mysqli_real_escape_string($conn, $body);
    $status = mysqli_real_escape_string($conn, $status);
    
    $sql = "INSERT INTO email_log (to_email, subject, body, status) VALUES ('$to', '$subject', '$body', '$status')";
    return mysqli_query($conn, $sql);
}
?>