<?php
namespace Incoviba\Controller\Proyectos;

use Psr\Log\LoggerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Repository;
use Incoviba\Service;

class Brokers
{
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, LoggerInterface $logger,
                             Service\Proyecto\Broker $brokerService, Repository\Proyecto $proyectoRepository,
                             View $view): ResponseInterface
    {
        $projects = [];
        try {
            $projects = $proyectoRepository->fetchAll('descripcion');
        } catch (EmptyResult $exception) {
            $logger->error($exception);
        }
        $brokers = $brokerService->getAll();
        return $view->render($response, 'proyectos.brokers', compact('brokers', 'projects'));
    }
    public function show(ServerRequestInterface $request, ResponseInterface $response, LoggerInterface $logger,
                         Service\Proyecto\Broker $brokerService, Repository\Proyecto $proyectoRepository,
                         View $view, int $broker_rut): ResponseInterface
    {
        $broker = null;
        try {
            $broker = $brokerService->get($broker_rut);
        } catch (Read $exception) {
            $logger->error($exception);
        }
        $projects = [];
        try {
            $projects = $proyectoRepository->fetchAll('descripcion');
        } catch (EmptyResult $exception) {
            $logger->error($exception);
        }
        return $view->render($response, 'proyectos.brokers.show', compact('broker', 'projects'));
    }
}
