<?php

namespace Modules\Internal\Http\Controllers\Api;

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

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

        return $benefits;
    }

    public function activation(Request $request, $plan_id)
    {
        $request->validate([
            'active' => 'required'
        ]);

        // abort(404);

        $plan = CorporatePlan::findOrFail($plan_id);
        $plan->active = $request->active == '1';

        if ($plan->save()) {
            return response()->json([
                'plan' => $plan,
                '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_plans')->where('corporate_id', $corporate_id)
            ],
            'name' => 'required'
        ]);

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

        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 = CorporatePlan::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 = CorporatePlan::findOrFail($id);
        $request->validate([
            'code' => [
                'required',
                Rule::unique('corporate_plans')->where('corporate_id', $corporate_id)->ignore($corporatePlan->id)
            ],
            'name' => 'required'
        ]);

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

        return $corporatePlan;
    }

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