<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\Dashboard\BeforeAfters\DeleteBeforeAftersRequest;
use App\Http\Requests\Dashboard\BeforeAfters\StoreBeforeAftersRequest;
use App\Http\Requests\Dashboard\BeforeAfters\UpdateBeforeAftersRequest;
use App\Models\BeforeAfter;
use App\Services\Dashboard\BeforeAfterService;
use Illuminate\Http\Request;

class BeforeAfterController extends Controller
{
    protected $beforeAfterService;

    public function __construct(BeforeAfterService $beforeAfterService)
    {
        $this->beforeAfterService = $beforeAfterService;
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $this->authorize('before_afters.view');

        $beforeAfters = BeforeAfter::orderBy('order')->orderBy('id', 'desc')->get();
        return view('Dashboard.BeforeAfters.index', compact('beforeAfters'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $this->authorize('before_afters.create');

        return view('Dashboard.BeforeAfters.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreBeforeAftersRequest $request)
    {
        $this->authorize('before_afters.store');

        try {
            $response = $this->beforeAfterService->store($request);

            if ($response) {
                return redirect()->route('dashboard.before-afters.index')->with('success', __('dashboard.your_item_added_successfully'));
            } else {
                return redirect()->back()->with('error', 'Error occurred while adding.');
            }
        } catch (\Exception $e) {
            return redirect()->back()->with('error', __('dashboard.failed_to_add_item') . ': ' . $e->getMessage());
        }
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(BeforeAfter $beforeAfter)
    {
        $this->authorize('before_afters.edit');

        return view('Dashboard.BeforeAfters.edit', compact('beforeAfter'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateBeforeAftersRequest $request, BeforeAfter $beforeAfter)
    {
        $this->authorize('before_afters.update');

        try {
            $response = $this->beforeAfterService->update($request, $beforeAfter);

            if ($response) {
                return redirect()->route('dashboard.before-afters.index')->with('success', __('dashboard.your_item_updated_successfully'));
            } else {
                return redirect()->back()->with('error', 'Error occurred while updating.');
            }
        } catch (\Exception $e) {
            return redirect()->back()->with('error', __('dashboard.failed_to_add_item') . ': ' . $e->getMessage());
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(DeleteBeforeAftersRequest $request)
    {
        $this->authorize('before_afters.delete');

        $selectedIds = $request->input('selectedIds');

        $deleted = $this->beforeAfterService->delete($selectedIds);

        if (request()->ajax()) {
            if (!$deleted) {
                return response()->json(['message' => __('dashboard.an_error_occurred')], 422);
            }
            return response()->json(['success' => true, 'message' => __('dashboard.your_items_deleted_successfully')]);
        }
        if (!$deleted) {
            return redirect()->back()->withErrors(__('dashboard.an_error_occurred'));
        }

        return redirect()->back()->with('success', __('dashboard.your_items_deleted_successfully'));
    }
}

