<?php

class AnnualPlan extends \Eloquent
{
	protected $fillable = [];
	protected $table = 'annual_plans';
	protected $appends = ["outcomes"];

	// return outcomes in annual plan to
	public function program()
	{
		return $this->belongsTo('Program');
	}
	public function annualCycle()
	{

		return $this->belongsTo('AnnualCycle');
	}

	//courses_with_transformative

	//

	public function getCoursesAttribute()
	{
		/*
		$sql = Course::join('typ_semester_courses', 'typ_semester_courses.course_id', '=', 'courses.id')
			->join('typ_semester_objectives', 'typ_semester_courses.typ_semester_objective_id', '=', 'typ_semester_objectives.id')
			->join("typ_semester_outcome", 'typ_semester_outcome.id', '=', 'typ_semester_objectives.typ_semester_outcome_id')
			->join("annual_cycle", function ($join) {
				$join->on('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_start')
					->orOn('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_end');
			})
			->join('annual_plans', 'annual_plans.annual_cycle_id', '=', 'annual_cycle.id')
			->join('typ_program', 'typ_program.program_id', '=', 'annual_plans.program_id')
			->where('typ_program.id', '=', 'typ_semester_outcome.typ_program_id')
			->where('annual_plans.program_id', $this->program_id)
			->where('annual_plans.id', $this->id)
			->select('courses.*', 'typ_semester_courses.id as typ_semester_course_id', 'annual_plans.program_id', 'typ_semester_outcome.semester_id as semester_id')
			->toSql();
		Log::info($sql);
		Log::info("Aqui sql");

		*/
		return Course::join('typ_semester_courses', 'typ_semester_courses.course_id', '=', 'courses.id')
			->join('typ_semester_objectives', 'typ_semester_courses.typ_semester_objective_id', '=', 'typ_semester_objectives.id')
			->join("typ_semester_outcome", 'typ_semester_outcome.id', '=', 'typ_semester_objectives.typ_semester_outcome_id')
			->join("annual_cycle", function ($join) {
				$join->on('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_start')
					->orOn('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_end');
			})
			->join('annual_plans', 'annual_plans.annual_cycle_id', '=', 'annual_cycle.id')
			->join('typ_program', function ($join) {

				$join->on('typ_program.program_id', '=', 'annual_plans.program_id')
					->on('typ_program.id', '=', 'typ_semester_outcome.typ_program_id');
			})

			->where('annual_plans.program_id', $this->program_id)
			->where('annual_plans.id', $this->id)
			->select('courses.*', 'typ_semester_courses.id as typ_semester_course_id', 'annual_plans.program_id', 'typ_semester_outcome.semester_id as semester_id')
			->get();
	}

	//Aprendi luego que se puede hacer esto smh

	public function getCoursesWithTransformativeActionsAttribute()
	{
		/*$courses = DB::table("annual_plans")

			->join('annual_plan_transformative', 'annual_plans.id', '=', 'annual_plan_transformative.annual_plan_id')
			->join('typ_semester_courses', 'annual_plan_transformative.typ_semester_course_id', '=', 'typ_semester_courses.id')
			->where('annual_plan_id', $this->id)
			->select('typ_semester_courses.id as typ_semester_course_id', 'typ_semester_courses.course_id as course_id')
			->get();
		$course_list = [];
		foreach ($courses as $c) {
			$cour = Course::find($c->course_id);
			$cour->setAttribute('typ_semester_course_id', $c->typ_semester_course_id);
			$course_list[] = $cour;
		}
		return $course_list*/

		return Course::join('typ_semester_courses', 'typ_semester_courses.course_id', '=', 'courses.id')
			->join('annual_plan_transformative', 'typ_semester_courses.id', '=', 'annual_plan_transformative.typ_semester_course_id')
			->where('annual_plan_id', $this->id)
			->select('courses.*', 'typ_semester_courses.id as typ_semester_course_id')
			->groupBy("courses.id")
			//->with("proposed_transformative_actions")
			->get();
	}





	public function	getOutcomesAttribute()
	{


		return Outcome::join('typ_semester_outcome', 'typ_semester_outcome.outcome_id', '=', 'outcomes.id')
			->join('annual_cycle', function ($j) {
				$j->on('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_start')
					->orOn('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_end');
			})
			->join('annual_plans', 'annual_cycle.id', '=', 'annual_plans.annual_cycle_id')
			->join('typ_program', function ($j) {
				$j->on('typ_semester_outcome.typ_program_id', '=', 'typ_program.id')
					->on('typ_program.program_id', '=', 'annual_plans.program_id');
			})
			->join('typ_semester_objectives', 'typ_semester_objectives.typ_semester_outcome_id', '=', 'typ_semester_outcome.id')
			->join('typ_semester_courses', 'typ_semester_courses.typ_semester_objective_id', '=', 'typ_semester_objectives.id')
			->where('annual_plans.id', $this->id)
			->groupBy('outcomes.id', 'semester_id')
			->orderBy('typ_semester_outcome.semester_id')
			->select(
				'outcomes.*',
				'outcomes.id as outcome_id',
				'typ_semester_outcome.id as typ_semester_outcome_id',
				'typ_semester_outcome.semester_id as semester_id',
				DB::raw("{$this->program_id} as program_id")
			)->get();



		/*$outcomes_query = DB::table('annual_plans')
			->join('annual_cycle', 'annual_cycle.id', '=', 'annual_plans.annual_cycle_id')
			->join('typ_semester_outcome', function ($q) {
				$q->on('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_start')
					->orOn('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_end');
			})
			->join('typ_program', function ($j) {
				$j->on('typ_semester_outcome.typ_program_id', '=', 'typ_program.id')
					->on('typ_program.program_id', '=', 'annual_plans.program_id');
			})
			->join('typ_semester_objectives', 'typ_semester_objectives.typ_semester_outcome_id', '=', 'typ_semester_outcome.id')
			->join('typ_semester_courses', 'typ_semester_courses.typ_semester_objective_id', '=', 'typ_semester_objectives.id')
			->join('outcomes', 'typ_semester_outcome.outcome_id', '=', 'outcomes.id')
			->where('annual_plans.id', $this->id)
			->groupBy('outcomes.id')
			->orderBy('typ_semester_outcome.semester_id')
			->select('outcomes.id as outcome_id', 'typ_semester_outcome.id as typ_semester_outcome_id', 'typ_semester_outcome.semester_id as semester_id');
		//Log::info($outcomes_query->toSql());


		//$outcomes = $outcomes_query->lists('outcome_id');
		//$outcomes_typ_semester_id = $outcomes_query->lists('typ_semester_outcome_id');
		//$outcomes_semester = $outcomes_query->lists('semester_id');

		//->lists(array('outcome_id', 'typ_semester_outcome_id', "typ_semester_outcome.semester_id"));
		$outcomes = $outcomes_query->get();
		//Log::info("an_id");
		//Log::info($an_id);
		$an_id = $this->id;
		//Log::info($an_id);
		//Log::info($outcomes);



		/*$outcomes_full = Outcome::whereIn("id", $outcomes)->get();

		Log::info($outcomes_full);

		foreach ($outcomes_full as $index => $outc) {
			$outc->setAttribute('typ_semester_outcome_id', $outcomes_typ_semester_id[$index]);
			$outc->setAttribute('semester_id', $outcomes_semester[$index]);
			$outc->setAttribute('program_id', $this->program_id);
			Log::info("Imagine");
		}
		//Log::info();*/
		/*
		$outcomes_full = [];
		//Log::info("Estamos aqui");
		//Log::info($outcomes);
		foreach ($outcomes as $index => $outc) {

			//Log::info("outcome_id");
			//Log::info($outc->outcome_id);
			$outcomes_full[$index] = Outcome::where('id', $outc->outcome_id)->first();
			//Log::info(array($outcome));
			$outcomes_full[$index]->setAttribute('typ_semester_outcome_id', $outc->typ_semester_outcome_id);
			$outcomes_full[$index]->setAttribute('semester_id', $outc->semester_id);
			$outcomes_full[$index]->setAttribute('program_id', $this->program_id);
			//$outcomes_full[] = $outcome;
		}


		return $outcomes_full;*/

		/*with([
			'typ_semester_outcome_id' => function ($query) use (&$an_id) {
				Log::info($this->id);
				$query->join('typ_semester_outcome', 'typ_semester_outcome.outcome_id', '=', $this->id)
					->join('annual_cycle', function ($q) {
						$q->on('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_start')
							->orOn('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_end');
					})->join('typ_semester_objectives', 'typ_semester_objectives.typ_semester_outcome_id', '=', 'typ_semester_outcome.id')
					->join('typ_semester_courses', 'typ_semester_courses.typ_semester_objective_id', '=', 'typ_semester_objectives.id')

					->join('annual_cycle', 'annual_cycle.id', '=', 'annual_plans.annual_cycle_id')
					->join('typ_semester_outcome', function ($q) {
						$q->on('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_start')
							->orOn('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_end');
					})
					->join('typ_program', function ($j) {
						$j->on('typ_semester_outcome.typ_program_id', '=', 'typ_program.id')
							->on('typ_program.program_id', '=', 'annual_plans.program_id');
					})
					->where('annual_plans.id', $an_id)
					->select('typ_outcome_semester_id')
					->first();
				//->first()->typ_outcome_semester_id;
				//->where('outcome_id',$this->id)

			}
		]);*/


		/*
			select * from annual_plans
			
			select outcome_id, typ_semester_outcome_id, outcomes.* from annual_plans 
		
			join annual_cycle on annual_cycle.id = annual_plans.annual_cycle_id 
			join typ_semester_outcome on typ_semester_outcome.semester_id = annual_cycle.semester_start or typ_semester_outcome.semester_id = annual_cycle.semester_end 
			join typ_program on typ_semester_outcome.typ_program_id = typ_program.id and typ_program.program_id = annual_plans.program_id
			join typ_semester_objectives on typ_semester_outcome.id = typ_semester_objectives.typ_semester_outcome_id 
			join typ_semester_courses on typ_semester_objectives.id = typ_semester_courses.typ_semester_objective_id
			join outcomes on typ_semester_outcome.outcome_id = outcomes.id
			
			where annual_plans.id = 3
			GROUP by outcome_id
		*/
	}
}