<?php

namespace App\Http\Controllers\Api\Channels;

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

class ChannelIndexController extends Controller
{
    public function __invoke(): JsonResponse
    {
        $rows = ChannelAccount::query()
            ->where('tenant_id', request()->user()->tenant_id)
            ->orderBy('type')
            ->get()
            ->map(fn (ChannelAccount $account) => [
                'id' => $account->id,
                'type' => $account->type,
                'displayName' => $account->display_name,
                'status' => $account->status,
                'lastSyncAt' => $account->last_sync_at?->format('M d, H:i'),
                'externalAccountId' => $account->external_account_id,
                'externalBusinessId' => $account->external_business_id,
                'metadata' => $account->metadata_json,
            ])
            ->values();

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