<?php

namespace Entity\Model;

use Helpers\QueryBuilder\QueryBuilder;

class Memos extends QueryBuilder
{
    protected \PDO $pdo;
    
    protected string $table = 'MEMOS with (nolock)';
    
    protected string $primaryKey = 'NODEMEMO';
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    /**
     * @param int|string $id
     */
    public function getValue($id) :?string
    {
        if (empty($id)) {
            return null;
        }
        return $this->find($id)['CONTMEMO'] ?? null;
    }

    public function clone(int $memoId) :int
    {
        $sql = new \Helpers\Outil\Sql();

        // Case 'NULL'
        if (intval($memoId) == 0) {
            return 0;
        }

        // TODO : merge request
        $newId = $sql->generateNum($this->pdo);
        $req = "INSERT INTO MEMOS(NODEMEMO, CONTMEMO) VALUES(:id, (
            SELECT CONTMEMO
            FROM MEMOS
            WHERE NODEMEMO = :originalId
        ))";
        
        $memos = $this->pdo->prepare($req);
        $memos->execute([
            ':originalId' => $memoId,
            ':id' => $newId,
        ]);

        return (int)$newId;
    }
}
