<?php

declare(strict_types=1);

namespace App\Notifications\Findings;

use App\Models\Finding;
use App\Models\ManagedEnvironment;
use App\Support\OpsUx\OperationUxPresenter;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;

final class FindingEventNotification extends Notification
{
    use Queueable;

    /**
     * @param  array<string, mixed>  $event
     */
    public function __construct(
        private readonly Finding $finding,
        private readonly ManagedEnvironment $tenant,
        private readonly array $event,
    ) {}

    /**
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['database'];
    }

    /**
     * @return array<string, mixed>
     */
    public function toDatabase(object $notifiable): array
    {
        $message = OperationUxPresenter::findingDatabaseNotificationMessage(
            $this->finding,
            $this->tenant,
            $this->event,
        );

        $message['finding_event'] = [
            'event_type' => (string) ($this->event['event_type'] ?? ''),
            'finding_id' => (int) $this->finding->getKey(),
            'recipient_reason' => data_get($this->event, 'metadata.recipient_reason'),
            'fingerprint_key' => (string) ($this->event['fingerprint_key'] ?? ''),
            'due_cycle_key' => $this->event['due_cycle_key'] ?? null,
            'tenant_name' => $this->tenant->getFilamentName(),
            'severity' => (string) ($this->event['severity'] ?? ''),
        ];

        return $message;
    }
}
