<?php

namespace App\Modules\Notifications\Jobs;

use App\Models\User;
use App\Models\UserNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

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

    public function __construct(
        public string $userId,
        public string $type,
        public string $title,
        public string $message,
        public string $channel = 'database',
        public string $priority = 'normal',
        public array $data = []
    ) {}

    public function handle(): void
    {
        UserNotification::create([
            'user_id' => $this->userId,
            'type' => $this->type,
            'title' => $this->title,
            'message' => $this->message,
            'channel' => $this->channel,
            'priority' => $this->priority,
            'data' => $this->data,
        ]);

        if ($this->channel === 'email') {
            $user = User::find($this->userId);
            if ($user) {
                // Mail::to($user)->send(new NotificationMail($this->title, $this->message));
            }
        }
    }
}
