<?php

namespace App\Http\Controllers\Dashboard\Service;

use App\Http\Controllers\Controller as ExController;
use App\Models\Service;
use App\Http\Requests\Dashboard\Service\Update as UpdateRequest;
use App\Http\Requests\Dashboard\Service\Store as StoreRequest;
use App\Jobs\Dashboard\Service\Store as StoreJob;
use App\Jobs\Dashboard\Service\Update as UpdateJob;
use App\Models\Problem;
use App\Models\ServiceProblem;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

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

        $services = Service::latest('id')->paginate(20);
        return view('dashboard.services.index', compact('services'));
    }

    public function create()
    {
        $this->authorize('create', 'services');

        $problems = Problem::all();
        return view('dashboard.services.create', compact('problems'));
    }

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

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

        if ($request->with_problem != null) {
            foreach ($request->problems as $problem) {
                ServiceProblem::create([
                    'service_id' => $service->id,
                    'problem_id' => $problem
                ]);
            }
        }

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

    public function edit(Service $service)
    {
        $this->authorize('update', 'services');
        $problems = Problem::all();
        return view('dashboard.services.edit', compact('service', 'problems'));
    }

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

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

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

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

        $existingProblemIds = $service->problems->pluck('id')->toArray();
        $newProblemIds = $request->problems ?? [];

        $toDelete = array_diff($existingProblemIds, $newProblemIds);
        $toAdd = array_diff($newProblemIds, $existingProblemIds);

        if ($request->with_problem != null) {
            foreach ($toAdd as $problem) {
                ServiceProblem::create([
                    'service_id' => $service->id,
                    'problem_id' => $problem
                ]);
            }
        } else {
            ServiceProblem::where('service_id', $service->id)->delete();
        }
        ServiceProblem::whereIn('problem_id', $toDelete)->delete();

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

    public function destroy(Service $service)
    {
        $this->authorize('delete', 'services');

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

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

        ServiceProblem::where('service_id', $service->id)->delete();

        $service->delete();

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