<?php

class Simulator
{
    public PDO $dbh;

    public function __construct(Database $database)
    {
        $this->dbh = $database->conn;
    }

    // Return true if team exists, false if not
    public function insert(string $cardnumber): bool 
    {
        $sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE Kortnummer = ?');
        $sth->execute([$cardnumber]);

        $row = $sth->fetch(PDO::FETCH_ASSOC);
        if ($row)
        {
            // Team exists, insert into time table
            $sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)');
            $sth->execute([$row['LagID']]);
            return TRUE;
        }
        // Team does not exist, lets create it 
        $sth = $this->dbh->prepare(
            "INSERT INTO `lagtabell` (`LagID`, `LagNavn`, `Bedrift`, `Kortnummer`, `Lagleder`, `Telefon`, `Deltagere`, `Runder`, `Bestetid`) VALUES (NULL, 'NN', 'NN', ?, 'NN', '0', '0', '0', now())"
        );
        $sth->execute([$cardnumber]);
        return FALSE;
    }
}