<?php
namespace API\InfoLegale\Traits;

trait Financial
{
    
    /**
     * @return array<mixed>
     */
    public function balances(string $siren, string $country)
    {

        try {
            $response = $this->client->get($this->urlApi.'companies/'.$country.'/'.$siren.'/financial/balances', [
            
                    'headers' => [
                        'Authorization' => 'Bearer '. $this->token
                    ],

                ]);

            $this->code = $response->getStatusCode();
            $this->header = $response->getHeaderLine('content-type');
            
            $return =  json_decode($response->getBody()->getContents(), true);

            foreach ($return['balances'] as $key => $value) {
                $return['balances'][$key]['returnBalance'] = $this->callBack($value['ratios']['@link']);
            }
            return $return;
        } catch (\Exception $e) {
            return [];
        }
    }
    
    /**
     * @return array<mixed>
     */
    public function balancesSoceial(string $siren, string $country, ?string $date = null)
    {
        
        if ($date == null) {
            return [];
        }

        $response = $this->client->get($this->urlApi.'companies/'.$country.'/'.$siren.'/financial/balance/'.$date.'/social', [
           
                'headers' => [
                    'Authorization' => 'Bearer '. $this->token
                ],

            ]);

        $this->code = $response->getStatusCode();
        $this->header = $response->getHeaderLine('content-type');
        
        return  json_decode($response->getBody()->getContents(), true);

        // foreach ($return['balances'] as $key => $value) {
        //     $return['balances'][$key]['returnBalance'] = $this->callBack($value['ratios']['@link']);
        // }
        // return $return;
    }

    /**
     * @return array<mixed>
     */
    public function ratios(string $siren, string $country, string $closingDate, string $type = 'social')
    {
        
        $response = $this->client->get($this->urlApi.'companies/'.$country.'/'.$siren.'/financial/balances'.$closingDate.'/'.$type.'/ratios', [
           
                'headers' => [
                    'Authorization' => 'Bearer '. $this->token
                ],

            ]);

        $this->code = $response->getStatusCode();
        $this->header = $response->getHeaderLine('content-type');
        
        return json_decode($response->getBody()->getContents(), true);
    }
}
