<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Http;
use Illuminate\Notifications\Messages\MailMessage;

class BrevoResetPassword extends Notification
{
    public $token;
    public $email;

    public function __construct($token, $email)
    {
        $this->token = $token;
        $this->email = $email;
    }

    public function via($notifiable)
    {
        return ['mail']; // WAJIB: pakai channel yang valid
    }

    public function toMail($notifiable)
    {
        $resetUrl = URL::temporarySignedRoute(
            'password.reset',
            Carbon::now()->addMinutes(60),
            ['token' => $this->token, 'email' => $this->email]
        );

        // Kirim langsung lewat Brevo API
        Http::withHeaders([
            'api-key' => env('BREVO_API_KEY'),
            'Content-Type' => 'application/json',
            'accept' => 'application/json',
        ])->post('https://api.brevo.com/v3/smtp/email', [
            'sender' => [
                'name' => 'Pintera Website',
                'email' => 'agya.rwildanti@gmail.com' // HARUS diverifikasi di Brevo
            ],
            'to' => [
                ['email' => $this->email],
            ],
            'subject' => 'Reset Password Aplikasi Kamu',
            'htmlContent' => "
                <p>Klik link berikut untuk mereset password:</p>
                <p><a href='{$resetUrl}'>Reset Password</a></p>
                <p>Link berlaku 60 menit.</p>
            ",
        ]);

        // Return dummy MailMessage agar Laravel tidak error
        return (new MailMessage)
            ->subject('Reset Password sedang diproses...')
            ->line('Link reset sudah dikirim ke email Anda.');
    }
}
