<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\CreateServiceRequest;
use App\Http\Requests\UpdateServiceRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use App\Services\ServicesService;
use App\Models\Service;
use Exception;

class ServicesController extends Controller
{
    protected $model;
    protected ServicesService $servicesService;

    public function __construct(ServicesService $servicesService)
    {
        $this->model = new Service();
        $this->servicesService = $servicesService;
    }

    // show services list page
    public function index()
    {
        $services = $this->servicesService->retrieve($this->model);
        return $this->servicesService->displayView('dashboard.services.list', ['services' => $services]);
    }

    // Show the form for creating a new service.
    public function create()
    {
        return $this->servicesService->displayView('dashboard.services.create');
    }

    // Store a newly created service.
    public function store(CreateServiceRequest $request)
    {
        try{
            $this->servicesService->storeService($request);
            Session::flash('success', 'Service created successfully');
        } catch(Exception $e) {
            Session::flash('error', 'Something went wrong');
        }

        return Redirect::back();
    }

    // Display the specified resource.
    public function show($id)
    {
        //
    }

    //Show the form for editing the specified service.
    public function edit($id)
    {
        $service = $this->servicesService->show($this->model, $id);
        return $this->servicesService->displayView('dashboard.services.update', ['service' => $service]);
    }

    // Update the specified service.
    public function update(UpdateServiceRequest $request, $id)
    {
        try{
            $this->servicesService->updateService($id, $request);
            Session::flash('success', 'Service updated successfully');
        } catch(Exception $e) {
            Session::flash('error', 'Something went wrong');
        }

        return Redirect::back();
    }

    // Remove the specified service.
    public function destroy($id)
    {
        try{
            $this->servicesService->deleteService($id);
            Session::flash('success', 'Service deleted successfully');
        } catch(Exception $e) {
            Session::flash('error', 'Something went wrong');
        }

        return Redirect::back();
    }
}
