<?php
namespace App\gateway;

use App\metier\Alumni;
use App\metier\Offre;
class OffreGateway
{
    private \App\gateway\Connection $con;

    /**
     * @param $con
     */
    public function __construct(\App\gateway\Connection $con){
        $this->con = $con;
    }

    public function getMaxid() : int
    {
        $query = 'SELECT MAX(id) FROM Offre';
        $this->con->executeQuery($query, array());
        $res = $this->con->getResults();
        if($res[0]['MAX(id)'] == null )
            return 1;
        else
        return intval($res[0]['MAX(id)'])+1;
    }

    public function addOffers(Offre $offre)
    {
        $query = 'INSERT INTO Offre VALUES (:i, :o, :t, :d, :img, :ty, :v, :e, :desc, :pro, :exp, :niv, :mail, :num, :web, :remote)';
        $this->con->executeQuery($query, array(
            ':i' => array($offre->getId(), \PDO::PARAM_INT),
            ':o' => array($offre->getOffreurId(), \PDO::PARAM_STR),
            ':t' => array($offre->getNom(), \PDO::PARAM_STR),
            ':d' => array($offre->getDescription(), \PDO::PARAM_STR),
            'img' => array($offre->getImgId(), \PDO::PARAM_INT),
            ':ty' => array($offre->getTypeContrat(), \PDO::PARAM_STR),
            ':v' => array($offre->getVille(), \PDO::PARAM_STR),
            ':e' => array($offre->getEntreprise(), \PDO::PARAM_STR),
            ':desc' => array($offre->getDescriptifPoste(), \PDO::PARAM_STR),
            ':pro' => array($offre->getProfil(), \PDO::PARAM_STR),
            ':exp' => array($offre->getExperience(), \PDO::PARAM_STR),
            ':niv' => array($offre->getNiveauEtudes(), \PDO::PARAM_STR),
            ':mail' => array($offre->getMailContact(), \PDO::PARAM_STR),
            ':num' => array($offre->getNumero(), \PDO::PARAM_STR),
            ':web' => array($offre->getSiteUrl(), \PDO::PARAM_STR),
            ':remote' => array($offre->isRemote(), \PDO::PARAM_BOOL)
        ));
    }


    public function  getOffers() : array
    {
        $query = 'SELECT * FROM offre';
        $this->con->executeQuery($query, array());
        return $this->con->getResults();
    }

    public function getOfferFromId($id) : array
    {
        $query = "SELECT * FROM offre WHERE id=:id";
        $this->con->executeQuery($query, array(
            ':id' => array($id, \PDO::PARAM_INT)
        ));
        return $this->con->getResults();
    }
}