<?php

namespace App\Http\Controllers\Admin;

use Exception;
use App\Models\ImageGallery;
use Illuminate\Http\Request;
use App\Services\ImageGalleryService;
use App\Http\Requests\ImageGalleryRequest;
use App\Http\Requests\PaginateRequest;
use App\Http\Resources\ImageGalleryResource;

class ImageGalleryController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    private ImageGalleryService $imageGalleryService;

    public function __construct(ImageGalleryService $imageGallery)
    {
        parent::__construct();
        $this->imageGalleryService = $imageGallery;
    }

    public function index(PaginateRequest $request)
    {
        try {
            return ImageGalleryResource::collection($this->imageGalleryService->list($request));
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ImageGalleryRequest $request): \Illuminate\Http\Response | ImageGalleryResource | \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\Routing\ResponseFactory
    {
        try {
            return new ImageGalleryResource($this->imageGalleryService->store($request));
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(ImageGallery $imageGallery): ImageGalleryResource | \Illuminate\Http\Response
    {
        try {
            return new ImageGalleryResource($this->imageGalleryService->show($imageGallery));
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ImageGalleryRequest $request, ImageGallery $imageGallery): ImageGalleryResource | \Illuminate\Http\Response
    {
        try {
            return new ImageGalleryResource($this->imageGalleryService->update($request, $imageGallery));
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(ImageGallery $imageGallery)
    {
        try {
            $this->imageGalleryService->destroy($imageGallery);
            return response('', 202);
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }


    public function sortImageGallery(Request $request): \Illuminate\Foundation\Application|\Illuminate\Http\Response|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory
    {
        try {
            $this->imageGalleryService->sortImageGallery($request);
            return response('', 202);
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }
}