<?php
header('Content-Type: application/json charset=utf-8');
require_once "{$_SERVER['DOCUMENT_ROOT']}/class/c_abstract_data.php";

final class Periodo_v1 extends WebServiceSGU
{
    public function __construct()
    {
        parent::__construct("/catalogos/periodos/v1/seleccionar");
    }
}
final class Periodo_v2 extends WebServiceSGU
{
    public function __construct()
    {
        parent::__construct("/catalogos/periodos/v2/seleccionar");
    }
}
final class Periodos extends WebServiceSGU
{
    // use DatabaseModel;
    private readonly Periodo_v1 $periodo_v1;
    private readonly Periodo_v2 $periodo_v2;


    public function __construct()
    {
        parent::__construct("/catalogos/periodos/seleccionar");
        $this->periodo_v1 = new Periodo_v1();
        $this->periodo_v2 = new Periodo_v2();
    }

    public function get_v1(): array
    {
        return $this->periodo_v1->get();
    }

    public function get_v2(): array
    {
        return $this->periodo_v2->get();
    }

    public function get_merged(): array
    {
        $v2Data = $this->get_v2();

        // Create an associative array with IdPeriodo as the key for faster lookup
        $v2Lookup = array_column($v2Data, null, 'IdPeriodo');

        return array_map(function ($itemV1) use ($v2Lookup) {
            if (isset($v2Lookup[$itemV1['IdPeriodo']])) {
                $itemV2 = $v2Lookup[$itemV1['IdPeriodo']];
                $mergedItem = array_merge($itemV1, $itemV2);
                unset($mergedItem['NombreCarrera']); // Remove NombreCarrera as specified
                return $mergedItem;
            }
            return $itemV1; // If no matching IdPeriodo found in v2, return the original v1 item
        }, $this->get_v1());
    }
}

final class Espacios extends WebServiceSGU
{
    public function __construct()
    {
        parent::__construct("/catalogos/espacios/seleccionar");
    }
}

final class Carreras extends WebServiceSGU
{
    public function __construct()
    {
        parent::__construct("/catalogos/carreras/seleccionar");
    }
}

final class Horarios extends WebServiceSGU
{
    public function __construct()
    {
        parent::__construct(
            "/seleccionar",
            <<<JSON
                {
                    "\$schema": "http://json-schema.org/draft-07/schema#",
                    "type": "object",
                    "required": ["idPeriodo"],
                    "properties": {
                        "idPeriodo": {
                            "type": "integer",
                            "description": "Identificador del periodo a consultar."
                        },
                        "claveFacultad": {
                            "type": "string",
                            "description": "Clave de la facultad a consultar.",
                            "pattern": "^[a-zA-Z0-9]*$"
                        },
                        "claveCarrera": {
                            "type": "string",
                            "description": "Clave de la carrera a consultar.",
                            "pattern": "^[a-zA-Z0-9]*$"
                        },
                        "claveProfesor": {
                            "type": "string",
                            "description": "Clave del empleado a consultar.",
                            "pattern": "^[a-zA-Z0-9]*$"
                        },
                        "fecha": {
                            "type": "string",
                            "description": "Fecha de la clase.",
                            "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
                        }
                    }
                }
            JSON
        );
    }
}