<?php

namespace App\Notifications;

use App\Models\FormSubmission;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;

class FormSubmissionActivityNotification extends Notification
{
    use Queueable;

    public function __construct(
        public FormSubmission $submission,
        public string $action,
    ) {}

    public function via(object $notifiable): array
    {
        return ['database'];
    }

    public function toDatabase(object $notifiable): array
    {
        $this->submission->loadMissing(['form', 'filiale', 'user']);

        $formTitle = $this->submission->form->title;
        $filialeName = $this->submission->filiale->name;

        if ($this->action === 'updated') {
            $title = 'Formulaire modifié';
            $message = "La filiale {$filialeName} a modifié sa réponse au formulaire « {$formTitle} ».";
        } else {
            $title = 'Nouvelle soumission';
            $message = "La filiale {$filialeName} a soumis le formulaire « {$formTitle} ».";
        }

        return [
            'submission_id' => $this->submission->id,
            'form_id' => $this->submission->form_id,
            'filiale_id' => $this->submission->filiale_id,
            'action' => $this->action,
            'title' => $title,
            'message' => $message,
            'url' => route('admin.submissions.show', $this->submission),
        ];
    }
}
