<?php
class Departamento
{
    public $codigoDepartamento;
    public $nombreDepartamento;

    public function __construct()
    {
    }

    public static function obtenerTodos($db = null): object
    {
        try {
            if (empty($db)) $db = obtenerConexion();

            $query = "SELECT * FROM general.view_departamentos;";
            $stmt = $db->prepare($query, [PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL]);
            $stmt->execute();

            $data = $stmt->fetchAll(PDO::FETCH_CLASS, __CLASS__);

            return (object)['status' => 200, 'data' => $data];
        } catch (PDOException $e) {
            return (object)[
                'status' => 500,
                'message' => $e->getMessage()
            ];
        } catch (Exception $e) {
            return (object)[
                'status' => 500,
                'message' => $e->getMessage()
            ];
        }
    }

    public static function obtenerPorPais($codigoPais, $db = null): object
    {
        try {
            if (empty($db)) $db = obtenerConexion();

            $codigoPais > 1 && $codigoPais = 0;

            $query = "SELECT * FROM general.view_departamentos WHERE codigopais = :codigopais;";
            $stmt = $db->prepare($query, [PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL]);
            $stmt->bindParam(":codigopais", $codigoPais, PDO::PARAM_INT);

            $stmt->execute();

            $data = $stmt->fetchAll(PDO::FETCH_CLASS, __CLASS__);

            return (object)['status' => 200, 'data' => $data];
        } catch (PDOException $e) {
            return (object)[
                'status' => 500,
                'message' => $e->getMessage()
            ];
        } catch (Exception $e) {
            return (object)[
                'status' => 500,
                'message' => $e->getMessage()
            ];
        }
    }
}
