<?php

namespace Modules\Internal\Http\Controllers\Api;

use App\Models\CorporateDivision;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Validation\Rule;

class DivisionController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index(Request $request, $corporate_id)
    {
        $benefits = CorporateDivision::query()
            ->filter($request->all())
            ->where('corporate_id', $corporate_id)
            ->orderBy('id', 'DESC')
            ->paginate(0)
            ->appends($request->all());

        return $benefits;
    }

    /**
     * Show the form for creating a new resource.
     * @return Renderable
     */
    public function create()
    {
        return view('internal::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param Request $request
     * @return Renderable
     */
    public function store(Request $request, $corporate_id)
    {
        $request->validate([
            'code' => [
                'required',
            ],
            'name' => 'required'
        ]);

        $newCorporatePlan = CorporateDivision::create([
            'corporate_id' => $corporate_id,
            'code' => $request->code,
            'name' => $request->name,
            'description' => $request->description ? $request->description : null,
        ]);

        return $newCorporatePlan;
    }

    /**
     * Show the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function show($id)
    {
        return view('internal::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function edit($corporate_id, $id)
    {
        $corporatePlan = CorporateDivision::findOrFail($id);

        return $corporatePlan;
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(Request $request, $corporate_id, $id)
    {
        $corporatePlan = CorporateDivision::findOrFail($id);
        $request->validate([
            'code' => [
                'required',
            ],
            'name' => 'required'
        ]);

        $corporatePlan->fill([
            'code' => $request->code,
            'name' => $request->name,
            'description' => $request->description,
        ])->save();

        return $corporatePlan;
    }

    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return Renderable
     */
    public function destroy($id)
    {
        //
    }
}
