<?php

namespace App\Http\Controllers\Dashboard\UsefulInfoController;

use App\Http\Controllers\Controller as ExController;
use App\Models\UsefulInfo;

use App\Http\Requests\Dashboard\UsefulInfoRequest\Update as UpdateRequest;
use App\Http\Requests\Dashboard\UsefulInfoRequest\Store as StoreRequest;

use App\Jobs\Dashboard\UsefulInfoJob\Store as StoreJob;
use App\Jobs\Dashboard\UsefulInfoJob\Update as UpdateJob;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

class Controller extends ExController
{
    public function index()
    {
        $this->authorize('view', 'usefulinfos');

        $usefulinfos = UsefulInfo::orderBy('position')->latest('id')->paginate(20);
        return view('dashboard.usefulinfos.index', compact('usefulinfos'));
    }

    public function create()
    {
        $this->authorize('create', 'usefulinfos');
        return view('dashboard.usefulinfos.create');
    }

    public function store(StoreRequest $request)
    {
        if ($request->hasFile('image')) {
            $path =  $request->file('image')->store('uploads/usefulinfos');
        }

        $this->dispatchSync(StoreJob::fromRequest($request, $path));

        $this->info(trans('admin.messages.created'));
        return redirect()->route('dashboard.usefulinfos.index');
    }

    public function edit(UsefulInfo $usefulinfo)
    {
        $this->authorize('update', 'usefulinfos');
        return view('dashboard.usefulinfos.edit', compact('usefulinfo'));
    }

    public function update(UpdateRequest $request, UsefulInfo $usefulinfo)
    {
        if ($request->hasFile('image')) {
            if (env('FILESYSTEM_DISK') == 's3') {
                $imagePath = $usefulinfo->image;
                Storage::disk('s3')->delete($imagePath);
            } else {
                $imagePath = public_path($usefulinfo->image);

                if (File::exists($imagePath)) {
                    File::delete($imagePath);
                }
            }

            $path =  $request->file('image')->store('uploads/usefulinfos');
        } else {
            $path = $usefulinfo->image;
        }

        $this->dispatchSync(UpdateJob::fromRequest($usefulinfo, $request, $path));

        $this->info(trans('admin.messages.updated'));
        return redirect()->route('dashboard.usefulinfos.index');
    }

    public function destroy(UsefulInfo $usefulinfo)
    {
        $this->authorize('delete', 'usefulinfos');

        // delete items files
        foreach ($usefulinfo->items as $item) {
            $filePath = public_path($item->file_url);

            if (File::exists($filePath)) {
                File::delete($filePath);
            }

            $item->delete();
        }

        if (env('FILESYSTEM_DISK') == 's3') {
            $imagePath = $usefulinfo->image;
            Storage::disk('s3')->delete($imagePath);
        } else {
            $imagePath = public_path($usefulinfo->image);

            if (File::exists($imagePath)) {
                File::delete($imagePath);
            }
        }

        $usefulinfo->delete();

        $this->info(trans('admin.messages.deleted'));
        return redirect()->back();
    }
}
