<?php
defined('BASEPATH') OR exit('No direct script access allowed');

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

class My_phpmailer
{
    private $mail;
    
    public function __construct()
    {
        require_once APPPATH . 'third_party/PHPMailer/src/Exception.php';
        require_once APPPATH . 'third_party/PHPMailer/src/PHPMailer.php';
        require_once APPPATH . 'third_party/PHPMailer/src/SMTP.php';
        
        $this->mail = new PHPMailer(true);
        
        // Konfigurasi SMTP
        $this->mail->isSMTP();
        $this->mail->Host       = 'smtp.gmail.com';
        $this->mail->SMTPAuth   = true;
        $this->mail->Username   = 'rizqywahyu59@gmail.com'; // Email Anda
        $this->mail->Password   = 'abcdefghijklmnop';       // App Password
        $this->mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
        $this->mail->Port       = 465;
        
        $this->mail->setFrom('rizqywahyu59@gmail.com', 'KosCare System');
        $this->mail->isHTML(true);
    }
    
    public function sendResetEmail($to_email, $name, $code)
    {
        try {
            $this->mail->addAddress($to_email, $name);
            $this->mail->Subject = 'Reset Password - KosCare Admin';
            
            $html = $this->getResetEmailTemplate($name, $code);
            $this->mail->Body = $html;
            
            $this->mail->send();
            return true;
            
        } catch (Exception $e) {
            log_message('error', 'PHPMailer Error: ' . $this->mail->ErrorInfo);
            return false;
        }
    }
    
    private function getResetEmailTemplate($name, $code)
    {
        return "
        <!DOCTYPE html>
        <html>
        <head>
            <style>
                body { font-family: Arial, sans-serif; }
                .container { max-width: 600px; margin: auto; padding: 20px; border: 1px solid #ddd; }
                .header { background: #4a90e2; color: white; padding: 15px; text-align: center; }
                .code { 
                    background: #f4f4f4; 
                    padding: 20px; 
                    text-align: center; 
                    font-size: 32px; 
                    font-weight: bold; 
                    letter-spacing: 5px; 
                    margin: 20px 0; 
                }
            </style>
        </head>
        <body>
            <div class='container'>
                <div class='header'>
                    <h2>Reset Password KosCare Admin</h2>
                </div>
                
                <p>Halo <strong>$name</strong>,</p>
                
                <p>Berikut adalah kode reset password Anda:</p>
                
                <div class='code'>$code</div>
                
                <p>Kode ini berlaku selama <strong>30 menit</strong>.</p>
                
                <p><strong>Penting:</strong></p>
                <ul>
                    <li>Jangan bagikan kode ini kepada siapa pun</li>
                    <li>Jika Anda tidak meminta reset password, abaikan email ini</li>
                    <li>Password Anda tidak akan berubah sampai proses selesai</li>
                </ul>
                
                <hr>
                
                <p style='color: #666; font-size: 12px;'>
                    Email ini dikirim otomatis oleh sistem KosCare.<br>
                    © " . date('Y') . " KosCare. All rights reserved.
                </p>
            </div>
        </body>
        </html>
        ";
    }
}