<?php
/**
 * Demo Oracle — DemoOracleConfigRepository.
 *
 * Portage de modules/oracle/lib/OracleConfigRepository.php : requetes de LECTURE
 * uniquement.
 *
 * Convention conservee du module complet : classe sans etat, $conn (ressource
 * OCI8) en premier argument, retour systematique en array — vide en cas
 * d'echec OCI, jamais false, jamais d'exception. La page appelante affiche
 * l'erreur globale.
 */

if (class_exists('DemoOracleConfigRepository')) return;

class DemoOracleConfigRepository
{
    public static function getDbInfo($conn): array
    {
        $sql = "SELECT dbid, name, db_unique_name,
                       TO_CHAR(created, 'YYYY-MM-DD HH24:MI') AS created,
                       open_mode, log_mode, database_role, protection_mode,
                       protection_level, force_logging, flashback_on,
                       platform_name, cdb, current_scn, controlfile_type,
                       switchover_status
                FROM   V\$DATABASE";
        $stm = @oci_parse($conn, $sql);
        if (!$stm || !@oci_execute($stm, OCI_NO_AUTO_COMMIT)) return [];
        $row = oci_fetch_assoc($stm);
        oci_free_statement($stm);
        return $row ?: [];
    }

    public static function getInstances($conn): array
    {
        $sql = "SELECT inst_id, instance_number, instance_name, host_name,
                       version_full AS version, edition,
                       TO_CHAR(startup_time, 'YYYY-MM-DD HH24:MI') AS startup_time,
                       status, database_status, instance_role, active_state,
                       archiver, logins, thread#, database_type
                FROM   GV\$INSTANCE
                ORDER BY instance_number";
        $rows = [];
        $stm = @oci_parse($conn, $sql);
        if (!$stm || !@oci_execute($stm, OCI_NO_AUTO_COMMIT)) return $rows;
        while ($r = oci_fetch_assoc($stm)) {
            $rows[] = $r;
        }
        oci_free_statement($stm);
        return $rows;
    }

    public static function getParameters($conn): array
    {
        $sql = "SELECT i.instance_name iname
                     , p.name
                     , p.display_value
                     , p.ISDEFAULT
                     , nvl(p.default_value,'null') default_value
                     , p.isses_modifiable
                     , p.issys_modifiable
                     , p.ispdb_modifiable
                     , p.isinstance_modifiable
                     , p.ismodified
                     , p.isbasic
                     , p.description
                     , p.update_comment
                     , p.con_id
                     , p.type
                  FROM gv\$parameter p
                  JOIN gv\$instance i ON i.inst_id = p.INST_ID
                 ORDER BY p.name, i.instance_name";
        $rows = [];
        $stm = @oci_parse($conn, $sql);
        if (!$stm || !@oci_execute($stm, OCI_NO_AUTO_COMMIT)) return $rows;
        while ($r = oci_fetch_assoc($stm)) {
            $rows[] = $r;
        }
        oci_free_statement($stm);
        return $rows;
    }

    public static function getNlsParameters($conn): array
    {
        $sql = "SELECT parameter
                     , max(case when LVL='DB'       then value else null end) dbval
                     , max(case when LVL='INSTANCE' then value else null end) instval
                     , max(case when LVL='SESSION'  then value else null end) sessval
                  FROM (SELECT 'DB'       lvl, d.* FROM NLS_DATABASE_PARAMETERS d
                        UNION ALL
                        SELECT 'INSTANCE', i.* FROM NLS_INSTANCE_PARAMETERS i
                        UNION ALL
                        SELECT 'SESSION',  s.* FROM NLS_SESSION_PARAMETERS s)
                 GROUP BY parameter
                 ORDER BY parameter";
        $rows = [];
        $stm = @oci_parse($conn, $sql);
        if (!$stm || !@oci_execute($stm, OCI_NO_AUTO_COMMIT)) return $rows;
        while ($r = oci_fetch_assoc($stm)) {
            $rows[] = $r;
        }
        oci_free_statement($stm);
        return $rows;
    }
}
