<?php

class Seguro_Service_Car
{
    public static function isCarObrigatorio(int $idProposta)
    {
        $model = new Seguro_Model_Propostas();
        $idPropostaMae = $model->getIdPropostaMae($idProposta);
        $propostaMae = $model->find($idPropostaMae)->current();
        
        return $propostaMae->id_status == Seguro_Enum_PropostaStatus::ID_NAO_ENVIADA
            || $propostaMae->id_status == Seguro_Enum_PropostaStatus::ID_PROPOSTA_INCOMPLETA
            || strtotime(substr($propostaMae->dt_transmissao, 0, 10)) >= strtotime(DATA_CAR_OBRIGATORIO);
    }

    public function buscarItensTalhoesCar(array $campos, array $where): array
    {
        $dtosItensSegurados = $dtosItensSeguradosCar = $dtosTalhoes = [];
        $rows = (new Seguro_Model_ItensSegurados())->buscarDadosItensSeguradosTalhoes($campos, $where)->toArray();

        foreach ($rows as $row) {
            if (empty($dtosItensSegurados[$row['nr_item_segurado']])) {
                $dtosItensSegurados[$row['nr_item_segurado']] = (new Seguro_Dto_ItemSegurado())->deParaAtributos($row);
                $dtosTalhoes[$row['nr_item_segurado']] = (new Seguro_Dto_Talhao())->deParaAtributos($row);
            }
            
            $dtosItensSeguradosCar[$row['nr_item_segurado']][] = (new Seguro_Dto_ItemSeguradoCar())->deParaAtributos($row);
        }

        return [
            'itensSegurados' => $dtosItensSegurados,
            'itensSeguradosCar' => $dtosItensSeguradosCar,
            'talhoes' => $dtosTalhoes
        ];
    }
}