<?php

namespace App\Http\Controllers\Api\Channels;

use App\Domains\Channels\Models\WebhookEvent;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;

class ChannelWebhookLogController extends Controller
{
    public function __invoke(string $channel): JsonResponse
    {
        $rows = WebhookEvent::query()
            ->where('channel_account_id', $channel)
            ->orderByDesc('created_at')
            ->get()
            ->map(fn (WebhookEvent $event) => [
                'id' => $event->id,
                'source' => $event->source,
                'eventType' => $event->event_type,
                'status' => $event->processing_status,
                'retriesCount' => $event->retries_count,
                'errorMessage' => $event->error_message,
                'createdAt' => $event->created_at?->format('M d, H:i'),
            ])
            ->values();

        return response()->json(['data' => $rows]);
    }
}
