<?php

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

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

if (isset($_POST['email'])) {

    $user_email = htmlspecialchars(trim($_POST['email']));

    // HTML content for the user's confirmation email
    $user_html = "
<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>NatureHills - Newsletter Subscription Confirmation</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; 
        }
        .footer {
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #e8f5e9;
            text-align: center;
            color: #666666;
        }
        .leaf-icon {
            color: #ffffff;
            margin-right: 8px;
        }
        .naturehills-logo {
            color: #1B5E20;
            font-weight: bold;
            font-size: 22px;
            margin-bottom: 10px;
            text-align: center;
        }
        ul {
            padding-left: 20px;
        }
        ul li {
            margin-bottom: 10px;
        }
        .special-offer {
            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> Welcome to NatureHills Newsletter!</h2>
            <p>Your Connection to Organic Farming & Fresh Produce</p>
        </div>
        
        <div class='content'>
            <p>Thank you for subscribing to the NatureHills newsletter! We're excited to share updates about our organic farming practices, seasonal produce, farm events, and exclusive offers to bring fresh, healthy food to your table.</p>
            
            <div class='details'>
                <h3 style='color: #1B5E20; margin-top: 0;'>Subscription Details:</h3>
                <p>You will receive updates at: <span class='highlight'>$user_email</span></p>
            </div>

            <div class='special-offer'>
                <p><strong>Subscriber Exclusive:</strong> As a newsletter subscriber, you'll receive <span class='highlight'>10% OFF</span> your first farm produce order!</p>
            </div>

            <p>What you'll receive in our newsletter:</p>
            <ul>
                <li>Seasonal organic produce availability</li>
                <li>Farm-to-table recipes and cooking tips</li>
                <li>Upcoming farm events and workshops</li>
                <li>Sustainable farming practices insights</li>
                <li>CSA (Community Supported Agriculture) updates</li>
                <li>Farmers' market schedules and locations</li>
                <li>Organic gardening tips and tricks</li>
                <li>Special promotions on fresh produce</li>
            </ul>
        </div>

        <div class='footer'>
            <p>Need to update your preferences?<br>
            Contact us at <span class='highlight'>(555) 123-FARM</span> or reply to this email</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>";

    // HTML content for the notification email to the website owner
    $owner_html = "
<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>New NatureHills Newsletter Subscription</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;
        }
        .potential-lead {
            background: #fff8f0;
            border: 1px solid #FF8C00;
            border-radius: 6px;
            padding: 15px;
            margin: 15px 0;
        }
    </style>
</head>

<body>
    <div class='email-container'>
        <div class='naturehills-logo'>NatureHills - Organic Agriculture Farm</div>
        <div class='email-header'>
            <span class='leaf-icon'>🌿</span> New Newsletter Subscription
        </div>

        <!-- Subscriber Information -->
        <div class='section-title'>Subscriber Information</div>
        <table>
            <tr><th>Email:</th><td>$user_email</td></tr>
            <tr><th>Subscription Date:</th><td>" . date('Y-m-d H:i:s') . "</td></tr>
        </table>

        <div class='section-title'>Subscription Source</div>
        <div style='padding: 0 15px; line-height: 1.6;'>
            This subscription was received through the NatureHills website newsletter form.
        </div>

        <div class='section-title'>Newsletter Content</div>
        <div style='padding: 0 15px; line-height: 1.6;'>
            <p>Subscriber will receive our weekly newsletter featuring:</p>
            <ul>
                <li>Seasonal organic produce updates</li>
                <li>Farm-to-table recipes and cooking tips</li>
                <li>Farm events and workshop announcements</li>
                <li>Sustainable agriculture practices</li>
                <li>CSA program information</li>
                <li>Farmers' market schedules</li>
                <li>Organic gardening advice</li>
                <li>Special promotions and discounts</li>
            </ul>
        </div>

        <div class='potential-lead'>
            <p><strong>Marketing Opportunity:</strong> This subscriber has shown interest in organic farming and fresh produce. Consider:</p>
            <ul>
                <li>Add to weekly seasonal produce updates</li>
                <li>Include in organic gardening tips campaign</li>
                <li>Send welcome offer for first produce order</li>
                <li>Invite to upcoming farm events</li>
                <li>Share CSA membership information</li>
            </ul>
        </div>
    </div>
</body>
</html>";

    // Create a new PHPMailer instance
    $mail = new PHPMailer(true);

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

        // Send confirmation email to the user
        $mail->setFrom("your_email@gmail.com", "NatureHills Organic Farm");
        $mail->addAddress($user_email);
        $mail->isHTML(true);
        $mail->Subject = 'NatureHills Newsletter Subscription Confirmation';
        $mail->Body = $user_html;
        $mail->send();

        // Send notification email to the website owner
        $mail->clearAddresses();
        $mail->addAddress("your_email@gmail.com", "NatureHills Organic Farm");
        $mail->Subject = 'New NatureHills Newsletter Subscriber';
        $mail->Body = $owner_html;
        $mail->send();

        $msg = "Thank you for subscribing!";
    } catch (Exception $e) {
        $msg = "Error occurred while sending email.";
    }

    echo $msg;
}
?>