<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\GenericBackoffice\Entry;
use App\Http\Requests\Api\BaseShowWithPassword;
use App\Http\Resources\Entries;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\JsonResource;

class EntryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param Entry $request
     * @return JsonResource|JsonResponse
     */


    public function index(Entry $request)
    {
        $query = Entry::full()
            ->select('entries.*', 'entries_lang.*', 'website_objects.*', 'website_objects_lang.*')
            ->where('status', 'published')
            ->orderBy('website_objects.website_object_id', 'desc');


        $entry = $query->get();
        if ($entry) {
            return Entries::collection($entry);
        }
        return response()->json([], 404);

    }


    /**
     * Display the specified resource.
     *
     * @param BaseShowWithPassword $request
     * @return JsonResource|JsonResponse
     */
    public function show(BaseShowWithPassword $request)
    {
        $data = $request->validated();
        $query = Entry::full()
            ->select('*')
            ->where('status', 'published')
            ->where('lang_slug', $data['slug'])
            ->orderBy('position', 'asc');

        $entry = $query->first();        // Check if the entry is password protected
        if ($entry && !empty($entry->password_protected)) {
            // If protected but no password provided
            if (empty($data['pwd'])) {
                return response()->json([
                    'message' => 'This entry is password protected. Please provide a password.',
                    'requires_password' => true
                ], 401); // 401 Unauthorized
            }

            // If the provided password doesn't match
            if ($data['pwd'] !== $entry->password_protected) {
                return response()->json([
                    'message' => 'Invalid password.',
                    'requires_password' => true
                ], 403); // 403 Forbidden
            }

            // Correct password, continue loading the entry
        }

        // Load relationships and return the entry
        if ($entry) {
            $query->with([
                'blocks' => function ($query) {
                    $query
                        ->full()
                        ->with(['timelineCards' => function ($query) {
                            $query
                                ->full()
                                ->orderBy('position', 'asc');
                        }])
                        ->with(['cardItems' => function ($query) {
                            $query
                                ->full()
                                ->orderBy('position', 'asc');
                        }])
                        ->with(['textCards' => function ($query) {
                            $query
                                ->full()
                                ->orderBy('position', 'asc');
                        }])
                        ->with(['accordionItems' => function ($query) {
                            $query
                                ->full()
                                ->with(['fileCards' => function ($query) {
                                    $query
                                        ->full()
                                        ->orderBy('position', 'asc');
                                }])
                                ->orderBy('position', 'asc');
                        }])
                        ->with(['trackerCategories' => function ($query) {
                            $query
                                ->full()
                                ->with(['trackerActions' => function ($query) {
                                    $query
                                        ->full()
                                        ->orderBy('position', 'asc');
                                }])
                                ->orderBy('position', 'asc');
                        }])
                        ->with(['form' => function ($query) {
                            $query
                                ->full()
                                ->with(['formSections' => function ($query) {
                                    $query
                                        ->full()
                                        ->with(['formFields' => function ($query) {
                                            $query
                                                ->full()
                                                ->with(['fieldOptions' => function ($query) {
                                                    $query
                                                        ->full()
                                                        ->orderBy('position', 'asc');
                                                }])
                                                ->orderBy('position', 'asc');
                                        }])
                                        ->orderBy('position', 'asc');
                                }])
                                ->orderBy('position', 'asc');
                        }])
                        ->with(['teamMembers' => function ($query) {
                            $query
                                ->full()
                                ->orderBy('block_team_member.position', 'asc');
                        }])
                        ->with(['slider' => function ($query) {
                            $query
                                ->full()
                                ->orderBy('block_slider_item.position', 'asc');
                        }])
                        ->with(['imageGallery' => function ($query) {
                            $query
                                ->full()
                                ->orderBy('block_image_item.position', 'asc');
                        }])
                        ->with(['links' => function ($query) {
                            $query
                                ->full()
                                ->where('status', 'published')
                                ->orderBy('block_link.position', 'asc');
                        }])
                        ->where('status', 'published')
                        ->orderBy('entry_block.position', 'asc');
                }
            ]);

            $entryWithRelations = $query->first();
            return new Entries($entryWithRelations);
        }

        return response()->json([], 404);
    }
}
