<?php

namespace App\Teamtable;

use \PDO;

/**
 * Normally this kind of logic will be implemented using the Repository pattern.
 * However the important part is in mapRowToTeam(), that will create a business object from the
 * data fetched from database
 */
class TeamMapper
{
    public PDO $dbh;

    public function __construct(PDO $dbh)
    {
        $this->dbh = $dbh;
    }

    private function mapRowToTeam(array $row): Team
    {
        $team = new Team();
        $team->id = $row['LagID'];
        $team->setName($row['LagNavn']);
        $team->setCompany($row['Bedrift']);
        $team->setCardnumber($row['Kortnummer']);
        $team->setLeader($row['Lagleder']);
        $team->setPhone($row['Telefon']);
        $team->setParticipants($row['Deltagere']);
        $team->setRounds($row['Runder']);
        $team->best_time = $row['Bestetid'];
        return $team;
    }

    /**
     * Returns an array of all teams
     */
    public function getAll(): array
    {
        $sth = $this->dbh->query('SELECT * FROM lagtabell');
        $assoc_array = $sth->fetchAll(PDO::FETCH_ASSOC);

        $teams = [];
        foreach ($assoc_array as $key => $row)
        {
            array_push($teams, $this->mapRowToTeam($row));
        }
        return $teams;
        #while ($assoc_array)
        #{
        #    array_push($teams, $this->mapRowToTeam($assoc_array[0]));
        #    array_pop($assoc_array);
        #}
        #return $teams;
    }

    public function getByCardnumber(string $cardnumber): ?Team
    {
        $sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE Kortnummer = ? LIMIT 1');
        $sth->execute([$cardnumber]);
        $row = $sth->fetch(PDO::FETCH_ASSOC);
        if ($row)
        {
            return $this->mapRowToTeam($row);
        }
        return NULL;
    }

    public function get(int $id): ?Team
    {
        $sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE LagID = ? LIMIT 1');
        $sth->execute([$id]);
        $row = $sth->fetch(PDO::FETCH_ASSOC);
        if ($row)
        {
            return $this->mapRowToTeam($row);
        }
        return NULL;
    }

    public function create(Team $team): Team
    {
        $sth = $this->dbh->prepare(
            'INSERT INTO lagtabell
                (LagNavn, Bedrift, Kortnummer, Lagleder, Telefon, Deltagere, Runder, Bestetid)
            VALUES
                (?, ?, ?, ?, ?, ?, ?, ?)'
        );
        $sth->execute([
            $team->name,
            $team->company,
            $team->cardnumber,
            $team->leader,
            $team->phone,
            $team->participants,
            $team->rounds,
            $team->best_time
        ]);
        $last_id = $this->dbh->lastInsertId();
        return $this->get($last_id);
    }

    public function update(Team $team): Team
    {
        $sth = $this->dbh->prepare(
            'UPDATE lagtabell SET
                LagNavn = ?, Bedrift = ?, Kortnummer = ?,
                Lagleder = ?, Telefon = ?, Deltagere = ?,
                Runder = ?, Bestetid = ?
            WHERE
                LagID = ?'
        );
        $sth->execute([
            $team->name,
            $team->company,
            $team->cardnumber,
            $team->leader,
            $team->phone,
            $team->participants,
            $team->rounds,
            $team->best_time,
            $team->id
        ]);
        return $this->get($team->id);
    }

    public function delete(int $id): void
    {
        $sth = $this->dbh->prepare('DELETE FROM lagtabell WHERE LagID = ?');
        $sth->execute([$id]); 
    }
}