<?php

namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\Course;
use App\Models\Destination;
use App\Models\Level;
use App\Models\Institute;

use App\Traits\CourseSearch;

use Auth;
use Mail;
use Session;
use DB;

class CourseController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        
    }

    public function filters()
    {  
        $destinations = model('Destination')::select('id','name','name as label','sams_id as refId')->orderBy('name')->get();
        $levels = model('Level')::select('id','name','name as label','sams_id as refId')->orderBy('name')->get();

        return [
            'destinations' => $destinations,
            'levels' => $levels
        ];
    }
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function search(Request $request)
    {  
        $courseIds = CourseSearch::searchByRequest($request);

        return [
            'search_results' => $this->getCourseDetails($courseIds), 
            'filter_data' => $this->filterData($request)
        ];
    }

    function getCourseDetails($courseIds){
        $data = [];
        $result_ids = model('Course')::whereIn('id',$courseIds)->select('courses.*', \DB::raw('(SELECT name FROM institutes WHERE courses.institute_id = institutes.id ) as sort'))->orderBy('sort')->pluck('id')->toArray();
        foreach($result_ids as $id){
            $course = model('Course')::find($id);
            $campuses = model('CourseCampus')::where('course_id', $course->id)->pluck('campus_id')->toArray();
            $intakes = model('CourseIntake')::where('course_id', $course->id)->pluck('intake')->toArray();
            $intakeMonths = [];
            foreach($intakes as $intake){
                array_push($intakeMonths, date("M", mktime(0, 0, 0, $intake, 1)));
            }
            $data[] = [
                'destination' => $course->destination->name,
                'flag' => flag_url($course->destination->short_code),
                'institute' => $course->institute->name,
                'university_link' => $course->institute->website_link,
                'campuses' => model('Campus')::whereIn('id', $campuses)->pluck('name')->toArray(),
                'level' => $course->level->name,
                'courseName' => $course->name,
                'length' => $course->length.' months',
                'tution_fee' => $course->tution_fee == 0?'-':$course->destination->currency.number_format($course->tution_fee, 0),
                'intakes' => count($intakeMonths) > 0?$intakeMonths:'-',
                'ielts' => $course->ielts?$course->ielts:'-',
                'toefl' => $course->toefl?$course->toefl:'-',
                'pte' => $course->pte?$course->pte:'-',
                'duolingo' => $course->duolingo?$course->duolingo:'-',
                'scholarship' => $course->scholarship?$course->scholarship:'-',
                'deposit_required' => $course->deposit_required,
                //'min_gpa' => $course->min_gpa,
                //'ucas_fee' => $course->ucas_fee,
                'sop' => $course->sop,
                'cv' => $course->cv,
                'reference' => $course->reference,
                'entry_requirements' => $course->entry_requirement
            ];
        }

        return $data;
    }

    function filterData($request){
        $intakes = model('CourseIntake')::select('intake')->distinct()->orderBy('intake')->pluck('intake')->toArray();
        foreach($intakes as $intake){
            $intakeList[] = [
                'id' => $intake,
                'label' => date("F", mktime(0, 0, 0, $intake, 1))
            ];
        }
        $english_tests = ['IELTS', 'TOEFL', 'PTE', 'Duolingo'];
        foreach($english_tests as $test){
            $english_testsList[] = [
                'id' => strtolower($test),
                'label' => $test
            ];
        }

        $durations = [1,2,3,4];
        foreach($durations as $duration){
            $durationsList[] = [
                'id' => (12 * $duration),
                'label' => $duration==1?$duration.' Year':$duration.' Years'
            ];
        }

        return [
            'institutes' => model('Institute')::where('destination_id', $request->destination_id)->select('id','name','name as label','sams_id as refId')->orderBy('name')->get(),
            'intakes' => $intakeList,
            'english_tests' => $english_testsList,
            'durations' => $durationsList
        ];
    }
}
