<?php

declare(strict_types=1);

namespace App\Notifications;

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

class StoryStatusNotification extends Notification
{
    use Queueable;

    public function __construct(
        private readonly Story $story,
        private readonly string $event,
        private readonly ?string $reason = null,
    ) {
    }

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

    public function databaseType(object $notifiable): string
    {
        return in_array($this->event, ['approved', 'published'], true)
            ? 'story_published'
            : 'story_status';
    }

    public function toDatabase(object $notifiable): array
    {
        $message = match ($this->event) {
            'approved' => 'Your story "' . $this->story->title . '" was approved and published.',
            'rejected' => 'Your story "' . $this->story->title . '" was rejected. Update it and resubmit for review.',
            'published' => 'Your story "' . $this->story->title . '" is now published.',
            default => 'Story update: "' . $this->story->title . '" status changed.',
        };

        return [
            'type' => in_array($this->event, ['approved', 'published'], true)
                ? 'story_published'
                : 'story.' . $this->event,
            'story_id' => $this->story->id,
            'title' => $this->story->title,
            'slug' => $this->story->slug,
            'status' => $this->story->status,
            'reason' => $this->reason,
            'message' => $message,
            'url' => route('creator.stories.edit', ['story' => $this->story->id]),
        ];
    }
}
