<?php

    use Eae\Transformation\Transformer\MapTransformer;
    use Eae\Transformation\Transformer\CategoryTransformer;
    use Eae\Transformation\Transformer\PersonalityTransformer;
    use Eae\Transformation\Transformer\ThemeTransformer;
    use Eae\Transformation\Transformer\CountryTransformer;
    use Eae\Transformation\Transformer\LocationTransformer;

    class MapController extends Controller
    {
        /**
         * Display the specified resource.
         *
         * @param  string $slug
         * @return Response
         */
        public function index()
        {
            $occurrences = Occurrence::full()
                ->where('is_on_map', TRUE)
                ->select('*')
                ->whereHas('locations', function($query) {
                    $query->whereNotNull('location_lat')->whereNotNull('location_lng');
                })
                ->with([
                    'locations' => function ($query) {
                        $query->full()
                            ->select('*')
                            ->orderBy('location_occurrence.position', 'asc');
                    },
                    'occurrenceImage',
                    'personalities' => function ($query) {
                        $query->full()->select('*');
                    },
                    'themes' => function ($query) {
                        $query->full()->select('*');
                    },
                    'categories' => function ($query) {
                        $query->full()->select('*');
                    },
                    'story' => function ($query) {
                        $query
                            ->select('stories.id','website_objects_lang.lang_slug')
                            ->full();
                    }
                ])
                ->get();

            $countries = Country::full()
                ->whereExists(function($q) {
                    $q->select(DB::raw(1))
                        ->from('occurrences')
                        ->join('location_occurrence', 'occurrences.id', '=', 'location_occurrence.occurrence_id')
                        ->join('locations', 'locations.id', '=', 'location_occurrence.location_id')
                        ->whereRaw('locations.country_id = countries.id');
                })
                ->orderBy("title", "asc")->get();

            $locations = Location::full()
                ->whereExists(function($q) {
                    $q->select(DB::raw(1))
                        ->from('occurrences')
                        ->join('location_occurrence', 'occurrences.id', '=', 'location_occurrence.occurrence_id')
                        ->whereRaw('location_occurrence.location_id = locations.id');
                })
                ->orderBy("title", "asc")->get();

            
            $personalities = Personality::full()
                ->whereExists(function($q) {
                    $q->select(DB::raw(1))
                        ->from('occurrences')
                        ->join('occurrence_personality', 'occurrences.id', '=', 'occurrence_personality.occurrence_id')
                        ->whereRaw('occurrence_personality.personality_id = personalities.id');
                })
                ->orderBy("title", "asc")->get();

            $themes = Theme::full()
                ->whereExists(function($q) {
                    $q->select(DB::raw(1))
                        ->from('occurrences')
                        ->join('occurrence_theme', 'occurrences.id', '=', 'occurrence_theme.occurrence_id')
                        ->whereRaw('occurrence_theme.theme_id = themes.id');
                })
                ->orderBy("title", "asc")->get();

            $categories = Category::full()
                ->whereExists(function($q) {
                    $q->select(DB::raw(1))
                        ->from('occurrences')
                        ->join('category_occurrence', 'occurrences.id', '=', 'category_occurrence.occurrence_id')
                        ->whereRaw('category_occurrence.category_id = categories.id');
                })
                ->orderBy("title", "asc")->where('is_on_map', TRUE)->get();

            if ($occurrences) {
                // Beautify result.
                $result['map'] = TransformService::transformCollection(
                    $occurrences,
                    new MapTransformer(true))->toArray();

                $result['categories'] = TransformService::transformCollection(
                    $categories,
                    new CategoryTransformer())->toArray();

                $result['countries'] = TransformService::transformCollection(
                    $countries,
                    new CountryTransformer())->toArray();

                $result['locations'] = TransformService::transformCollection(
                    $locations,
                    new LocationTransformer())->toArray();

                $result['personalities'] = TransformService::transformCollection(
                    $personalities,
                    new PersonalityTransformer())->toArray();

                $result['themes'] = TransformService::transformCollection(
                    $themes,
                    new ThemeTransformer())->toArray();



                return Response::json($result);
            }

            App::abort(404);
        }
    }
