<?php

namespace Database;
use Model\User;
use \PDO;
use \DateTime;
use Model\Role;
use Model\Coach;
use Shared\Log;

class CoachMapper {
    public function coachSqlToEntity(array $data): array {
        $coachEntities = [];

        foreach ($data as $coachData) {
            $coach = new CoachEntity();
            if (isset($coachData['idathlete'])) {
                $coach->setIdAthlete($coachData['idathlete']);
            }

            if (isset($coachData['nom'])) {
                $coach->setNom($coachData['nom']);
            }

            if (isset($coachData['prenom'])) {
                $coach->setPrenom($coachData['prenom']);
            }

            if (isset($coachData['email'])) {
                $coach->setEmail($coachData['email']);
            }

            if (isset($coachData['sexe'])) {
                $coach->setSexe($coachData['sexe']);
            }

            if (isset($coachData['taille'])) {
                $coach->setTaille($coachData['taille']);
            }

            if (isset($coachData['poids'])) {
                $coach->setPoids($coachData['poids']);
            }

            if (isset($coachData['motdepasse'])) {
                $coach->setMotDePasse($coachData['motdepasse']);
            }

            if (isset($coachData['datenaissance'])) {
                $coach->setDateNaissance($coachData['datenaissance']);
            }

            if (isset($coachData['iscoach'])) {
                $coach->setIsCoach($coachData['iscoach']);
            }

            $coachEntities[] = $coach;
        }

        return $coachEntities;
    }
    
    public function coachEntityToModel(CoachEntity $coachEntity): User {
        $role = new Coach(); 
        $date = new DateTime($coachEntity->getDateNaissance());
        $user = new User(
            $coachEntity->getIdAthlete(),
            $coachEntity->getNom(),
            $coachEntity->getPrenom(),
            "myUsername",
            $coachEntity->getEmail(),
            $coachEntity->getMotDePasse(),
            $coachEntity->getSexe(),
            $coachEntity->getTaille(),
            $coachEntity->getPoids(),
            $date,
            $role
        );

        return $user;
    }

    public function coachToEntity(User $user):CoachEntity{

        $coach = new CoachEntity();
        $coach->setIdAthlete($user->getId());
        $coach->setNom($user->getNom());
        $coach->setPrenom($user->getPrenom());
        $coach->setEmail($user->getEmail());
        $coach->setSexe($user->getSexe());
        $coach->setTaille($user->getTaille());
        $coach->setPoids($user->getPoids());
        $coach->setMotDePasse($user->getMotDePasse());
        $coach->setDateNaissance($user->getDateNaissance());
        $coach->setIsCoach(TRUE);

        return $coach;
    }

}

?>

