<?php
/**
 * api/api.php
 *
 * Minimal REST API stub — migrated from the legacy mysql_connect version.
 * The original used mysql_connect() which was removed in PHP 7.
 * Rewritten to use PDO via config/database.php.
 *
 * Note: this API has no real endpoints yet. Extend as needed.
 */

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/Rest.inc.php';

class API extends REST
{
    public string $data = '';

    public function __construct()
    {
        parent::__construct();
    }

    public function processApi(): void
    {
        $func = strtolower(trim(str_replace('/', '', $this->_request['rquest'] ?? '')));
        if ($func !== '' && method_exists($this, $func)) {
            $this->$func();
        } else {
            $this->response('{"error":"Not found"}', 404);
        }
    }

    private function hello(): void
    {
        $this->response(json_encode(['message' => 'Hello from Crop Monitor API']), 200);
    }

    private function test(): void
    {
        if ($this->get_request_method() !== 'GET') {
            $this->response('', 406);
            return;
        }
        $param = $this->_request['var'] ?? '';
        $this->response(json_encode(['param' => $param]), 200);
    }
}

$api = new API();
$api->processApi();
