<?php
session_start();

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

include($_SERVER['DOCUMENT_ROOT'] . "/SEAT/config/db.php");

/* =====================================
   JSON RESPONSE
===================================== */
function jsonResponse($exists = false, $error = null){

    echo json_encode([
        "exists" => $exists,
        "error"  => $error
    ]);

    exit;
}

/* =====================================
   REUSABLE EXISTS CHECK
===================================== */
function recordExists($conn, $query, $types, ...$params){

    $stmt = $conn->prepare($query);

    if(!$stmt){
        jsonResponse(false, $conn->error);
    }

    $stmt->bind_param($types, ...$params);

    if(!$stmt->execute()){

        $stmt->close();

        jsonResponse(false, $stmt->error);
    }

    $stmt->store_result();

    $exists = $stmt->num_rows > 0;

    $stmt->close();

    return $exists;
}

/* =====================================
   GET INPUT
===================================== */
$field = trim($_POST['field'] ?? "");
$value = trim($_POST['value'] ?? "");


/* =====================================
   VALIDATE INPUT
===================================== */
if(empty($field) || empty($value)){
    jsonResponse(false, "Missing field/value");
}

/* =====================================
   EMAIL CHECK
===================================== */
if($field === "email"){

    $exists = recordExists(
        $conn,
        "
        SELECT id
        FROM users
        WHERE email = ?
        ",
        "s",
        strtolower($value)
    );

    jsonResponse($exists);
}

/* =====================================
   USER PHONE CHECK
===================================== */
if($field === "phone"){

    $exists = recordExists(
        $conn,
        "
        SELECT id
        FROM participants
        WHERE phone = ?

        UNION

        SELECT id
        FROM organizers
        WHERE advisor_phone = ?
        ",
        "ss",
        $value,
        $value
    );

    jsonResponse($exists);
}

/* =====================================
   STUDENT MATRIC CHECK
===================================== */
if($field === "matric"){

    $exists = recordExists(
        $conn,
        "
        SELECT id
        FROM participants
        WHERE matric_no = ?
        ",
        "s",
        $value
    );

    jsonResponse($exists);
}

/* =====================================
   STAFF ID CHECK
===================================== */
if($field === "staff_matric"){

    $exists = recordExists(
        $conn,
        "
        SELECT id
        FROM participants
        WHERE staff_no = ?
        ",
        "s",
        strtoupper($value)
    );

    jsonResponse($exists);
}

/* =====================================
   ADVISOR PHONE CHECK
===================================== */
if($field === "advisor_phone"){

    $exists = recordExists(
        $conn,
        "
        SELECT id
        FROM organizers
        WHERE advisor_phone = ?

        UNION

        SELECT id
        FROM participants
        WHERE phone = ?
        ",
        "ss",
        $value,
        $value
    );

    jsonResponse($exists);
}

/* =====================================
   INVALID FIELD
===================================== */
jsonResponse(false, "Invalid field");
?>