<?php

namespace Modules\Internal\Http\Controllers\Api;

use App\Helpers\Helper;
use App\Models\Corporate;
use App\Models\CorporateService;
use App\Models\CorporateServiceConfig;
use App\Models\CorporateServiceSpeciality;
use App\Models\Speciality;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Internal\Transformers\CorporateServiceConfigResource;

class CorporateServiceController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index(Request $request, $corporate_id)
    {
        $services = CorporateService::with('configs', 'service')->where('corporate_id', $corporate_id)->filter($request->toArray())->paginate();

        return Helper::paginateResources(CorporateServiceConfigResource::collection($services));
    }

    /**
     * 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)
    {
        //
    }

    /**
     * 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($id)
    {
        return view('internal::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(Request $request, $corporate_id)
    {
        $corporateService = CorporateService::where('corporate_id', $corporate_id)->where('service_code', $request->service_code)->first();

        $corporateServiceConfig = $corporateService->configs()->updateOrCreate([
            'corporate_service_id' => $corporateService->id,
            'name' => $request->config_name
        ], [
            'name' => $request->config_name,
            'value' => $request->config_value
        ]);

        return $corporateServiceConfig;
    }

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

    public function corporateServiceIndex($corporate_id, $service_code)
    {
        $corporate = Corporate::findOrFail($corporate_id);
        $corporateService = CorporateService::query()
            ->where('corporate_id', $corporate_id)
            ->where('service_code', $service_code)
            ->with([
                'configs', 'service',
                'specialities' => function ($speciality) {
                    $speciality->where('active', true);
                },
                'specialities.speciality',
                'specialities.exclusions.rules'
            ])
            ->first();

        // $service = CorporateServiceConfigResource::make($corporateService);
        $specialities = Speciality::get();

        return response()->json(
            [
                'corporate' => $corporate,
                'service' => CorporateServiceConfigResource::make($corporateService),
                'specialities' => $specialities,
            ]
        );
    }

    public function corporateServiceUpdate(Request $request, $corporate_id, $service_code)
    {
        // $corporate = Corporate::findOrFail($corporate_id);
        $corporateService = CorporateService::query()
            ->where('corporate_id', $corporate_id)
            ->where('service_code', $service_code)
            // ->with('configs', 'service')
            ->first();
        $corporateService->fill([
            'status' => $request->status == 'active' ? 'active' : 'inactive'
        ]);
        $corporateService->save();

        return response()->json($corporateService);
    }

    public function corporateServiceSpecialityUpdate(Request $request, $corporate_id, $service_code)
    {


        // return response()->json([$request->msc, $request->name, $request->speciality_id]);

        $corporateService = CorporateService::query()
            ->where('corporate_id', $corporate_id)
            ->where('service_code', $service_code)
            ->first();


        $corporateServiceSpeciality = CorporateServiceSpeciality::updateOrCreate([
            'corporate_service_id' => $corporateService->id,
            'speciality_id' => $request->speciality_id,
        ], [
            'corporate_service_id' => $corporateService->id,
            'speciality_id' => $request->speciality_id,
            'active' => $request->active
        ]);

        $exclusion =  $corporateServiceSpeciality->exclusions()->updateOrCreate([
            'corporate_id' => $corporate_id,
            'service_code' => $service_code,
        ], [
            'corporate_id' => $corporate_id,
            'service_code' => $service_code,
            'type' => 'speciality',
        ]);


        $selected_specialities = CorporateServiceSpeciality::query()
            ->where('corporate_service_id', $corporateService->id)
            ->where('active', true)
            ->with('speciality')
            ->get()
            ->pluck('speciality.name', 'speciality.id');

        return response()->json($selected_specialities);
    }

    public function storeExclusion(Request $request, $corporate_id, $service_code)
    {

        $corporateService = CorporateService::query()
            ->where('corporate_id', $corporate_id)
            ->where('service_code', $service_code)
            ->first();

        $corporateServiceSpeciality = CorporateServiceSpeciality::where('corporate_service_id', $corporateService->id)
            ->where('speciality_id', $request->speciality_id)
            ->first();

        // return ([$corporateServiceSpeciality, $request->msc, $request->name, $request->speciality_id, $request->type]);

        $exclusion =  $corporateServiceSpeciality->exclusions()->updateOrCreate([
            'corporate_id' => $corporate_id,
            'service_code' => $service_code,
        ], [
            'corporate_id' => $corporate_id,
            'service_code' => $service_code,
            'type' => 'speciality',
        ]);


        if ($request->type == 'msc') {
            if ($request->name == 'member' && $request->msc == "1") {
                $m = "m";
            } else {
                $m = "";
            }

            if ($request->name == 'spouse' && $request->msc == "1") {
                $s = "s";
            } else {
                $s = "";
            }

            if ($request->name == 'child' && $request->msc == "1") {
                $c = "c";
            } else {
                $c = "";
            }

            $values = $m . $s . $c;




            $exclusion_rule = $exclusion->rules()->updateOrCreate([
                'exclusion_id' => $exclusion->id,
                'name' => 'msc',
            ], [
                'name' => 'msc',
                'values' => $values,
            ]);
        }

        $selected_specialities = CorporateServiceSpeciality::query()
            ->where('corporate_service_id', $corporateService->id)
            ->where('active', true)
            ->with('speciality')
            ->get()
            ->pluck('speciality.name', 'speciality.id');

        return response()->json($selected_specialities);
    }
}
