<?php

namespace App\DAO;

use App\DAO\DAO;
use App\DAO\DAOResponse\DAOResponse;
use App\DAO\DAOResponse\DAOResponseInterface;

class SerialDAO extends DAO {

	public function getNewSerialUUID(): DAOResponse {

		$daoResponse = new DAOResponse();

		$sql = <<<SQL
			select uuid() as seria_uuid
		SQL;

		$conn = db_connect();
		$result = $conn->query( $sql )
			->getResultArray();
		$errors = $conn->error();

		if ($errors ['code'] != 0) {
			$daoResponse->setStatus( DAOResponseInterface::DATABASE_CONNECTION_ERROR );
			$daoResponse->setErrors( $errors );
			$daoResponse->setData( [ ] );
			log_message( "ERROR", json_encode( $errors ) );
		} else {
			if (count( $result ) == 0) {
				$daoResponse->setStatus( DAOResponseInterface::RECORD_NOT_FOUND );
				$daoResponse->setData( [ ] );
			} else {
				$daoResponse->setStatus( DAOResponseInterface::SUCESS );
				$daoResponse->setData( $result [0] );
			}
		}

		return $daoResponse;

	}

}

