<?php

namespace App\Http\Controllers;

use App\Http\Requests\Destination\StoreDestinationRequest;
use App\Http\Requests\Destination\UpdateDestinationRequest;
use App\Http\Resources\DestinationCollection;
use App\Http\Resources\DestinationResource;
use App\Models\Attend;
use App\Models\Destination;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class DestinationController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(): JsonResponse
    {

        return response()->json([
            'success' => true,
            'message' => 'Destinations indexed successfully.',
            'data' => Destination::all()
        ], 200);
    }

    public function indexByMe() {
        $destinations = Destination::where('user_id', Auth::id())->get();

        return response()->json([
            'success' => true,
            'message' => 'Users destinations indexed successfully.',
            'data' => $destinations
        ], 200);
    }

    public function indexMine() {
        $attends = Attend::where('user_id', Auth::id())->select('destination_id')->get();
        $destinations = [];

        foreach ($attends as $attend) {
            $destinations[] = Destination::where('id', $attend['destination_id'])->first();
        }

        return response()->json([
            'success' => true,
            'message' => 'Users destinations indexed successfully.',
            'data' => $destinations
        ], 200);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreDestinationRequest $request): JsonResponse
    {
        $validated = $request->validated();

        $validated['image_url'] = $request->file('image')->store('public/images');
        unset($validated['image']);

        $validated['user_id'] = Auth::user()['id'];

        $destinationResource = new DestinationResource(Destination::create($validated));

        return response()->json([
            'success' => true,
            'message' => 'Destination created successfully.',
            'data' => [
                'destination' => $destinationResource
            ]
        ], 201);
    }

    /**
     * Display the specified resource.
     */
    public function show(Destination $destination): JsonResponse
    {
        $destinationResource = new DestinationResource($destination);

        return response()->json([
            'success' => true,
            'message' => 'Destination found successfully.',
            'data' => $destinationResource
        ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateDestinationRequest $request, Destination $destination): JsonResponse
    {
        $validated = $request->validated();

        if (Auth::user()['id'] !== $destination['user_id']) {
            return response()->json([
                'success' => false,
                'message' => 'Unauthorized.'
            ], 401);
        }

        $validated['image_url'] = $request->file('image')->store('public/images');
        unset($validated['image']);

        $destination->update($validated);
        $destinationResource = new DestinationResource($destination);

        return response()->json([
            'success' => true,
            'message' => 'Destination updated successfully.',
            'data' => [
                'destinations' => $destinationResource
            ]
        ]);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Destination $destination): JsonResponse
    {
        if (Auth::user()['id'] !== $destination['user_id']) {
            return response()->json([
                'success' => false,
                'message' => 'Unauthorized.'
            ], 401);
        }

        $destination->delete();

        return response()->json([
            'success' => true,
            'message' => 'Destination removed successfully.',
        ]);
    }

    public function search(string $searchTerm)
    {
        $query = Destination::query();

        if ($searchTerm) {
            $query->where('title', 'like', "%$searchTerm%")
                ->orWhere('description', 'like', "%$searchTerm%");
        }

        $destinations = $query->get();

        return response()->json([
            'success' => true,
            'message' => 'Search executed successfully.',
            'data' => [
                'destinations' => $destinations
            ]
        ]);
    }
}
