<?php

namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\User;
use Illuminate\Support\Facades\Mail;
use App\Mail\UserProvisionedMail;
use Illuminate\Support\Facades\Log;

class ProvisionUserJob implements ShouldQueue
{
    use Queueable, Dispatchable, InteractsWithQueue, SerializesModels;

    public function __construct(
        public User $user,
        public string $temporaryPassword
    ) {}

    public function handle(): void
    {
        // 1. Send the Email
        Mail::to($this->user->email)->send(
            new UserProvisionedMail($this->user, $this->temporaryPassword)
        );

        // 2. The WhatsApp Notification (Placeholder)
        if ($this->user->phone_number) {
            // Note: When you connect a service like Twilio or a local WhatsApp Business API,
            // the cURL/HTTP request to send the WhatsApp message goes right here!
            Log::info("WhatsApp message queued for: " . $this->user->phone_number);
        }
    }
}