<?php

header('Content-Type: application/json');

try {

    // Connect to online database
    $db = new PDO(
        'sqlite:' . __DIR__ . '/database.db'
    );

    $db->setAttribute(
        PDO::ATTR_ERRMODE,
        PDO::ERRMODE_EXCEPTION
    );


    /*
     * --------------------------------------------------
     * PROCEDURE PER EXAMINER
     * --------------------------------------------------
     */

    $stmt = $db->query("
        SELECT
            id,
            procedure_number,
            is_selected
        FROM procedure_per_examiner
        ORDER BY id ASC
    ");

    $procedurePerExaminer = [];

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

        $procedurePerExaminer[] = [
            'id' => (int)$row['id'],
            'procedure_number' => (int)$row['procedure_number'],
            'is_selected' => (int)$row['is_selected']
        ];
    }


    /*
     * --------------------------------------------------
     * OSCE SCORE
     * --------------------------------------------------
     */

    $stmt = $db->query("
        SELECT
            id,
            total_score
        FROM osce_score
        ORDER BY id ASC
    ");

    $osceScore = [];

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

        $osceScore[] = [
            'id' => (int)$row['id'],
            'total_score' => (int)$row['total_score']
        ];
    }


    /*
     * --------------------------------------------------
     * EXAMINERS TABLE
     * --------------------------------------------------
     */

    $stmt = $db->query("
        SELECT
            id,
            examiner_number
        FROM examiners_table
        ORDER BY id ASC
    ");

    $examinersTable = [];

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

        $examinersTable[] = [
            'id' => (int)$row['id'],
            'examiner_number' => (int)$row['examiner_number']
        ];
    }


    /*
     * --------------------------------------------------
     * OSCE PROCEDURES
     * --------------------------------------------------
     */

    $stmt = $db->query("
        SELECT
            id,
            table_name
        FROM OSCE_procedures
        ORDER BY id ASC
    ");

    $osceProcedures = [];

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

        $osceProcedures[] = [
            'id' => (int)$row['id'],
            'table_name' => $row['table_name']
        ];
    }


    /*
     * --------------------------------------------------
     * SEND EVERYTHING
     * --------------------------------------------------
     */

    echo json_encode(
        [
            'success' => true,

            'procedure_per_examiner' =>
            $procedurePerExaminer,

            'osce_score' =>
            $osceScore,

            'examiners_table' =>
            $examinersTable,

            'OSCE_procedures' =>
            $osceProcedures
        ],
        JSON_UNESCAPED_UNICODE
    );
} catch (Exception $e) {

    echo json_encode(
        [
            'success' => false,
            'message' => $e->getMessage()
        ]
    );
}
