<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Throwable;

class CountriesController extends Controller
{
    private $response_controller;
    private $encrypt_controller;
    public function __construct()
    {
        $this->response_controller = new ResponseController();
        $this->encrypt_controller = new EncryptionController();
    }

    public function getCountries($line_number)
    {
        try {

            // Busca si el subcontratista existe
            $countries = DB::table("S002V01TPAIS")
                ->select(
                    "PAIS_IDPA as COUNTRY_ID",
                    "PAIS_NOMB as NAME",
                    "PAIS_LADA as LADA",
                    "PAIS_NOME AS NOMECLARUTA_ISO2",
                    "PAIS_NOM2 AS NOMECLARUTA_ISO3"
                )
                ->where('PAIS_NULI', '=', $line_number)
                ->get();

            // Verifica si el objeto esta vacio
            if (!isset($countries[0]) && empty($countries[0])) {
                return $this->response_controller
                    ->makeResponse(TRUE, "ERR_SUBCONTRATISTA_REG001: No se encontró al subcontratista", $countries, 500);
            }

            return $this->response_controller->makeResponse(FALSE, 'Consulta exitosa', $countries);

        } catch (Throwable $th) {
            return $this->response_controller
                ->makeResponse(TRUE, 'ERR_SUBCONTRATISTA_REG005: Error inesperado', strtoupper($th->getMessage()), 500);
        }
    }

}