<?php

namespace App\Notifications;

use App\Services\MobileDeepLink;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Arr;

class EngagementReminderNotification extends Notification
{
    use Queueable;

    private const MESSAGE_KEYS = [
        'publish_meal',
        'meal_photo',
        'evening_checkin',
        'community_inspiration',
        'workout_checkin',
    ];

    public function __construct(private readonly string $messageKey) {}

    public static function randomMessageKey(): string
    {
        return Arr::random(self::MESSAGE_KEYS);
    }

    /**
     * @return list<string>
     */
    public static function messageKeys(): array
    {
        return self::MESSAGE_KEYS;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['expo'];
    }

    /**
     * @return array{
     *     title: string,
     *     body: string,
     *     sound: string,
     *     channelId: string,
     *     data: array{type: string, message_key: string, url: string}
     * }
     */
    public function toExpoPush(object $notifiable): array
    {
        $message = $this->message();

        return [
            'title' => $message['title'],
            'body' => $message['body'],
            'sound' => 'default',
            'channelId' => 'default',
            'data' => [
                'type' => 'engagement_reminder',
                'message_key' => $this->messageKey,
                'url' => MobileDeepLink::to($this->path()),
            ],
        ];
    }

    /**
     * @return array{title: string, body: string}
     */
    private function message(): array
    {
        $message = trans("api.notifications.engagement_reminders.{$this->messageKey}");

        if (is_array($message)) {
            return $message;
        }

        $fallback = trans('api.notifications.engagement_reminders.publish_meal');

        return is_array($fallback)
            ? $fallback
            : [
                'title' => 'Bowli',
                'body' => 'Publie ton plat du jour quand tu as un moment.',
            ];
    }

    private function path(): string
    {
        return $this->messageKey === 'workout_checkin'
            ? 'profile/workouts?create=1'
            : 'create';
    }
}
