<?php
require_once ('config/db.php');

// Email configuration - REPLACE WITH YOUR DETAILS
$owner_email = 'user-mail'; // Your email address
$smtp_host = 'ssl0.ovh.net';
$smtp_port = 587;
$smtp_user = 'contact@moneyteam.fr'; // Your Gmail
$smtp_pass = '$2GDu}5zRu'
 code'; // Gmail App Password


// Validate CAPTCHA
if (!isset($_POST['captcha']) || $_POST['captcha'] != $_SESSION['captcha']) {
    $_SESSION['message'] = '❌ Invalid CAPTCHA code. Please try again.';
    $_SESSION['msg_type'] = 'error';
    header('Location: index.php');
    exit();
}

// Validate terms checkbox
if (!isset($_POST['terms'])) {
    $_SESSION['message'] = '❌ You must agree to the terms and conditions.';
    $_SESSION['msg_type'] = 'error';
    header('Location: index.php');
    exit();
}

// Sanitize input data
$name = htmlspecialchars(trim($_POST['name']));
$email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
$phone = htmlspecialchars(trim($_POST['phone']));
$inquiry_type = htmlspecialchars(trim($_POST['inquiry_type']));
$message = htmlspecialchars(trim($_POST['message']));

// Validate email
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $_SESSION['message'] = '❌ Invalid email address.';
    $_SESSION['msg_type'] = 'error';
    header('Location: index.php');
    exit();
}

try {
    // Database connection already included from config/db.php
    // $conn variable is now available
    
    // Insert data into database
    $stmt = $conn->prepare("INSERT INTO phpmailer (name, email, phone, inquiry_type, message, created_at) VALUES (?, ?, ?, ?, ?, NOW())");
    $stmt->bind_param("sssss", $name, $email, $phone, $inquiry_type, $message);
    
    if (!$stmt->execute()) {
        throw new Exception("Database insert failed: " . $stmt->error);
    }
    
    $stmt->close();
    
    // Send email using PHPMailer
    require 'phpmailer/PHPMailer.php';
    require 'phpmailer/SMTP.php';
    require 'phpmailer/Exception.php';
    
    $mail = new PHPMailer\PHPMailer\PHPMailer(true);
    
    // SMTP Configuration
    $mail->isSMTP();
    $mail->Host = $smtp_host;
    $mail->SMTPAuth = true;
    $mail->Username = $smtp_user;
    $mail->Password = $smtp_pass;
    $mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = $smtp_port;
    
    // Email content
    $mail->setFrom($smtp_user, 'Contact Form System');
    $mail->addAddress($owner_email);
    $mail->addReplyTo($email, $name);
    
    $mail->isHTML(true);
    $mail->Subject = "New Contact Form Submission: $inquiry_type";
    
    $mail->Body = "
    <html>
    <head>
        <style>
            * { margin: 0; padding: 0; box-sizing: border-box; }
            body { 
                font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; 
                line-height: 1.6; 
                color: #1a1a1a; 
                background-color: #f5f5f5;
            }
            .email-wrapper { 
                max-width: 650px; 
                margin: 40px auto; 
                background: #ffffff; 
                border-radius: 12px;
                overflow: hidden;
                box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            }
            .header { 
                background: linear-gradient(135deg, #10b981 0%, #059669 100%); 
                color: white; 
                padding: 40px 30px; 
                text-align: center;
                position: relative;
            }
            .header::after {
                content: '';
                position: absolute;
                bottom: -2px;
                left: 0;
                right: 0;
                height: 3px;
                background: #047857;
            }
            .header h1 { 
                font-size: 28px; 
                font-weight: 700;
                margin-bottom: 8px;
                letter-spacing: -0.5px;
            }
            .header p {
                font-size: 14px;
                opacity: 0.95;
                font-weight: 400;
            }
            .icon-badge {
                display: inline-block;
                width: 60px;
                height: 60px;
                background: rgba(255, 255, 255, 0.2);
                border-radius: 50%;
                margin-bottom: 15px;
                line-height: 60px;
                font-size: 30px;
            }
            .content { 
                padding: 40px 30px; 
                background: #ffffff;
            }
            .field { 
                margin-bottom: 24px;
                border-bottom: 1px solid #e5e7eb;
                padding-bottom: 20px;
            }
            .field:last-of-type {
                border-bottom: none;
                padding-bottom: 0;
            }
            .label { 
                font-weight: 600; 
                color: #10b981; 
                display: block; 
                margin-bottom: 8px;
                font-size: 13px;
                text-transform: uppercase;
                letter-spacing: 0.5px;
            }
            .value { 
                padding: 14px 18px; 
                background: #f9fafb; 
                border-left: 4px solid #10b981;
                color: #1f2937;
                font-size: 15px;
                border-radius: 4px;
                word-wrap: break-word;
            }
            .message-value {
                background: #f0fdf4;
                min-height: 80px;
                border-left-color: #059669;
            }
            .footer { 
                text-align: center; 
                padding: 25px 30px;
                background: #f9fafb;
                border-top: 1px solid #e5e7eb;
            }
            .footer p {
                color: #6b7280;
                font-size: 13px;
                margin-bottom: 8px;
            }
            .timestamp {
                color: #10b981;
                font-weight: 600;
                font-size: 12px;
                display: inline-block;
                padding: 6px 12px;
                background: white;
                border-radius: 4px;
                margin-top: 8px;
            }
            .divider {
                height: 2px;
                background: linear-gradient(to right, transparent, #10b981, transparent);
                margin: 30px 0;
            }
        </style>
    </head>
    <body>
        <div class='email-wrapper'>
            <div class='header'>
                <div class='icon-badge'>📧</div>
                <h1>New Contact Inquiry</h1>
                <p>You have received a new message from your website</p>
            </div>
            <div class='content'>
                <div class='field'>
                    <span class='label'>👤 Full Name</span>
                    <div class='value'>$name</div>
                </div>
                <div class='field'>
                    <span class='label'>✉️ Email Address</span>
                    <div class='value'>$email</div>
                </div>
                <div class='field'>
                    <span class='label'>📱 Phone Number</span>
                    <div class='value'>$phone</div>
                </div>
                <div class='field'>
                    <span class='label'>🏷️ Inquiry Type</span>
                    <div class='value'>$inquiry_type</div>
                </div>
                <div class='divider'></div>
                <div class='field'>
                    <span class='label'>💬 Message</span>
                    <div class='value message-value'>" . nl2br($message) . "</div>
                </div>
            </div>
            <div class='footer'>
                <p>This message was sent from your website contact form</p>
                <span class='timestamp'>⏰ " . date('F j, Y • g:i A') . "</span>
            </div>
        </div>
    </body>
    </html>
    ";
    
    $mail->AltBody = "New contact form submission from $name ($email)\n\nPhone: $phone\nInquiry Type: $inquiry_type\n\nMessage:\n$message";
    
    $mail->send();
    
    // Success message
    $_SESSION['message'] = '✅ Thank you! Your message has been sent successfully. We will contact you soon.';
    $_SESSION['msg_type'] = 'success';
    
    // Regenerate CAPTCHA
    $_SESSION['captcha'] = rand(1000, 9999);
    
} catch (Exception $e) {
    $_SESSION['message'] = '❌ Error: ' . $e->getMessage();
    $_SESSION['msg_type'] = 'error';
}

$conn->close();
header('Location: index.php');
exit();
?>