<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'smtp/Exception.php';
require 'smtp/PHPMailer.php';
require 'smtp/SMTP.php';

if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['number']) && isset($_POST['comment'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['number'];
    $message = $_POST['comment'];
    
    // Generate subject based on message content or use default
    $subject = "NatureHills Contact Inquiry - " . date('Y-m-d');

    // Admin Email HTML with NatureHills Agriculture Farm Theme
    $htmlAdmin = "
<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>New Contact Form Submission - NatureHills Organic Farm</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f8fdf6;
            margin: 0;
            padding: 20px;
            color: #1B5E20;
        }
        .email-container {
            max-width: 700px;
            background: #ffffff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 15px rgba(27, 94, 32, 0.1);
            margin: auto;
            border: 1px solid #e8f5e9;
        }
        .email-header {
            background: #1B5E20;
            color: #ffffff;
            text-align: center;
            padding: 20px;
            font-size: 24px;
            font-weight: bold;
            border-radius: 6px 6px 0 0;
            margin-bottom: 20px;
        }
        .section-title {
            background: #f8fdf6;
            font-size: 18px;
            font-weight: bold;
            padding: 12px 15px;
            margin: 25px 0 15px 0;
            border-radius: 6px;
            color: #1B5E20;
            border-left: 4px solid #FF8C00;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 15px 0;
        }
        th, td {
            padding: 12px 15px;
            border-bottom: 1px solid #e8f5e9;
            text-align: left;
        }
        th {
            background: #f8fdf6;
            font-weight: 600;
            color: #1B5E20;
            width: 30%;
        }
        .leaf-icon {
            color: #ffffff;
            margin-right: 8px;
        }
        .naturehills-logo {
            color: #1B5E20;
            font-weight: bold;
            font-size: 22px;
            margin-bottom: 10px;
            text-align: center;
        }
        .highlight {
            color: #FF8C00;
            font-weight: bold;
        }
        .urgent-note {
            background: #fff8f0;
            border: 1px solid #FF8C00;
            border-radius: 6px;
            padding: 15px;
            margin: 15px 0;
            color: #1B5E20;
        }
        .farm-icon {
            text-align: center;
            font-size: 40px;
            margin: 10px 0;
        }
    </style>
</head>

<body>
    <div class='email-container'>
        <div class='naturehills-logo'>NatureHills - Organic Agriculture Farm</div>
        <div class='farm-icon'>🌱</div>
        <div class='email-header'>
            <span class='leaf-icon'>🌿</span> New Contact Form Submission
        </div>

        <!-- Client Information -->
        <div class='section-title'>Contact Information</div>
        <table>
            <tr><th>Name:</th><td>$name</td></tr>
            <tr><th>Email:</th><td>$email</td></tr>
            <tr><th>Phone:</th><td>$phone</td></tr>
            <tr><th>Date:</th><td>" . date('F j, Y \a\t g:i A') . "</td></tr>
        </table>

        <!-- Message Details -->
        <div class='section-title'>Farm Inquiry Details</div>
        <div style='padding: 0 15px; line-height: 1.6;'>
            " . nl2br($message) . "
        </div>

        <!-- Submission Details -->
        <div class='section-title'>Submission Information</div>
        <div style='padding: 0 15px; line-height: 1.6;'>
            <p><strong>Received:</strong> " . date('F j, Y \a\t g:i A') . "</p>
            <p><strong>Source:</strong> NatureHills Website Contact Form</p>
        </div>

        <div class='urgent-note'>
            <p><strong>🌿 Action Required:</strong> Please respond to this farm inquiry within 24 hours to provide excellent customer service for potential produce orders or farm visits.</p>
        </div>
        
    </div>
</body>
</html>";

    // Client Email HTML with NatureHills Agriculture Farm Theme
    $htmlClient = "
<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Thank You for Contacting NatureHills Organic Farm</title>
    <style>
        body { 
            font-family: 'Arial', sans-serif; 
            background-color: #f8fdf6; 
            padding: 20px; 
            color: #1B5E20;
            line-height: 1.6;
        }
        .email-container { 
            max-width: 600px; 
            background: #ffffff; 
            padding: 30px; 
            margin: auto; 
            border-radius: 10px; 
            box-shadow: 0 0 15px rgba(27, 94, 32, 0.1);
            border: 1px solid #e8f5e9;
        }
        .header { 
            background: #1B5E20;
            color: #ffffff; 
            text-align: center; 
            padding: 25px; 
            border-radius: 8px;
            margin-bottom: 25px;
        }
        .header h2 {
            margin: 0;
            font-size: 28px;
        }
        .header p {
            margin: 10px 0 0;
            font-size: 16px;
        }
        .content { 
            margin: 25px 0; 
        }
        .details { 
            background: #f8fdf6; 
            padding: 20px; 
            border-radius: 8px;
            margin: 20px 0;
            border-left: 4px solid #FF8C00;
        }
        .highlight { 
            color: #FF8C00; 
            font-weight: bold; 
        }
        .contact-summary { 
            width: 100%; 
            border-collapse: collapse; 
            margin: 15px 0; 
        }
        .contact-summary th { 
            background: #e8f5e9; 
            text-align: left; 
            padding: 12px 15px;
            color: #1B5E20;
            width: 30%;
        }
        .contact-summary td { 
            padding: 12px 15px; 
            border-bottom: 1px solid #e8f5e9; 
        }
        .footer {
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #e8f5e9;
            text-align: center;
            color: #666666;
        }
        .leaf-icon {
            color: #ffffff;
            margin-right: 8px;
        }
        ol {
            padding-left: 20px;
        }
        ol li {
            margin-bottom: 10px;
        }
        .naturehills-logo {
            color: #1B5E20;
            font-weight: bold;
            font-size: 22px;
            margin-bottom: 10px;
            text-align: center;
        }
        .response-time {
            background: #fff8f0;
            border: 1px solid #FF8C00;
            border-radius: 6px;
            padding: 15px;
            margin: 15px 0;
        }
        .farm-icon {
            text-align: center;
            font-size: 40px;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class='email-container'>
        <div class='naturehills-logo'>NatureHills - Organic Agriculture Farm</div>
        <div class='farm-icon'>🌱</div>
        <div class='header'>
            <h2><span class='leaf-icon'>🌿</span> Thank You, $name!</h2>
            <p>We've received your inquiry at NatureHills Organic Farm</p>
        </div>
        
        <div class='content'>
            <p>Thank you for contacting NatureHills Organic Agriculture Farm. We appreciate you reaching out to us and will respond to your inquiry about our farm produce, events, or services promptly.</p>
            
            <div class='details'>
                <h3 style='color: #1B5E20; margin-top: 0;'>Inquiry Summary:</h3>
                <table class='contact-summary'>
                    <tr><th>Name:</th><td>$name</td></tr>
                    <tr><th>Email:</th><td>$email</td></tr>
                    <tr><th>Phone:</th><td>$phone</td></tr>
                    <tr><th>Date:</th><td>" . date('F j, Y') . "</td></tr>
                </table>

                <h3 style='color: #1B5E20;'>Your Message:</h3>
                <p>" . nl2br($message) . "</p>
            </div>

            <div class='response-time'>
                <p><strong>Response Time:</strong> Our farm team typically responds within 24 hours during business hours. For urgent inquiries about farm produce orders or farm tours, please call us directly.</p>
            </div>

            <p>We're reviewing your inquiry details and will provide you with the best information about our organic produce, farm events, or services.</p>
            
            <h3 style='color: #1B5E20;'>What to Expect Next:</h3>
            <ol>
                <li>Confirmation of your inquiry receipt (this email)</li>
                <li>Response from our farm specialist within 24 hours</li>
                <li>Information about seasonal produce availability</li>
                <li>Farm tour scheduling if requested</li>
                <li>Follow-up to answer any additional questions</li>
            </ol>
        </div>

        <div class='footer'>
            <p>Need immediate assistance or want to visit our farm?<br>
            Call us at <span class='highlight'>(555) 123-FARM</span> for immediate help</p>
            <p style='margin-top: 15px; font-size: 14px;'>
                <span class='leaf-icon'>🌾</span> NatureHills - Growing Fresh, Organic Produce Since 1995
            </p>
        </div>
    </div>
</body>
</html>";

    $mail = new PHPMailer(true);

    try {
        // SMTP settings
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = 'your_email@gmail.com';  // Your SMTP Username
        $mail->Password = 'Your App Password';       // Your SMTP Password (App Password)
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption
        $mail->Port = 587;

        // Sender and recipient settings for admin email
        $mail->setFrom('your_email@gmail.com', 'NatureHills Organic Farm');
        $mail->addAddress('your_email@gmail.com', 'NatureHills Organic Farm');
        
        // Content for admin
        $mail->isHTML(true);
        $mail->Subject = 'New Contact Form Submission from ' . $name . ' - NatureHills';
        $mail->Body = $htmlAdmin;

        $mail->send();
        
        // Clear addresses and send to client
        $mail->clearAddresses();
        $mail->addAddress($email, $name);
        $mail->Subject = 'Thank You for Contacting NatureHills Organic Farm';
        $mail->Body = $htmlClient;
        
        $mail->send();
        
        echo 'Message Sent Successfully!';

    } catch (Exception $e) {
        echo "Error Occurred: {$mail->ErrorInfo}";
    }
}
?>