<?php

namespace App\Traits;
 
trait CourseSearch{
 
    public static function searchByRequest($request) {
        $ids = [];
        $level_ids = [];
        $subject_ids = [];
        $institute_ids = [];
        $intake_ids = [];
        $length_ids = [];
        $requirement_ids = [];
        $fees_ids = [];
        $tags_ids = [];

        $query = model('Course')::where('destination_id', $request->destination_id);
        $all_ids = (clone $query)->pluck('id')->toArray();
        $search_type = $request->search_type;
        $search_text = $request->search_text;

        if($request->level_id){
            $level_ids = (clone $query)->where('level_id', $request->level_id)->pluck('id')->toArray();
        }else{
            $level_ids = $all_ids;
        }

        if($search_type == 'subject'){
            if($search_text != null){
                $subject_ids = (clone $query)->where('name', 'like', '%' . $search_text . '%')->pluck('id')->toArray();
            }else{
                $subject_ids = $all_ids;
            }
        }else{
            if($search_text != null){
                $subject_ids = (clone $query)->whereHas('institute', function($qry) use($search_text){
                    $qry->where('name', 'like', '%' . $search_text . '%');
                })->pluck('id')->toArray();
            }else{
                $subject_ids = $all_ids;
            }
        }

        if($request->institute_ids){
            $institute_ids = (clone $query)->whereIn('institute_id', $request->institute_ids)->pluck('id')->toArray();
        }else{
            $institute_ids = $all_ids;
        }

        if($request->intake_ids){
            $intakeCheck = model('CourseIntake')::whereIn('Intake', $request->intake_ids)->pluck('course_id')->toArray();
            $intake_ids = (clone $query)->whereIn('id', $intakeCheck)->pluck('id')->toArray();
        }else{
            $intake_ids = $all_ids;
        }

        if($request->durations){
            $length_ids = (clone $query)->whereIn('length', $request->durations)->pluck('id')->toArray();
        }else{
            $length_ids = $all_ids;
        }

        if($request->course_fees){
            $range = [
                '15000' => [0,15000],
                '25000' => [15000,25000],
                '35000' => [25000,35000],
                '50000' => [35000,50000],
                '56000' => [50000,1000000]
            ];
            foreach($request->course_fees as $feeRange){
                $limit = $range[$feeRange];
                $from = $limit[0];
                $to = $limit[1];
                $range_ids = (clone $query)->where('tution_fee','>=', $from)->where('tution_fee','<=', $to)->pluck('id')->toArray();
                $fees_ids = array_merge($fees_ids, $range_ids);
            }
        }else{
            $fees_ids = $all_ids;
        }

        if($request->english_requirements){
            $requirement_ids = (clone $query)->whereNotNull($request->english_requirements)->pluck('id')->toArray();
        }else{
            $requirement_ids = $all_ids;
        }
        
        $ids = array_intersect($all_ids, $level_ids, $subject_ids, $institute_ids, $intake_ids, $length_ids, $requirement_ids, $fees_ids);

        if($search_text != null){
            $tags_ids = model('CourseTag')::whereIn('course_id', $level_ids)->where('keyword', 'like', '%' . $search_text . '%')->pluck('course_id')->toArray();
            $ids = array_merge($ids, $tags_ids);
        }

        return $ids;
    }
}

?>