<?php

namespace App\Services\Notifications;

use App\Models\Notification;

class NotificationActiveService
{
    public function list(?string $targetGroup): array
    {
        $rows = Notification::getActiveNotifications($targetGroup);
        $now = time();

        $notifications = array_map(static function ($row) use ($now) {
            $expiresAt = $row['expires_at'] ?? null;
            $expiryTs = $expiresAt ? strtotime((string) $expiresAt) : false;
            $isExpired = $expiryTs !== false && $expiryTs <= $now;

            return [
                'id' => $row['id'] ?? null,
                'title' => $row['title'] ?? null,
                'message' => $row['message'] ?? null,
                'priority' => $row['priority'] ?? null,
                'scheduled_at' => $row['scheduled_at'] ?? null,
                'expires_at' => $expiresAt,
                'created_at' => $row['created_at'] ?? null,
                'target_group' => $row['target_group'] ?? null,
                'isExpired' => $isExpired,
            ];
        }, $rows ?? []);

        return ['notifications' => $notifications];
    }
}
