<?php

namespace Entity\Model;

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

class GrilTheme extends QueryBuilder
{
    protected string $table = 'GRIL_THEME with (nolock)';
    protected string $primaryKey = '';
    
    /**
     * getArchitecture
     *
     * @return array<mixed>
     */
    public function getArchitecture(\Helpers\Trad\ITrad $trad) : array
    {

        return  [
            'ID_MODEL' =>
            [
                'nomChamp' => $trad->t('Identifiant Modèle'),
                'type' => 'TEXT',
                'taille' => 10,
                'obligatoire' => true
            ],
            'ID_THEME' =>
            [
                'nomChamp' => $trad->t('Identifiant Thème'),
                'type' => 'TEXT',
                'taille' => 10,
                'obligatoire' => true
            ],
            'ID_PERE' =>
            [
                'nomChamp' => $trad->t('Identifiant Thème père'),
                'type' => 'TEXTAREA',
                'taille' => 10,
                'obligatoire' => true
            ],
            'LIB_THEME' =>
            [
                'nomChamp' => $trad->t('Libellé Thème'),
                'type' => 'TEXT',
                'taille' => 60,
                'obligatoire' => true
            ],
            'NM_COM' =>
            [
                'nomChamp' => $trad->t('Commentaire'),
                'type' => 'FLOAT',// actually a ref memos
                'obligatoire' => true
            ],
            'NM_ORDRE' =>
            [
                'nomChamp' => $trad->t('Position'),
                'type' => 'FLOAT',
                'obligatoire' => true
            ],
            'POIDS' =>
            [
                'nomChamp' => $trad->t('Poids'),
                'type' => 'FLOAT',
                'obligatoire' => true
            ],
            'THEME_LIE' =>
            [
                'nomChamp' => $trad->t('Thème lié'),
                'type' => 'TEXT',
                'taille' => 10,
                '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_THEME ON GRIL_THEME.NM_COM = MEMOS.NODEMEMO
            WHERE " . $this->getDeleteWhere($id, $param)
        )
        ->execute($param);

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

        return $res;
    }
}
