<?php
// Use Statements
use classes\CRUD\CRUDController;
use classes\FunctionsGateway;
use classes\MysqliVeu;
use classes\RDTResponse;
use \classes\Location;


// Includes and requires
include_once __DIR__ . '/./coreInit.php';

// ----------------------------------------------------------------------------
// Global Variables Statements and function loader call
// ----------------------------------------------------------------------------

$function = $_GET['function'] ?? '';
$postedData = json_decode((file_get_contents('php://input')), true) ?? [];

// Functions Loader
FunctionsGateway::call($function, $postedData);

// ----------------------------------------------------------------------------
// Local Helper Functions
// ----------------------------------------------------------------------------


// ----------------------------------------------------------------------------
// Main Functions
// ----------------------------------------------------------------------------

function validatePartNumber($postedData)
{
    $database = new MysqliVeu();

    // Posted Data Validations
    if (empty($postedData['partNumber']))
    {
        RDTResponse::fail('No part number provided');
    }

    $sql = "SELECT id FROM part WHERE part_number = ?";

    $result = $database->queryParamsRow($sql, [$postedData['partNumber']]);

    if (empty($result))
    {
        RDTResponse::fail("Incorrect Part Number");
    }

    RDTResponse::ok("validatePartNumber");
}


function validateLocation($postedData)
{
    $database = new MysqliVeu();

    $location = new Location($database);
    $location->getLocationRecordByLocationCode($postedData['locationCode']);

    if (!$location->validateLocationHash($postedData['locationHashCode']))
    {
        RDTResponse::fail("Incorrect Location");
    }

    RDTResponse::ok("validateLocationHash");
}
