<?php

namespace App\Controllers;

use App\Models\EventsModel;

class Events extends BaseController
{
    protected $eventsModel;

    public function __construct()
    {
        $this->eventsModel = new \App\Models\EventsModel();
    }

    public function index()
    {
        $events = $this->eventsModel->getEventList();
        return view('events_list', [
            'menu'   => 'menus/raven_full',
            'header' => 'headers/default',
            'events' => $events
        ]);
    }

    public function dashboard($id)
    {
        return view('event_dashboard', ['id' => $id]);
    }

    public function test($id)
    {
echo($id);
        //$data = $this->eventsModel->getcourseConfig($id);
     $this->readertimingpointJson($id);
        exit;
        $data = $this->eventsModel->getLinkedDatatimingpointWithConfig($id);
        echo "<pre>";
        print_r($data);
        echo "</pre>";
        exit;


        echo "<pre>";
        print_r($data);
        echo "</pre>";
        exit;

        $data = $this->eventsModel->getLinkedData($id);
        $data = $this->eventsModel->getEventList();
        echo "<pre>";
        print_r($data);
        echo "</pre>";

    }
    // ---------------------------------------------------------
    // NOUVELLE FONCTION : READ — récupère les données Event
    // ---------------------------------------------------------
    public function reader($id)
    {
        $model = new EventsModel();

        // Lire les données dans le modèle
       $eventData = $this->eventsModel->getLinkedData($id);
       $data = $this->eventsModel->getStatsByEventName($eventData['config']['con_event_name']);
       $configtp = $this->eventsModel->getLinkedDatatimingpointWithConfig($id);
        $configcourse = $this->eventsModel->getcourseConfig($id);

        // Passer à la vue




        return view('event_reader', [
            'id'    => $id,
            'data' => $eventData,
            'readers' => $data,
            'config_tp' => $configtp,
            'config_course' => $configcourse['config_course'],
        ]);
    }
    public function readerJson($id)
    {
        // Lire les données comme dans ta méthode reader()
        $eventData = $this->eventsModel->getLinkedData($id);
        $readers   = $this->eventsModel->getStatsByEventName($eventData['config']['con_event_name']);

        // Rafraîchissement (1 seconde pour maintenant)
        $refreshRate = 1;

        // Retour JSON
        return $this->response->setJSON([
            'refreshRate' => $refreshRate,
            'readers'     => $readers
        ]);
    }
    public function readertimingpointJson($id)
    {


        $readertimingpoint = $this->eventsModel->getLinkedDatatimingpoint($id);

        // Rafraîchissement (1 seconde pour maintenant)
        $refreshRate = 1;
        //

        $status = $this->eventsModel->getProcessStatus($id);


        // Retour JSON
        return $this->response->setJSON([
            'refreshRate' => $refreshRate,
            'readertimingpoint'     => $readertimingpoint,
            'process_status'        => $status
        ]);

    }

    public function reset($con_id)
    {
        $this->eventsModel->resetCourse($con_id);


        // revenir sur la même page (celle d’avant)
        return redirect()->back();
    }

    public function create(int $courseId)
    {
        $results = $this->eventsModel->duplicateResultTables($courseId);

        // Pour les tests en browser, on affiche brut (JSON ou <pre>)
        // Version JSON (propre dans le navigateur) :
       // return $this->response->setJSON($results);


        echo '<pre>';
        print_r($results);
        echo '</pre>';


    }

}
