<?php
namespace Incoviba\Controller\Ventas;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Common\Ideal;
use Incoviba\Exception\ServiceAction;
use Incoviba\Service;

class Promotions extends Ideal\Controller
{
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view,
                             Service\Venta\Promotion $promotionService): ResponseInterface
    {
        $promotions = $promotionService->getAll('description');

        return $view->render($response, 'ventas.promotions', ['promotions' => $promotions]);
    }
    public function show(ServerRequestInterface $request, ResponseInterface $response, View $view,
                         Service\Venta\Promotion $promotionService, Service\Proyecto $proyectoService,
                         Service\Proyecto\Broker $brokerService,
                         int $promotion_id): ResponseInterface
    {
        $promotion = null;
        try {
            $promotion = $promotionService->getById($promotion_id);
        } catch (ServiceAction\Read) {}
        $projects = $proyectoService->getVendibles('descripcion');
        $brokers = $brokerService->getAll('name');
        return $view->render($response, 'ventas.promotions.show', ['promotion' => $promotion,
            'projects' => $projects, 'brokers' => $brokers]);
    }
}
