<?php

namespace Entity\Model;

use Helpers\QueryBuilder\QueryBuilder;

class Memos extends QueryBuilder
{
    protected string $table = 'MEMOS with (nolock)';
    protected string $primaryKey = 'NODEMEMO';

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

    /**
     * @param string|int $memoId
     * @return int
     */
    public function clone($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;
    }
}
