<?php

namespace App\modele;

use App\gateway\AlumniGateway;
use App\gateway\Connection;
use App\gateway\ImageGateway;
use App\gateway\OffreGateway;
use App\metier\Alumni;
use App\metier\Offre;
use App\metier\Image;
use mysql_xdevapi\Exception;

class OffreModele
{

    private OffreGateway $offreGw;

    public function __construct()
    {
        $this->offreGw = new OffreGateway(new Connection("mysql:host=localhost;dbname=dbAlica", "test", "test"));
    }

    public function publishOffers(Image $img)
    {

        $desc = $_POST["description"];
        $descposte = $_POST["descriptPoste"];
        $nom = $_POST["name"];
        $ville = $_POST["ville"];
        $entreprise = $_POST["entreprise"];
        $profilRecherche = $_POST["profilRecherche"];
        $mail = $_POST["mail"];
        $num = $_POST["num"];
        $site = $_POST["site"];
        $exp = $_POST["choixExp"];
        $typeContrat = $_POST["typeContrat"];
        $niveauEtudes = $_POST["education"];

        if(isset($_POST["fullRemote"]))
        {
           $remote = true;
        }
        else $remote = false;
        // à la place de NULL passer id utilisateur créateur offre
        $offre = new Offre($this->offreGw->getMaxid(),
            new Alumni(12,"test.mail@icloud.fr","password","admin"),
            $nom,
            $desc,
            $img,
            $typeContrat,
            $ville,
            $entreprise,
            $descposte,
            $profilRecherche,
            $exp,
            $niveauEtudes,
            $mail,
            $num,
            $site,
            $remote);
            $this->offreGw->addOffers($offre);

            return $offre;

    }
    public function getOffers() : array
    {
        $res = $this->offreGw->getOffers();
        $offers = $this->CreateOffers($res);
        return $offers;
    }


    public function getOfferFromId(int $id) : ?Offre
    {
        try{
            $res = $this->offreGw->getOfferFromId($id);
            return $this->CreateOffers($res)[0];
        }
        catch (Exception)
        {
            return null;
        }
    }

    public function CreateOffers($res) : array
    {
        $alGw = new AlumniGateway(new Connection("mysql:host=localhost;dbname=dbAlica", "test", "test"));
        $imgGw = new ImageGateway(new Connection("mysql:host=localhost;dbname=dbAlica", "test", "test"));

        $offers=[];
        foreach ($res as $row)
        {
            $imgRaw = $imgGw->obtenirParId($row['image']);
            $img = new Image(intval($imgRaw[0]["id"]),$imgRaw[0]['nom'], $imgRaw[0]['taille'], $imgRaw[0]['type'], $imgRaw[0]['blob']);

            $resal = $alGw->ObtenirParId($row['offreur']);
            $alumni = new Alumni($resal[0]['id'],$resal[0]['mail'],$resal[0]['mdp'],$resal[0]['role']);

            $offers[]=new Offre($row['id'],
                $alumni,
                $row['titre'],
                $row['description'],
                $img,
                $row['typeContrat'],
                $row['ville'],
                $row["entreprise"],
                $row['descriptifPoste'],
                $row['profil'],
                $row['experience'],
                $row['niveauEtudes'],
                $row['mailContact'],
                $row['numero'],
                $row['websiteURL'],
                $row['remote']);
        }


        return $offers;
    }





}