<?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 filter(Request $request, $corporate_id){
        
        $benefits = CorporatePlan::query()
                        ->filter($request->all())
                        ->where('corporate_id', $corporate_id);
                        // ->where('type', $request->type)
                        // ->where('code', $request->code);

        if ($request->has('service_code') && is_array($request->service_code) && count($request->service_code) > 0) {
            $benefits->whereIn('service_code', $request->service_code);
        }
        if ($request->has('type') && is_array($request->type) && count($request->type) > 0) {
            $benefits->whereIn('type', $request->type);
        }
        if ($request->has('code') && is_array($request->code) && count($request->code) > 0) {
            $benefits->whereIn('code', $request->code);
        }

       
        $benefits = $benefits->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 ? 0 : 1;
        $plan->reason = $request->reason;

        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)
            ],
        ]);
        $corporatePlan->fill([
            'code' => $request->code,
            'corporate_plan_id' => $request->plan,
            'service_code' => $request->service,
            'type' => $request->type,
            'limit_rules' => $request->limit
        ])->save();
        
        return $corporatePlan;
    }

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