<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\GenericBackoffice\Entry;
use App\Http\Requests\Api\BaseShow;
use App\Http\Resources\Entries;

class EntryController extends Controller
{
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(BaseShow $request)
    {
        $data = $request->validated();
        $query = Entry::full()
            ->where('status', 'published')
            ->where('page_type', $data['slug'])
            ->orderBy('website_objects.website_object_id', 'desc');

        if (isset($data['slug_page'])) {
            $query->where('slug', $data['slug_page']);
        }

        if ($data['slug'] != "technical-record") {
            $query->with([
                'blocks' => function ($query) {
                    $query->full()->orderBy('block_entry.position', 'asc');
                },
                'mediaVideos' => function ($query) {
                    $query->full()->orderBy('website_objects.position', 'asc');
                },
                'miniDocumentaries' => function ($query) {
                    $query->full()->where('status', 'published')->orderBy('entry_mini_documentary.position', 'asc');
                },
                'partners' => function ($query) {
                    $query->full()->orderBy('website_objects.position', 'asc');
                },
                'products' => function ($query) {
                    $query->full()->orderBy('entry_product.position', 'asc');
                },
                'videoThumbnail',
                'docsThumbnail',
                'webseriesThumbnail',
                'link1Thumbnail',
                'link2Thumbnail',
                'link3Thumbnail',
                'link4Thumbnail'
            ]);
        }
        $entry = $query->first();

        if ($entry) {
            return new Entries($entry);
        }
    }
}
