<?php declare(strict_types=1);

namespace App\Repository;

use App\Controller\DatabaseConnection;
use App\Entity\DynDNS;
use Monolog\Logger;
use PDO;
use PDOException;

/**
 *
 */
class DynDNSRepository
{
	public function __construct(
		private readonly DatabaseConnection $databaseConnection,
		private readonly Logger             $logger)
	{
		$this->logger->debug(message: "DynDNSRepository::__construct()");
	}
	
	/**
	 * @param \App\Entity\DynDNS $dynDNS
	 *
	 * @return int
	 */
	public function delete(DynDNS $dynDNS): int
	{
		$dynDNSName = $dynDNS->getName();
		$this->logger->debug(message: "delete($dynDNSName)");
		
		$sql = "
            DELETE FROM " . DatabaseConnection::TABLE_DYNDNS . "
            WHERE id = :id";
		
		try {
			$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
			$id = $dynDNS->getId();
			$statement->bindParam(param: 'id', var: $id);
			$statement->execute();
			
			return $statement->rowCount();
		} catch (PDOException $e) {
			exit($e->getMessage());
		}
	}
	
	/**
	 * @return array
	 */
	public function findAll(): array
	{
		$this->logger->debug(message: "findAll()");
		
		$dyndns = [];
		$sql = "
            SELECT id, name, a, aaaa
            FROM " . DatabaseConnection::TABLE_DYNDNS . "
            ORDER BY name";
		
		try {
			$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
			$statement->execute();
			while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
				$dyndns = new DynDNS(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], id: $result['id']);
				$dyndns[] = $dyndns;
			}
			return $dyndns;
		} catch (PDOException $e) {
			exit($e->getMessage());
		}
	}
	
	/**
	 * @param String $name
	 *
	 * @return \App\Entity\Domain|bool
	 */
	public function findByName(string $name): DynDNS|bool
	{
		$this->logger->debug(message: "findByName($name)");
		
		$sql = "
			SELECT id, name, a, aaaa
			FROM " . DatabaseConnection::TABLE_DYNDNS . "
            WHERE name = :name";
		
		try {
			$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
			$statement->bindParam(param: ':name', var: $name);
			$statement->execute();
			if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
				return new DynDNS(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], id: $result['id']);
			} else {
				return false;
			}
		} catch (PDOException $e) {
			exit($e->getMessage());
		}
	}
	
	/**
	 * @param String $field
	 *
	 * @return int
	 */
	public function getLongestEntry(string $field): int
	{
		$sql = "
            SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DYNDNS;
		
		try {
			$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
			$statement->execute();
			$result = $statement->fetch();
			return $result['length'];
		} catch (PDOException $e) {
			exit($e->getMessage());
		}
	}
	
	/**
	 * @param \App\Entity\DynDNS $dynDNS
	 *
	 * @return string|false
	 */
	public function insert(DynDNS $dynDNS): bool|string
	{
		$dynDNSName = $dynDNS->getName();
		$this->logger->debug(message: "insert($dynDNSName)");
		
		$sql = "
            INSERT INTO " . DatabaseConnection::TABLE_DYNDNS . " (name, a, aaaa)
            VALUES (:name, :a, :aaaa)";
		
		try {
			$a = $dynDNS->getA();
			$aaaa = $dynDNS->getAaaa();
			
			$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
			$statement->bindParam(param: ':name', var: $dynDNSName);
			$statement->bindParam(param: ':a', var: $a);
			$statement->bindParam(param: ':aaaa', var: $aaaa);
			$statement->execute();
			
			return $this->databaseConnection->getConnection()->lastInsertId();
		} catch (PDOException $e) {
			exit($e->getMessage());
		}
	}
	
	/**
	 * @param \App\Entity\DynDNS $dynDNS
	 *
	 * @return false|int
	 */
	public function update(DynDNS $dynDNS): bool|int
	{
		$dynDNSnName = $dynDNS->getName();
		
		$this->logger->debug(message: "update($dynDNSnName)");
		
		$id = $dynDNS->getId();
		$current = $this->findByID(id: $id);
		
		if (empty($dynDNSnName)) {
			$name = $current->getName();
		} else {
			$name = $dynDNSnName;
		}
		if (empty($dynDNS->getA())) {
			$a = $current->getA();
		} else {
			$a = $dynDNS->getA();
		}
		if (empty($dynDNS->getAaaa())) {
			$aaaa = $current->getAaaa();
		} else {
			$aaaa = $dynDNS->getAaaa();
		}
		
		
		$sql = "
			UPDATE " . DatabaseConnection::TABLE_DYNDNS . " SET
				name = :name,
				a = :a,
				aaaa = :aaaa
            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: 'a', var: $a);
			$statement->bindParam(param: 'aaaa', var: $aaaa);
			$statement->execute();
			
			return $statement->rowCount();
		} catch (PDOException $e) {
			echo $e->getMessage();
			return false;
		}
	}
	
	/**
	 * @param int $id
	 *
	 * @return bool|\App\Entity\Domain
	 */
	public function findByID(int $id): bool|DynDNS
	{
		$this->logger->debug(message: "findById($id)");
		
		$sql = "
			SELECT id, name, a, aaaa
			FROM . " . DatabaseConnection::TABLE_DYNDNS . "
            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 DynDNS(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], id: $result['id']);
			} else {
				return false;
			}
		} catch (PDOException $e) {
			exit($e->getMessage());
		}
	}
}
