<?php
// +----------------------------------------------------------------------------------------+
// © 2002-2006 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
//
// Fichier en UTF-8
// +----------------------------------------------------------------------------------------+
// $Id: SchemeParser.php,v 1.2 2026/08/14 10:16:07 dbellamy Exp $

/* Documentation automatique de la base de données PMB
 * Source : scheme.json.zip (généré par build_scheme.php)
 */

error_reporting(E_ALL & ~E_NOTICE);

//require_once __DIR__ . '/../../vendor/autoload.php';

class SchemeParser
{

    private $t_table = [];
    private $t_parcours = [];
    private $t_att = [];
    private $t_relation = [];
    private $titre = "";
    public $ID;

    public function __construct()
    {
        $this->t_table    = [];
        $this->t_parcours = [];
        $this->t_att      = [];
        $this->t_relation = [];
        $this->titre      = "<h3>Base de donn&eacute;es PMB</h3>";
        $this->ID         = -1;
    }

    public function get_table()
    {
        return $this->t_table;
    }

    public function get_parcours()
    {
        return $this->t_parcours;
    }

    public function get_attribut()
    {
        return $this->t_att;
    }

    public function get_relation()
    {
        return $this->t_relation;
    }

    public function getTitre()
    {
        return $this->titre;
    }

    public function parseJsonFile()
    {
        $zip = new ZipArchive();
        $zip->open(__DIR__ . '/scheme.json.zip');
        $raw  = $zip->getFromName('scheme.json');
        $zip->close();
        $data = json_decode($raw, true);

        $this->titre = "<h3>" . htmlspecialchars($data['name'] ?? 'pmb') . "</h3><h5>" . date("d/m/Y") . "</h5>";

        // -- 1. Tables et champs ------------------------------------------
        foreach ($data['tables'] ?? [] as $table) {
            $tId = $table['name'];

            // Clé primaire depuis constraints ou indexes
            $pk = [];
            foreach ($table['constraints'] ?? [] as $c) {
                if (($c['type'] ?? '') === 'PRIMARY KEY' || ($c['name'] ?? '') === 'PRIMARY') {
                    $pk = $c['columns'] ?? [];
                    break;
                }
            }
            if (!$pk) {
                foreach ($table['indexes'] ?? [] as $ix) {
                    if (($ix['name'] ?? '') === 'PRIMARY') {
                        $pk = $ix['columns'] ?? [];
                        break;
                    }
                }
            }
            $pkSet = array_flip($pk);

            $this->t_table[$tId] = [
                'NAME'  => $tId,
                'DESC'  => $table['comment'] ?? '',
                'LIENS' => [],
                'KEY'   => implode(',', $pk),
                'ATTRS' => [],
            ];
            $this->t_parcours[$tId] = $tId;

            foreach ($table['columns'] ?? [] as $col) {
                $fName = $col['name'];
                $this->t_table[$tId]['ATTRS'][$fName] = [
                    'NAME'   => $fName,
                    'DESC'   => $col['comment'] ?? '',
                    'TYPE'   => $col['type']    ?? '',
                    'SIGNE'  => preg_match('/unsigned/i', $col['type'] ?? '') ? "Non sign&eacute;" : "Sign&eacute;",
                    'DEFVAL' => isset($col['default']) ? (string) $col['default'] : '',
                    'KEY'    => isset($pkSet[$fName]) ? "Cl&eacute; primaire" : "",
                    'REF'    => '',
                ];
            }
        }

        // -- 2. Relations -------------------------------------------------
        foreach ($data['relations'] ?? [] as $i => $rel) {
            $childTable  = $rel['table']            ?? '';
            $childCol    = $rel['columns'][0]       ?? '';
            $parentTable = $rel['parentTable']      ?? '';
            $parentCol   = $rel['parentColumns'][0] ?? '';

            if (!isset($this->t_table[$childTable], $this->t_table[$parentTable])) continue;
            if ($childCol === '' || $parentCol === '') continue;

            $relId = 'rel_' . $i;
            $this->t_relation[$relId] = [
                'DESC'   => '',
                'T_PERE' => $parentTable,
                'F_PERE' => $parentCol,
                'T_FILS' => $childTable,
                'F_FILS' => $childCol,
            ];
            $this->t_table[$childTable]['LIENS'][]  = $relId;
            $this->t_table[$parentTable]['LIENS'][] = $relId;

            if (isset($this->t_table[$childTable]['ATTRS'][$childCol])) {
                $this->t_table[$childTable]['ATTRS'][$childCol]['REF'] = $parentTable;
                if (empty($this->t_table[$childTable]['ATTRS'][$childCol]['KEY']))
                    $this->t_table[$childTable]['ATTRS'][$childCol]['KEY'] = "Cl&eacute; &eacute;trang&egrave;re";
            }
        }

        asort($this->t_parcours);
    }
}
