<?php

namespace App\Libraries;

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

class EmailExecutor {

    public function __construct() {
        // Email configuration is handled in the methods
    }

    /**
     * Send welcome email to newly registered user
     */
    public function sendWelcomeEmail($user) {
        try {
            $mail = new PHPMailer(true);

            // Server settings
            $mail->isSMTP();
            $mail->Host = 'mail.apdigital.co.tz';
            $mail->SMTPAuth = true;
            $mail->Username = 'info@apdigital.co.tz';
            $mail->Password = 'Kiswahili.123';
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
            $mail->Port = 465;

            // Recipients
            $mail->setFrom('info@apdigital.co.tz', 'DevanEvents');
            $mail->addAddress($user->email);

            // Content
            $mail->isHTML(true);
            $mail->Subject = 'Karibu DevanEvents!';
            $mail->Body = $this->getWelcomeEmailTemplate($user);
            $mail->AltBody = 'Karibu DevanEvents! Akaunti yako imesajiliwa kikamilifu.';

            $mail->send();

            if (function_exists('log_message')) {
                log_message('info', 'Welcome email sent to ' . $user->email);
            }
            return true;
        } catch (Exception $e) {
            if (function_exists('log_message')) {
                log_message('error', 'Exception sending welcome email to ' . $user->email . ': ' . $e->getMessage());
            }
            return false;
        }
    }

    /**
     * Get welcome email HTML template
     */
    private function getWelcomeEmailTemplate($user) {
        $name = $user->name ?? 'Mshiriki';

        return "
        <!DOCTYPE html>
        <html lang='sw'>
        <head>
            <meta charset='UTF-8'>
            <meta name='viewport' content='width=device-width, initial-scale=1.0'>
            <title>Karibu DevanEvents</title>
            <style>
                body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
                .container { max-width: 600px; margin: 0 auto; padding: 20px; }
                .header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; text-align: center; border-radius: 10px 10px 0 0; }
                .content { background: #f9f9f9; padding: 30px; border-radius: 0 0 10px 10px; }
                .button { display: inline-block; background: #667eea; color: white; padding: 12px 30px; text-decoration: none; border-radius: 5px; margin: 20px 0; }
                .footer { text-align: center; margin-top: 30px; color: #666; font-size: 12px; }
            </style>
        </head>
        <body>
            <div class='container'>
                <div class='header'>
                    <h1>Karibu DevanEvents!</h1>
                </div>
                <div class='content'>
                    <h2>Ahsante kujiunga nasi, {$name}!</h2>

                    <p>Tumepokea usajili wako kikamilifu. Sasa unaweza:</p>

                    <ul>
                        <li>🔍 Kutafuta matukio mbalimbali na hafla za kila aina</li>
                        <li>📝 Kujisajili kwa hafla unazopenda kwa urahisi</li>
                        <li>🎫 Kununua tiketi na kupata uthibitisho wa papo hapo</li>
                        <li>🔔 Kupata taarifa za matukio mapya na mabadiliko</li>
                        <li>📱 Kufuatilia usajili wako na historia ya matukio</li>
                    </ul>

                    <p>Jukwaa letu linalenga kukuunganisha na fursa za kujifunza, kufurahia na kukutana na watu wengine wenye nia moja.</p>

                    <p>Piga bila kusita kwa maswali yoyote au ushauri!</p>

                    <p style='text-align: center;'>
                        <strong>🔔 TIMU YA DEVANEVENTS</strong>
                    </p>
                </div>
                <div class='footer'>
                    <p>Hii ni email ya kiotomatiki. Tafadhali usijibu.</p>
                </div>
            </div>
        </body>
        </html>
        ";
    }

    /**
     * Send custom email with provided data
     */
    public function sendCustomEmail($emailData) {
        try {
            $mail = new PHPMailer(true);

            // Server settings
            $mail->isSMTP();
            $mail->Host = 'mail.apdigital.co.tz';
            $mail->SMTPAuth = true;
            $mail->Username = 'info@apdigital.co.tz';
            $mail->Password = 'Kiswahili.123';
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
            $mail->Port = 465;

            // Recipients
            $mail->setFrom('info@apdigital.co.tz', 'DevanEvents');
            $mail->addAddress($emailData['to']);

            // Content
            $mail->isHTML(true);
            $mail->Subject = $emailData['subject'];
            $mail->Body = $emailData['message'];
            $mail->AltBody = strip_tags($emailData['message']);

            $mail->send();

            if (function_exists('log_message')) {
                log_message('info', 'Custom email sent to ' . $emailData['to']);
            }
            return true;
        } catch (Exception $e) {
            if (function_exists('log_message')) {
                log_message('error', 'Exception sending custom email to ' . $emailData['to'] . ': ' . $e->getMessage());
            }
            return false;
        }
    }
}