<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\PropertiesCollection;
use App\Http\Resources\NotificationsCollection;
use App\Http\Resources\Properties;
use App\Models\Property;
use App\Http\Requests\Api\BaseShow;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class MapController extends Controller
{
    public function index()
    {
        $activeLang = getActiveLanguage();
        $cacheValues = Cache::remember("properties-{$activeLang}", now()->addDay(), function () {
            $properties = Property::full()
                ->with([
                    'propertyType' => function ($query) {
                        $query->full()->propertyTypeFilters();
                    },
                    'classifications' => function ($query) {
                        $query->full()->classificationFilters();
                    },
                    'accesses' => function ($query) {
                        $query->full()->accessFilters();
                    },
                    'images' => function ($query) {
                        $query->full()->orderBy('id', 'asc')->with('image');
                    },
                    'chronologies' => function ($query) {
                        $query->full();
                    },
                    'epigraphs' => function ($query) {
                        $query->full();
                    },
                    'qrcode',
                ])
                //->paginate(15);
                ->get();
            return (new PropertiesCollection($properties))->toResponse(request())->getData(true);
        });
        return response()->json($cacheValues);
    }

    public function getNotifications() {
        $notifications = Property::select('id', 'geolocation')->get();
        return New NotificationsCollection($notifications);
    }

    public function show(BaseShow $request)
    {
        $data = $request->validated();
        $slug = (string) $data['slug'] ?? "";
        $property = Property::select("*")
            ->full()
            ->where('lang_slug', $slug)
            ->with([
                'epigraphs' => function ($query) {
                    $query->select('*')->full()->with([
                        'classifications' => function ($query) {
                            $query->select('*')->full()->classificationFilters();
                        },
                        'audioVisuals' => function ($query) {
                            $query->select('*')->full();
                        },
                        'location' => function ($query) {
                            $query->select('*')->full();
                        },
                    ]);
                },
                'audioVisuals' => function ($query) {
                    $query->select('*')->full();
                },
                'accesses' => function ($query) {
                    $query->select('*')->full()->accessFilters();
                },
                'images' => function ($query) {
                    $query->select('*')->full()->orderBy('id', 'asc')->with('image');
                },
                'chronologies' => function ($query) {
                    $query->select('*')->full()->orderBy('website_objects.position', 'asc');
                },
                'themes' => function ($query) {
                    $query->select('*')->full()->orderBy('theme_languages.title', 'asc')->with('icon');
                },
                'location' => function ($query) {
                    $query->select('*')->full();
                },
                'classifications' => function ($query) {
                    $query->select('*')->full();
                },
                'propertyType' => function ($query) {
                    $query->select('*')->full();
                },
                'typologies' => function ($query) {
                    $query->select('*')->full();
                },
                //I Had to do this in order to remove some extra queries since full() wasn't loading website and website languages relation
                'websites' => function ($query) {
                    $query->with('languages');
                },
            ])
            ->first();
        if ($property) {
            return response()->json(new Properties($property, true));
        } else {
            return response()->json();
        }
    }
}
