<?php
require_once('_conf/globals.php');
require_once('_conf/db_con.php');
$postData = file_get_contents('php://input');
$postData = json_decode($postData);

if(isset($_GET) && !empty($_GET) && isset($_GET['countries']) && $_GET['countries']  == 'true'){
    require_once('filter_class.php');
    $filter = new StellaFilter();
    echo $filter->returnCountries();
}

if(isset($_GET) && !empty($_GET) && isset($_GET['allowedFilter']) && $_GET['allowedFilter']  == 'true'){
    require_once('includes/allowedFilter.php');
    $allowedFilter = new stdClass();
    $allowedFilter->sexualities = $sexualities; 
    $allowedFilter->lookingFor = $lookingFor;  
    $allowedFilter->hairs = $hairs;  
    $allowedFilter->hairColors = $hairColors;  
    $allowedFilter->facialHairs = $facialHairs;  
    $allowedFilter->pubicHairs = $pubicHairs;  
    $allowedFilter->bodyHairs = $bodyHairs;  
    $allowedFilter->tattoos = $tattoos;  
    $allowedFilter->piercings = $piercings;  
    $allowedFilter->eyeColors = $eyeColors;  
    $allowedFilter->breastSizes = $breastSizes;  
    $allowedFilter->abs = $abs;  
    $allowedFilter->bottomSizes = $bottomSizes;  
    $allowedFilter->bodyTypes = $bodyTypes;  
    $allowedFilter->ethnicities = $ethnicities;  
    $allowedFilter->fitnessLevels = $fitnessLevels;  
    $allowedFilter->dietaryPreferences = $dietaryPreferences;  
    $allowedFilter->healthHabits = $healthHabits;  
    $allowedFilter->exerciseFrequency = $exerciseFrequency;  
    $allowedFilter->sleepingHabits = $sleepingHabits;  
    $allowedFilter->foodAllergies = $foodAllergies;  
    $allowedFilter->alcoholConsumptions = $alcoholConsumptions;  
    $allowedFilter->smoking = $smoking;  
    $allowedFilter->jobs = $jobs;  
    $allowedFilter->mainLiving = $mainLiving;  
    $allowedFilter->financialGoals = $financialGoals;  
    $allowedFilter->spendingHabits = $spendingHabits;  
    $allowedFilter->financialIndependence = $financialIndependence;  
    $allowedFilter->fetishes = $fetishes;  
    $allowedFilter->personalities = $personalities;  
    $allowedFilter->hobbiesAndActivities = $hobbiesAndActivities;  
    $allowedFilter->movieGenres = $movieGenres;  
    $allowedFilter->bookGenres = $bookGenres;  
    $allowedFilter->highestGraduation = $highestGraduation;  
    $allowedFilter->education = $education;  
    $allowedFilter->travelPreferences = $travelPreferences;  
    $allowedFilter->communicationPreferences = $communicationPreferences;  
    $allowedFilter->religion = $religion;  
    $allowedFilter->languages = $languages;  
    $allowedFilter->currentSituation = $currentSituation;  
    $allowedFilter->pets = $pets;
    $allowedFilter->sexualActivity = $sexualActivity;
    header('Content-Type: application/json');
    echo json_encode($allowedFilter);
}

if(isset($postData->action) && $postData->action == 'search'){
    require_once('filter_class.php');
    require_once('users_class.php');
    $filter = new StellaFilter();

    // Calculate age based on year range
    $filter->minAge = $filter->calcYear($postData->filter->minAge);
    $filter->maxAge = $filter->calcYear($postData->filter->maxAge);

    unset($postData->filter->minAge);  // Remove minAge from filter
    unset($postData->filter->maxAge);  // Remove maxAge from filter

    $user = new Users();
    if($user->isAuth()){
        if($user->hasPaid()){
            $searchTerms = $postData->filter;
            foreach($searchTerms as $termName => $term){
                if(isset($term) && !empty($term)){
                    $filter->setFilter($termName, $term);  // Set the filter dynamically
                }
            }
            // Call search with the dynamically set filters
            $users = $filter->search($searchTerms);
            echo $users;
        }
    }
}
