<?php
include('includes/config/config.php');
include('includes/functions.php');
include('log_entry.php');
error_log("Start Printing Request Attributes");
foreach ($_REQUEST as $key => $value) {
    error_log($key . " : " . $value . "\n");
}
error_log("End Printing Request Attributes");


$rules = explode(",", $_REQUEST['rules']);
$key = $_REQUEST['key_param_name'];
$val = $_REQUEST['val'];
$patient_id = $_REQUEST['emp_id'];

error_log("patient id " . $patient_id);
$pat_dob = getFieldFromTable('dob', 'patient_master', 'id', $patient_id);

$today = date("Y-m-d");
error_log("pat_dob " . $pat_dob);
$age = ($today - $pat_dob);
error_log("age " . $age);

$pat_gender = getFieldFromTable('gender', 'patient_master', 'id', $patient_id);
error_log("patient gender " . $pat_gender);

$data = [];

foreach ($rules as $id) {
    $query = "select * from rule_equation where rule_eq_id='" . $id . "'";

    error_log("rule equation getting query " . $query);

    $result = mysqli_query($conn, $query);

    $row = mysqli_fetch_assoc($result);

    $rule_age_start = $row['rule_age_start'];
    $rule_age_end = $row['rule_age_end'];
    $rule_gender = $row['rule_gender'];

    error_log("equation age gender data " . $rule_age_start . " " . $rule_age_end . " " . $rule_gender);

    if (intval($rule_age_end) != 0) {
        if (intval($rule_age_start) > intval($age)) {
            error_log("got here and continue for age mismatch" . $rule_age_start . " " . $age);
            continue;
        } else if (intval($rule_age_end) < intval($age)) {
            error_log("got here and continue for age mismatch" . $rule_age_end . " " . $age);
            continue;
        }
    }
    if (($rule_gender != "" || $rule_gender != null) && strtolower($rule_gender) != strtolower($pat_gender)) {
        error_log("got here and continue for gender mismatch");
        continue;
    }

    $equation = $row['rule_equation'];

    error_log("before equation " . $equation);

    if ($row['is_string_rule'] == 0) {
        if ($val != '' && $val != null) {
            $equation = str_replace($key, $val, $equation);

            error_log("after placing values in  equation " . $equation);

            $ans = solveMathExpression($equation);

            error_log("ans is " . $ans);

            if ($ans == 1) {
                error_log("got inside the if");
                $data = $row;
                $data['risks'] = getCommaSeperatedValuesForInClause("select health_risk_name from health_risk", 'health_risk_id', $row['risks']);
                $data['advices'] = getCommaSeperatedValuesForInClause("select health_advice_name from health_advice", 'health_advice_id', $row['advices']);
            }
        }
    } else if ($row['is_string_rule'] == 1 && $val != '' && $val != null) {
        $rule_val = getFieldFromTable('rule_s_val', 'rule_save', 'equation_rule_id', $row['rule_eq_id']);

        $rule_values = explode(",", $rule_val);

        for ($i = 0; $i < sizeof($rule_values); $i++) {
            if (strcmp(strtolower(trim($rule_values[$i])), strtolower(trim($val))) == 0) {
                error_log("got inside the string rule if");
                $data = $row;
                $data['risks'] = getCommaSeperatedValuesForInClause("select health_risk_name from health_risk", 'health_risk_id', $row['risks']);
                $data['advices'] = getCommaSeperatedValuesForInClause("select health_advice_name from health_advice", 'health_advice_id', $row['advices']);
            }
        }
    }
}


function solveMathExpression($expression)
{
    // Check if the math expression matches the pattern
    // if (preg_match($pattern, $expression)) {
    try {
        return round(eval("return ($expression);"), 2);
    } catch (ParseError $e) {
        error_log("getting exception in this " . $e->getMessage());
    }
}

error_log("final data is " . print_r($data, true));
echo json_encode($data);