<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreServiceRequest;
use App\Http\Requests\UpdateServicesRequest;
use App\Models\IprsNewService;
use App\Models\IprsNewServiceGroup;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ServiceController extends Controller
{
    /*
     * @var service_group
     *
     * */
    protected $service_group;
    /*
     * @var service_group
     *
     * */
    protected $services;

    /**
    *Initialize class with instances
     */
    public function __construct(){
        $this->service_group=IprsNewServiceGroup::class;
        $this->services =IprsNewService::class;
    }

    /**
     * Display a listing of the resource.
     * @return view
     */
    public function index(): view
    {
        $services =$this->services::with('iprsNewServiceGroup')->get();
        $service_groups=$this->service_group::select('id','SERVICE_NAME')->get();
        return view('services.service', [ 'services' => $services, 'service_groups'=>$service_groups ]);
    }

    /**
     * Show the form for creating a new resource.
     * @return view
     */
    public function create(): view
    {
        $service_groups=$this->service_group::select('id','SERVICE_NAME')->get();
        //return
        return view('service.serviceForm', ['service_groups'=>$service_groups]);
    }

    /**
     * @param StoreServiceRequest $request
     * @return RedirectResponse
     */
    public function store(StoreServiceRequest $request): RedirectResponse
    {
        $validated =$request->validated();

        IprsNewService::create([
            'SERVICE_NAME' => $validated['service_name'],
            'SERVICE_GROUP_ID' => $validated['service_group_id'],
            'SERVICE_FIELD_ID'=>$validated['service_field_id']
        ]);

        return redirect()->route('service.index')->with('Service created!');
    }

    /**
     * Display the specified resource.
     */
    public function show(IprsNewService $iprsNewService)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(IprsNewService $service)
    {
        $service_groups=$this->service_group::select('id','SERVICE_NAME')->get();
        //redirect
        return view('services.serviceEdit', ['service'=>$service, 'service_groups'=>$service_groups]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateServicesRequest $request, IprsNewService $service)
    {
        $validated =$request->validated();
        IprsNewService::query()->where('id', $service->id)->update([
            'SERVICE_NAME' => $validated['service_name'],
            'SERVICE_GROUP_ID' => $validated['service_group_id'],
            'SERVICE_FIELD_ID'=>$validated['service_field_id']
        ]);
        return redirect()->route('service.index')->with('Service updated');
    }
    /**
     * Remove the specified resource from storage.
     */
    public function destroy(IprsNewService $iprsNewService)
    {
        //
    }
}
