<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class BiometricConnection extends Notification
{
    use Queueable;

    private array $biometricNotification;

    /**
     * Create a new notification instance.
     */
    public function __construct(array $biometricNotification)
    {
        $this->biometricNotification = $biometricNotification;
    }

    /**
     * Get the notification's delivery channels.
     */
    public function via(object $notifiable): array
    {
        return ['mail', 'database'];
    }

    /**
     * Get the mail representation of the notification.
     */
    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
            ->view('emails.biometric', [
                'headerImageUrl' => asset('images/AUTOHUB25_BANNER.png'),
                'msg' => $this->generateMessage(),
                'notifiable' => $notifiable,
            ])
            ->subject('Biometric Notification');
    }

    /**
     * Get the array representation of the notification.
     */
    public function toArray(object $notifiable): array
    {
        return [
            'success' => $this->biometricNotification['success'] ?? null,
            'failure' => $this->biometricNotification['failure'] ?? null,
        ];
    }

    /**
     * Generate the notification message.
     */
    private function generateMessage(): string
    {
        $success = $this->biometricNotification['success'] ?? [];
        $failure = $this->biometricNotification['failure'] ?? [];

        $message = "<h3>Biometric Operation Report Dated: ".date('Y-m-d')."</h3>";

        // Success Section
        $message .= "<h4 style='color: green;'>Success:</h4>";
        if (!empty($success)) {
            $message .= "<ul>";
            foreach ($success as $successData) {
                $deviceName = $successData['device_name'] ?? 'Unknown Device';
                $ipAddress = $successData['ipaddress'] ?? 'Unknown IP';
                $message .= "<li>Device: <b>$deviceName</b> | IP: <b>$ipAddress</b></li>";
            }
            $message .= "</ul>";
        } else {
            $message .= "<p>No successful operations reported.</p>";
        }

        // Failure Section
        $message .= "<h4 style='color: red;'>Failed:</h4>";
        if (!empty($failure)) {
            $message .= "<ul>";
            foreach ($failure as $failureData) {
                $deviceName = $failureData['device_name'] ?? 'Unknown Device';
                $ipAddress = $failureData['ipaddress'] ?? 'Unknown IP';
                $message .= "<li>Device: <b>$deviceName</b> | IP: <b>$ipAddress</b></li>";
            }
            $message .= "</ul>";
        } else {
            $message .= "<p>No failed operations reported.</p>";
        }

        return $message;
    }

}
