<?php

namespace App\Http\Controllers\Admin\Pages\Galleries;

use App\Http\Controllers\Controller;
use App\Http\Requests\Certificates\CertificateStoreRequest;
use App\Http\Requests\Certificates\CertificateUpdateRequest;
use App\Http\Requests\Galleries\GalleryStoreRequest;
use App\Http\Requests\Galleries\GalleryUpdateRequest;
use App\Models\Certificate;
use App\Models\Gallery;
use App\Traits\Description;
use App\Traits\FileUploader;
use App\Traits\ImageCompressor;
use App\Traits\Slug;
use App\Traits\Title;
use DataTables;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\File;

class GalleryController extends Controller
{

    use ImageCompressor ,FileUploader,Slug ,Title,Description;

    public function __construct()
    {
        $this->middleware('permission:view galleries')->only('index', 'ajax');
        $this->middleware('permission:create gallery')->only('create');
        $this->middleware('permission:store gallery')->only('store');
        $this->middleware('permission:edit gallery')->only('edit');
        $this->middleware('permission:update gallery')->only('update');
        $this->middleware('permission:destroy gallery')->only('destroy');
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $galleries =Gallery::all();
        return view('app.admin.pages.galleries.all-galleries',compact([
            'galleries'
        ]));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $galleries = Gallery::all();
        return view('app.admin.pages.galleries.create-gallery',compact([
            'galleries'
        ]));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(GalleryStoreRequest $request)
    {
        try {
            $validatedData = $request->validated();


            if ($request->hasFile('image')) {
                $validatedData['image'] = self::compressImage($request->file('image'), 'GalleryImage');
            }

            Gallery::create($validatedData);


            return back()->with('success', __('Data successfully created!'));
        } catch (\Exception $e) {
            return back()->withErrors([$e->getMessage()]);
        }
    }



    /**
     * Display the specified resource.
     */
    public function show(Gallery $gallery)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Gallery $gallery)
    {
        $galleries=Gallery::all();
        return view('app.admin.pages.galleries.edit-gallery',compact([
            'galleries',
            'gallery',
        ]));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(GalleryUpdateRequest $request, Gallery $gallery)
    {
        $validatedData = $request->safe();
        try {
            if ($request->hasFile('image')) {
                File::delete('/storage/images/' . $gallery->getAttribute('image'));
                $validatedData['image'] = self::compressImage($request->file('image'), 'GalleryImage');
            } else {
                $validatedData['image'] = $gallery->getAttribute('image');
            }

            $gallery->update($validatedData->except('partners'));


            return back()->with('success', __('Data successfully updated!'));
        } catch (Exception $e) {
            return back()->withErrors([$e->getMessage()]);
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        try {
            $gallery=Gallery::find($id);
            $gallery->delete();
            $gallery->gallery()->delete();
            return back()->with('success', __('Data successfully deleted!'));
        } catch (Exception $e) {
            return back()->with('error', $e->getMessage());
        }
    }

    /**
     * @return JsonResponse
     * @throws Exception
     */
    public function ajax()
    {
        $galleries = Gallery::query();
        return DataTables::of($galleries)->editColumn('image', function (Gallery $galleries) {
            return view('app.admin.includes.table-elements.image', [
                'image' => $galleries->image()
            ]);
        })->addColumn('options', function (Gallery $gallery) {
            return view('app.admin.includes.table-elements.action-buttons', [
                'modelName' => 'gallery',
                'editUrl' => route('admin:galleries.edit', $gallery->getAttribute('id')),
                'deleteUrl' => route('admin:galleries.destroy', $gallery->getAttribute('id'))
            ]);
        })->toJson();
    }
}
