<?php

namespace Controllers;

use Libs\ResponseLib;
use Models\TokenModel;
use Psr\Http\Message\ServerRequestInterface;

class WalletTokensController
{
    private TokenModel $tokenModel;

    public function __construct()
    {
        $this->tokenModel = new TokenModel();
    }

    public function __invoke(ServerRequestInterface $request)
    {
        $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
        if ($companyId <= 0) {
            return ResponseLib::sendFail('Authenticated company not found', [], 'E_VALIDATE')->withStatus(401);
        }

        try {
            $tokens = $this->tokenModel->getByCompanyId($companyId);
        } catch (\Throwable $e) {
            return ResponseLib::sendFail('Failed to fetch wallet tokens: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
        }

        return ResponseLib::sendOk([
            'company_id' => $companyId,
            'tokens' => $tokens,
        ], 'S_WALLET_TOKENS');
    }
}
