<?php

namespace Controllers;

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

class CompanySummaryController
{
    private CompanySummaryModel $summaryModel;

    public function __construct()
    {
        $this->summaryModel = new CompanySummaryModel();
    }

    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 {
            $summary = $this->summaryModel->getSummary($companyId);
        } catch (\Throwable $e) {
            return ResponseLib::sendFail('Failed to fetch company summary: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
        }

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