<?php
/**
 * Email Service Class
 * Handles sending emails (abandoned carts, notifications, etc.)
 */

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

class EmailService {
    private $fromEmail;
    private $fromName;

    public function __construct() {
        $this->fromEmail = EMAIL_FROM ?? SITE_EMAIL;
        $this->fromName = EMAIL_FROM_NAME ?? SITE_NAME;
    }

    /**
     * Send abandoned cart reminder email
     */
    public function sendAbandonedCartReminder($email, $cartItems, $cartId) {
        $subject = 'Ai lăsat produse în coș!';
        
        $itemsHtml = '';
        $total = 0;
        foreach ($cartItems as $item) {
            $itemTotal = $item['price_at_add'] * $item['quantity'];
            $total += $itemTotal;
            $productImage = !empty($item['image']) ? SITE_URL . '/' . $item['image'] : SITE_URL . '/assets/img/shop/electronics/thumbs/01.png';
            $itemsHtml .= "
                <tr>
                    <td style='padding: 15px; border-bottom: 1px solid #eee;'>
                        <img src='{$productImage}' width='80' alt='" . htmlspecialchars($item['name']) . "' style='border-radius: 4px;'>
                    </td>
                    <td style='padding: 15px; border-bottom: 1px solid #eee;'>
                        <strong>" . htmlspecialchars($item['name']) . "</strong>
                    </td>
                    <td style='padding: 15px; border-bottom: 1px solid #eee; text-align: center;'>{$item['quantity']}</td>
                    <td style='padding: 15px; border-bottom: 1px solid #eee; text-align: right; font-weight: bold;'>" . number_format($itemTotal, 2, ',', '.') . " RON</td>
                </tr>
            ";
        }

        $checkoutUrl = SITE_URL . '/cos';
        
        $message = "
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset='UTF-8'>
            <meta name='viewport' content='width=device-width, initial-scale=1.0'>
            <style>
                body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; line-height: 1.6; color: #333; margin: 0; padding: 0; background-color: #f4f4f4; }
                .container { max-width: 600px; margin: 0 auto; background: #ffffff; }
                .header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px 20px; text-align: center; }
                .header h1 { margin: 0; font-size: 24px; }
                .content { padding: 30px 20px; }
                .content p { margin: 15px 0; }
                .button { display: inline-block; padding: 14px 28px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; text-decoration: none; border-radius: 6px; margin: 25px 0; font-weight: bold; text-align: center; }
                table { width: 100%; border-collapse: collapse; margin: 20px 0; background: #fff; }
                table th { background: #f8f9fa; padding: 12px; text-align: left; border-bottom: 2px solid #dee2e6; font-weight: 600; }
                table td { padding: 15px; border-bottom: 1px solid #eee; }
                .total-row { background: #f8f9fa; font-size: 18px; }
                .footer { text-align: center; padding: 20px; color: #666; font-size: 12px; background: #f8f9fa; }
                .button-container { text-align: center; margin: 30px 0; }
            </style>
        </head>
        <body>
            <div class='container'>
                <div class='header'>
                    <h1>Completează comanda ta!</h1>
                </div>
                <div class='content'>
                    <p>Salut,</p>
                    <p>Ai lăsat câteva produse în coșul tău. Nu rata aceste produse minunate!</p>
                    <table>
                        <thead>
                            <tr>
                                <th style='width: 100px;'>Imagine</th>
                                <th>Produs</th>
                                <th style='text-align: center; width: 80px;'>Cantitate</th>
                                <th style='text-align: right; width: 120px;'>Preț</th>
                            </tr>
                        </thead>
                        <tbody>
                            {$itemsHtml}
                        </tbody>
                        <tfoot>
                            <tr class='total-row'>
                                <td colspan='3' style='padding: 15px; text-align: right; font-weight: bold;'>Total:</td>
                                <td style='padding: 15px; text-align: right; font-weight: bold; font-size: 20px; color: #667eea;'>" . number_format($total, 2, ',', '.') . " RON</td>
                            </tr>
                        </tfoot>
                    </table>
                    <div class='button-container'>
                        <a href='{$checkoutUrl}' class='button'>Finalizează comanda</a>
                    </div>
                    <p style='margin-top: 30px; font-size: 14px; color: #666;'>
                        Acest email a fost trimis pentru că ai adăugat produse în coș dar nu ai finalizat comanda.
                    </p>
                </div>
                <div class='footer'>
                    <p>&copy; " . date('Y') . " " . SITE_NAME . ". Toate drepturile rezervate.</p>
                    <p style='margin-top: 10px;'>
                        <a href='" . SITE_URL . "/cont/notificari' style='color: #667eea; text-decoration: none;'>Gestionează notificările</a>
                    </p>
                </div>
            </div>
        </body>
        </html>
        ";

        return $this->sendEmail($email, $subject, $message);
    }

    /**
     * Send generic email
     */
    public function sendEmail($to, $subject, $message, $isHtml = true) {
        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: " . ($isHtml ? "text/html" : "text/plain") . "; charset=UTF-8\r\n";
        $headers .= "From: {$this->fromName} <{$this->fromEmail}>\r\n";
        $headers .= "Reply-To: {$this->fromEmail}\r\n";
        $headers .= "X-Mailer: PHP/" . phpversion();

        return mail($to, $subject, $message, $headers);
    }

    /**
     * Send order confirmation email
     */
    public function sendOrderConfirmation($email, $order) {
        $subject = 'Confirmare comandă #' . $order['order_number'];
        
        $orderUrl = SITE_URL . '/cont/comenzi';
        
        $message = "
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset='UTF-8'>
            <meta name='viewport' content='width=device-width, initial-scale=1.0'>
            <style>
                body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; line-height: 1.6; color: #333; margin: 0; padding: 0; background-color: #f4f4f4; }
                .container { max-width: 600px; margin: 0 auto; background: #ffffff; }
                .header { background: linear-gradient(135deg, #28a745 0%, #20c997 100%); color: white; padding: 30px 20px; text-align: center; }
                .header h1 { margin: 0; font-size: 24px; }
                .content { padding: 30px 20px; }
                .content p { margin: 15px 0; }
                .order-info { background: #f8f9fa; padding: 20px; border-radius: 6px; margin: 20px 0; }
                .order-info strong { color: #28a745; }
                .button { display: inline-block; padding: 14px 28px; background: linear-gradient(135deg, #28a745 0%, #20c997 100%); color: white; text-decoration: none; border-radius: 6px; margin: 25px 0; font-weight: bold; }
                .footer { text-align: center; padding: 20px; color: #666; font-size: 12px; background: #f8f9fa; }
                .button-container { text-align: center; margin: 30px 0; }
            </style>
        </head>
        <body>
            <div class='container'>
                <div class='header'>
                    <h1>✓ Comandă confirmată!</h1>
                </div>
                <div class='content'>
                    <p>Mulțumim pentru comandă!</p>
                    <div class='order-info'>
                        <p style='margin: 10px 0;'><strong>Număr comandă:</strong> {$order['order_number']}</p>
                        <p style='margin: 10px 0;'><strong>Total:</strong> " . number_format($order['total'], 2, ',', '.') . " RON</p>
                        <p style='margin: 10px 0;'><strong>Data:</strong> " . date('d.m.Y H:i', strtotime($order['created_at'])) . "</p>
                    </div>
                    <p>Vom trimite un alt email când comanda ta va fi expediată.</p>
                    <div class='button-container'>
                        <a href='{$orderUrl}' class='button'>Vezi detaliile comenzii</a>
                    </div>
                </div>
                <div class='footer'>
                    <p>&copy; " . date('Y') . " " . SITE_NAME . ". Toate drepturile rezervate.</p>
                </div>
            </div>
        </body>
        </html>
        ";

        return $this->sendEmail($email, $subject, $message);
    }
}
?>

