<?php

namespace App\Notifications;

use App\Models\Absenta;
use App\Notifications\Channels\WhatsAppChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class AbsentaAdaugata extends Notification
{
    use Queueable;

    public function __construct(public Absenta $absenta) {}

    public function via(object $notifiable): array
    {
        $channels = [];
        $clasa = $this->absenta->elev?->clasa;

        if ($clasa?->emailActiv() && $notifiable->email) {
            $channels[] = 'mail';
        }

        if ($clasa?->whatsappActiv() && $this->absenta->elev?->telefon_parinte) {
            $channels[] = WhatsAppChannel::class;
        }

        return $channels;
    }

    public function toMail(object $notifiable): MailMessage
    {
        $elev = $this->absenta->elev;
        $materie = $this->absenta->materie;

        return (new MailMessage)
            ->subject("Absență nouă — {$elev->nume} {$elev->prenume}")
            ->greeting("Bună ziua,")
            ->line("{$elev->nume} {$elev->prenume} a fost marcat/ă absent/ă la **{$materie->nume}**.")
            ->line("Data: {$this->absenta->data->format('d.m.Y')}")
            ->line("Status: " . ($this->absenta->motivata ? 'Motivată' : 'Nemotivată'))
            ->salutation('Cu respect, Catalogul Digital');
    }

    public function toWhatsApp(object $notifiable): string
    {
        $elev = $this->absenta->elev;
        $materie = $this->absenta->materie;

        return "⚠️ Absență: {$elev->nume} {$elev->prenume} — {$materie->nume} ({$this->absenta->data->format('d.m.Y')})";
    }
}
