<?php

namespace Controllers;

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

class HarvestListController
{
    private HarvestModel $harvestModel;

    public function __construct()
    {
        $this->harvestModel = new HarvestModel();
    }

    public function __invoke(ServerRequestInterface $request)
    {
        try {
            $harvests = $this->harvestModel->getAll();
        } catch (\Throwable $e) {
            return ResponseLib::sendFail('Falha ao listar safra: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
        }

        if (!$harvests) {
            return ResponseLib::sendFail('Nenhuma safra encontrada', [], 'E_NOT_FOUND')->withStatus(204);
        }

        return ResponseLib::sendOk($harvests, 'S_HARVEST_LIST');
    }
}
