<?php

namespace App\Services;

class EmailService
{
    public function sendPasswordReset(string $to, string $token): bool
    {
        $url = rtrim(base_url(), '/') . '/reset-password/' . rawurlencode($token);
        $email = service('email');
        $email->setTo($to);
        $email->setSubject('Reset your TibaLeta password');
        $email->setMessage("A password reset was requested for your TibaLeta account.\n\nReset password: {$url}\n\nThis link expires in 60 minutes. If you did not request it, you can ignore this email.");
        try {
            if ($email->send()) return true;
        } catch (\Throwable $e) {
            log_message('warning', 'Password reset email failed: {message}', ['message'=>$e->getMessage()]);
        }
        if (ENVIRONMENT !== 'production') log_message('info', 'TibaLeta dev reset URL for {email}: {url}', ['email'=>$to,'url'=>$url]);
        return false;
    }
}
