<?php
/*
 * Copyright (c) 2022. Micha Espey <tracer@24unix.net>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 */

namespace App\Repository;

use App\Service\DatabaseConnection;
use App\Entity\User;
use PDO;
use PDOException;

/**
 * Handles CRUD od User class.
 */
class UserRepository
{
    public function __construct(private readonly DatabaseConnection $databaseConnection)
    {
        // empty body
    }


    /**
     * @return array
     */
    public function findAll(string $orderBy = 'nick'): array
    {
        $users = [];
        $sql = "
            SELECT id, nick, first, last, is_admin
            FROM " . DatabaseConnection::TABLE_USERS . "
            ORDER BY :order";

        try {
            $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
            $statement->bindParam(param: ':order', var: $order);

            $statement->execute();
            while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
                $user = new User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']);
                $users[] = $user;
            }
            return $users;
        } catch (PDOException $e) {
            exit($e->getMessage());
        }
    }


    public function findByID(int $id): ?User
    {
        $sql = "
            SELECT id, nick, first, last, is_admin
            FROM " . DatabaseConnection::TABLE_USERS . "
            WHERE id = :id";

        try {
            $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
            $statement->bindParam(param: ':id', var: $id);
            $statement->execute();
            if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
                return new User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']);
            } else {
                return null;
            }
        } catch (PDOException $e) {
            exit($e->getMessage());
        }
    }

    public function findByNick(string $nick): ?User
    {
        $sql = "
            SELECT id, nick, first, last, is_admin
            FROM " . DatabaseConnection::TABLE_USERS . "
            WHERE nick = :nick";

        try {
            $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
            $statement->bindParam(param: ':nick', var: $nick);
            $statement->execute();
            if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
                return new User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']);
            } else {
                return null;
            }
        } catch (PDOException $e) {
            exit($e->getMessage());
        }
    }

    public function insert(User $user): bool|string
    {
    /*
        $sql = "
            INSERT INTO " . DatabaseConnection::TABLE_USERS . " (name, panel)
            VALUES (:name, :panel)";

        try {
            $name = $domain->getName();
            $panel = $domain->getPanel();
            $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
            $statement->bindParam(param: ':name', var: $name);
            $statement->bindParam(param: ':panel', var: $panel);
            $statement->execute();

            return $this->databaseConnection->getConnection()->lastInsertId();
        } catch (PDOException $e) {
            exit($e->getMessage());
        }
    */
        return false;
    }


    public function update(User $user): bool|int
    {
        $id = $user->getId();
        $current = $this->findByID(id: $id);

        /*
        if (empty($domain->getName())) {
            $name = $current->getName();
        } else {
            $name = $domain->getName();
        }
        if (empty($domain->getPanel())) {
            $panel = $current->getPanel();
        } else {
            $panel = $domain->getPanel();
        }

        $sql = "
			UPDATE " . DatabaseConnection::TABLE_USER . " SET
				name = :name,
				panel = :panel
            WHERE id = :id";

        try {
            $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
            $statement->bindParam(param: 'id', var: $id);
            $statement->bindParam(param: 'name', var: $name);
            $statement->bindParam(param: 'panel', var: $panel);
            $statement->execute();

            return $statement->rowCount();
        } catch (PDOException $e) {
            echo $e->getMessage();
            return false;
        }
        */
        return false;
    }


    public function delete(User $user): int
    {
        $sql = "
            DELETE FROM " . DatabaseConnection::TABLE_USERS . "
            WHERE id = :id";

        try {
            $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
            $id = $user->getId();
            $statement->bindParam(param: 'id', var: $id);
            $statement->execute();

            return $statement->rowCount();
        } catch (PDOException $e) {
            exit($e->getMessage());
        }
    }

}