<?php

namespace Entity\Model;

use Helpers\QueryBuilder\QueryBuilder;
use Helpers\Trad\Trad;

class GrilQstR extends QueryBuilder
{
    

    protected string $table = 'GRIL_QST_R with (nolock)';
    protected string $primaryKey = '';

    

    /**
     * getArchitecture
     *
     * @return array<mixed>
     */
    public function getArchitecture(\Helpers\Trad\ITrad $trad): array
    {
        return  [
            'ID_QUEST' =>
            [
                'nomChamp' => $trad->t('Identifiant Question'),
                'type' => 'NUMBER',
                'obligatoire' => true
            ],
            'ID_REP' =>
            [
                'nomChamp' => $trad->t('Identifiant Réponse'),
                'type' => 'NUMBER',
                'obligatoire' => true
            ],
            'TXT_REP' =>
            [
                'nomChamp' => $trad->t('Texte réponse'),
                'type' => 'TEXTAREA',
                'taille' => 120,
                'obligatoire' => true
            ],
            'NM_COM' =>
            [
                'nomChamp' => $trad->t('Commentaire'),
                'type' => 'TEXT',
                'taille' => 3000,
                'obligatoire' => true
            ],
            'COM_MANUEL' =>
            [
                'nomChamp' => $trad->t('Commentaire manuel'),
                'type' => 'RADIO',
                'obligatoire' => true
            ],
            'NOTE' =>
            [
                'nomChamp' => $trad->t('Note'),
                'type' => 'TEXT',
                'taille' => 1,
                'obligatoire' => true
            ],
            'COM_OBLIG' =>
            [
                'nomChamp' => $trad->t('Commentaire obligatoire'),
                'type' => 'RADIO',
                'obligatoire' => true
            ],
        ];
    }
    
    // TODO : factorize with other QueryBuilder
    // Cascade delete MEMOS
    public function delete($id)
    {
        $inTrans = $this->pdo->inTransaction();
        if (!$inTrans) {
            $this->pdo->beginTransaction();
        }
        
        $param = [];
        $this->pdo->prepare(
            "DELETE MEMOS FROM MEMOS 
            JOIN GRIL_QST_R ON GRIL_QST_R.NM_COM = MEMOS.NODEMEMO
            WHERE " . $this->getDeleteWhere($id, $param)
        )
        ->execute($param);

        $res = parent::delete($id);
        
        if (!$inTrans) {
            $this->pdo->commit();
        }

        return $res;
    }
}
