<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\CreateFishTypeRequest;
use App\Http\Requests\UpdateFishTypeRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use App\Services\FishTypesService;
use App\Models\Fish_type;
use Exception;

class FishTypesController extends Controller
{
    protected $model, $pagesModel;
    protected FishTypesService $fishTypesService;

    public function __construct(FishTypesService $fishTypesService)
    {
        $this->model = new Fish_type();
        $this->fishTypesService = $fishTypesService;
    }

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

    // Show the form for creating a new type.
    public function create()
    {
        return $this->fishTypesService->displayView('dashboard.fishes.types.create');
    }

    // Store a newly created type.
    public function store(CreateFishTypeRequest $request)
    {
        try{
            $this->fishTypesService->store($this->model, $request->toArray());
            Session::flash('success', 'Fish Type 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 type.
    public function edit($id)
    {
        $type = $this->fishTypesService->show($this->model, $id);
        return $this->fishTypesService->displayView('dashboard.fishes.types.update', [
            'type' => $type,
        ]);
    }

    //Update the specified resource in storage.
    public function update(UpdateFishTypeRequest $request, $id)
    {
        try{
            $this->fishTypesService->update($this->model, $id, $request->toArray());
            Session::flash('success', 'Fish Type updates successfully');
        } catch(Exception $e) {
            Session::flash('error', 'Something went wrong');
        }

        return Redirect::back();
    }

    // Remove the specified resource from storage.
    public function destroy($id)
    {
        try{
            $this->fishTypesService->delete($this->model, $id);
            Session::flash('success', 'Fish Type deleted successfully');
        } catch(\Exception $e) {
            Session::flash('error', 'Something went wrong');
        }

        return Redirect::back();
    }
}
