<?php

namespace App\Notifications;

use App\Models\OperationRun;
use App\Support\OpsUx\OperationUxPresenter;
use App\Support\ReasonTranslation\ReasonPresenter;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;

class OperationRunCompleted extends Notification
{
    use Queueable;

    public function __construct(
        public OperationRun $run
    ) {}

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

    public function toDatabase(object $notifiable): array
    {
        $message = OperationUxPresenter::terminalDatabaseNotificationMessage($this->run, $notifiable);
        $reasonEnvelope = app(ReasonPresenter::class)->forOperationRun($this->run, 'notification');
        $baselineTruthChanged = data_get($this->run->context, 'baseline_capture.current_baseline_changed');

        if ($reasonEnvelope !== null) {
            $message['reason_translation'] = $reasonEnvelope->toArray();
            $message['diagnostic_reason_code'] = $reasonEnvelope->diagnosticCode();
        }

        if (is_bool($baselineTruthChanged)) {
            $message['baseline_truth_changed'] = $baselineTruthChanged;
        }

        return $message;
    }
}
