<?php
/**
 * BookiFlex NotificationLog Extension
 *
 * @package    BookiFlex
 * @subpackage NotificationLog
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Extensions\NotificationLog\Admin\Transformer;

use BookiFlex\Extensions\Notification\Model\Notification;

defined('ABSPATH') || exit;

class NotificationLogTransformer
{
    private static function eventLabels(): array
    {
        return [
            'new_reservation'        => __('New Reservation', 'bookiflex'),
            'waiting_confirmation'   => __('Waiting Confirmation', 'bookiflex'),
            'reservation_overdue'    => __('Overdue', 'bookiflex'),
            'reservation_confirmed'  => __('Confirmed', 'bookiflex'),
            'reservation_cancelled'  => __('Cancelled', 'bookiflex'),
            'pre_arrival'            => __('Pre-Arrival', 'bookiflex'),
            'post_departure'         => __('Post-Departure', 'bookiflex'),
        ];
    }

    public function transform(Notification $notification): array
    {
        $reservation = $notification->getReservation();
        $labels      = self::eventLabels();

        return [
            'sid'                => $notification->getSid(),
            'reservation_number' => $reservation ? $reservation->getNumber() : '—',
            'event'              => $labels[$notification->getEvent()] ?? $notification->getEvent(),
            'stakeholder'        => $notification->getStakeholder()->value,
            'status'             => (string) $notification->getStatus(),
            'send_time'          => $notification->getSendTime()?->format('Y-m-d H:i'),
            'attempt'            => $notification->getAttempts(),
            'has_snapshot'       => $notification->hasSnapshot(),
        ];
    }

    /**
     * @param Notification[] $notifications
     */
    public function transformCollection(array $notifications): array
    {
        return array_map(fn(Notification $n) => $this->transform($n), $notifications);
    }
}
