<?php

namespace Modules\Internal\Http\Controllers\Api;

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

class CorporateBenefitController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index(Request $request, $corporate_id)
    {
        $benefits = CorporateBenefit::query()
            ->filter($request->all())
            ->where('corporate_id', $corporate_id)
            ->with('benefit', 'plan')
            ->paginate(10)
            ->appends($request->all());
        return $benefits;
    }
    
    public function activation(Request $request, $benefit_id)
    {
        $request->validate([
            'active' => 'required'
        ]);

        // abort(404);
        $benefit = CorporateBenefit::find($benefit_id);
        $benefit->active = $request->active == 1 ? 0 : 1;
        $benefit->reason = $request->reason;

        if ($benefit->save()) {
            return response()->json([
                'benefit' => $benefit,
                'message' => 'Status Updated Successfully'
            ]);
        }
    }
    /**
     * 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',
                Rule::unique('corporate_benefits')->where('corporate_id', $corporate_id)
            ],
            'name' => 'required'
        ]);

        $newCorporatePlan = CorporateBenefit::create([
            'corporate_id' => $corporate_id,
            'code' => $request->code,
            'name' => $request->name,
        ]);

        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)
    {
        $benefit = CorporateBenefit::findOrFail($id);

        return $benefit;
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(Request $request, $corporate_id, $id)
    {

        $corporateBenefit = CorporateBenefit::findOrFail($id);
      
        $request->validate([
            'budget' => [
                'required',
            ],
        ]);

        $corporateBenefit->fill([
            'budget' => $request->budget,
        ])->save();

        return $corporateBenefit;
    }

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