<?php

namespace Modules\HospitalPortal\Http\Controllers\Api;

use App\Helpers\Helper;
use App\Models\Member;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Validator;
use Modules\HospitalPortal\Helpers\ApiResponse;
use Illuminate\Support\Facades\DB;

class MemberController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function search(Request $request)
    {
        $data = [
            'no_polis' => $request->no_polis,
            'birth_date' => $request->birth_date
        ];
        $validator = Validator::make($request->all(), [
            'no_polis' => 'required',
            'birth_date' => 'required'
        ], [
            'no_polis.required' => trans('Validation.required',['attribute' => 'Member ID']),
            'birth_date.required' => trans('Validation.required',['attribute' => 'Birth Date']),
        ]);
        if ($validator->fails())
        {
            return ApiResponse::apiResponse('Bad Request', $data, $validator->errors(), 400);
        }
        else
        {
            $members = DB::table('members')
            ->leftJoin('member_policies', 'member_policies.member_id','=', 'members.member_id')
            ->leftJoin('persons', 'persons.id', '=', 'members.person_id')
            ->where('members.member_id', '=', $request->no_polis)
            ->where('members.birth_date', '=', $request->birth_date)
            ->select(
                'members.id',
                'members.name',
                'members.member_id',
                'member_policies.policy_id',
                'persons.nik',
                'members.email',
                'members.birth_date',
                'members.gender',
                'members.marital_status',
                'members.language',
                'members.race',
                'members.relation_with_principal')
            ->first();
            if($members)
            {
                $res_data['members'] = $members;

                $benefits = DB::table('member_plans')
                ->leftJoin('corporate_benefits','corporate_benefits.plan_id', '=', 'member_plans.plan_id')
                ->leftJoin('benefits', 'benefits.id', '=', 'corporate_benefits.benefit_id')
                ->leftJoin('plans', 'plans.id', '=', 'member_plans.plan_id')
                ->leftJoin('services', 'services.code', '=', 'plans.service_code')
                ->where('member_plans.member_id', '=', $members->id)
                ->select(
                    'benefits.description',
                    'benefits.code',
                    'corporate_benefits.corporate_id',
                    'plans.service_code'
                    )
                ->get();
                $res_data['benefits'] = $benefits;

                $services = DB::table('member_plans')
                ->leftJoin('plans', 'plans.id', '=', 'member_plans.plan_id')
                ->leftJoin('services', 'services.code', '=', 'plans.service_code')
                ->where('member_plans.member_id', $members->id)
                ->whereNull('member_plans.deleted_at')
                ->select('plans.service_code', 'services.name')
                ->get();
                $res_data['services'] = $services;

                // Group Services
                $groupServices = [];
                foreach ($res_data['benefits'] as $benefit) {
                    $serviceCode = $benefit->service_code;
                    $groupServices[$serviceCode][] = [
                        'description' => $benefit->description,
                        'code' => $benefit->code,
                    ];
                }

                $res_data['groupServices'] = $groupServices;

                $res_data['type'] = $request->type;

                // Provider
                $providers = DB::table('organizations')
                ->where('organizations.type', '=', 'hospital')
                ->where('status', '=', 'active')
                ->select(
                    'organizations.id',
                    'organizations.name'
                )
                ->get();

                $res_data['providers'] = $providers;


                return ApiResponse::apiResponse("Success", $res_data, trans('Message.success'), 200);
            }
            else
            {
                return ApiResponse::apiResponse("Data Not Found", $data, trans('Message.not_found'), 404);
            }

        }
    }
}
