<?php
// ============================================================
// LabRegistry
//
// Maps lab_code (uppercase) to lab-specific form and
// certificate implementations.
//
// HOW TO ADD A NEW LAB:
//   1. Create labs/{code}/form/{Class}Form.php   (implements ILabForm)
//   2. Create labs/{code}/certificate/{Class}Certificate.php (implements ILabCertificate)
//   3. Create views/lab/forms/{code}_form.php
//   4. Create views/lab/certificates/{code}_certificate.php
//   5. Add an entry to $registry below.
//   6. Add the table schema to database/{code}_schema.sql and import it.
//
// Labs NOT listed here fall back to GenericLabForm /
// GenericLabCertificate and the original generic views.
// ============================================================

class LabRegistry {

    private static array $registry = [

        // ── Mass and Related Quantities (Balance) ────────────
        'MRQL' => [
            'form_class'        => 'BalanceLabForm',
            'certificate_class' => 'BalanceLabCertificate',
            'form_file'         => __DIR__ . '/../labs/balance/form/BalanceLabForm.php',
            'cert_file'         => __DIR__ . '/../labs/balance/certificate/BalanceLabCertificate.php',
            'db_table'          => 'balance_lab_forms',
            'form_view'         => 'lab/forms/balance_form.php',
            'cert_view'         => 'lab/certificates/balance_certificate.php',
        ],

        // ── Temperature and Humidity Laboratory ──────────────
        'THL' => [
            'form_class'        => 'HumidityLabForm',
            'certificate_class' => 'HumidityLabCertificate',
            'form_file'         => __DIR__ . '/../labs/humidity/form/HumidityLabForm.php',
            'cert_file'         => __DIR__ . '/../labs/humidity/certificate/HumidityLabCertificate.php',
            'db_table'          => 'humidity_lab_forms',
            'form_view'         => 'lab/forms/humidity_form.php',
            'cert_view'         => 'lab/certificates/humidity_certificate.php',
        ],

        // ── Template for future labs ──────────────────────────
        // 'EQL' => [
        //     'form_class'        => 'ElectricalLabForm',
        //     'certificate_class' => 'ElectricalLabCertificate',
        //     'form_file'         => __DIR__ . '/../labs/electrical/form/ElectricalLabForm.php',
        //     'cert_file'         => __DIR__ . '/../labs/electrical/certificate/ElectricalLabCertificate.php',
        //     'db_table'          => 'electrical_lab_forms',
        //     'form_view'         => 'lab/forms/electrical_form.php',
        //     'cert_view'         => 'lab/certificates/electrical_certificate.php',
        // ],
        // 'PVL' => [ ... ],
        // 'FML' => [ ... ],
    ];

    // ── Public API ────────────────────────────────────────────

    public static function get(string $labCode): ?array {
        return self::$registry[strtoupper(trim($labCode))] ?? null;
    }

    public static function has(string $labCode): bool {
        return isset(self::$registry[strtoupper(trim($labCode))]);
    }

    public static function getFormHandler(string $labCode): ILabForm {
        $config = self::get($labCode);
        if ($config) {
            require_once $config['form_file'];
            $class = $config['form_class'];
            return new $class();
        }
        require_once __DIR__ . '/GenericLabForm.php';
        return new GenericLabForm();
    }

    public static function getCertificateHandler(string $labCode): ILabCertificate {
        $config = self::get($labCode);
        if ($config) {
            require_once $config['cert_file'];
            $class = $config['certificate_class'];
            return new $class();
        }
        require_once __DIR__ . '/GenericLabCertificate.php';
        return new GenericLabCertificate();
    }

    public static function getFormView(string $labCode): string {
        return self::get($labCode)['form_view'] ?? 'lab/workform.php';
    }

    public static function getCertView(string $labCode): string {
        return self::get($labCode)['cert_view'] ?? 'lab/certificate_view.php';
    }

    public static function getTable(string $labCode): string {
        return self::get($labCode)['db_table'] ?? 'lab_work_forms';
    }

    public static function all(): array {
        return array_keys(self::$registry);
    }
}
