<?php

namespace App\Http\Controllers\Api\OLDLMS;

use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Models\Member;
use App\Services\ClaimService;
use Illuminate\Http\Request;

class MembershipController extends Controller
{
    //
    public function check(Request $request) 
    {
        $request->validate([
            'member_id' => 'required',
            'birth_date' => 'required',
        ]);

        $member = Member::where('member_id', $request->member_id)->first();

        if (!$member) {
            return Helper::responseJson(statusCode: 404, message: 'Member not found.', status: 'error');
        }

        if (!$member->active) {
            return Helper::responseJson(statusCode: 406, message: 'The Member '.$request->member_id.' is Inactive.', status: 'error');
        }

        
        return Helper::responseJson(data: $member, message: 'Member Found');
    }

    public function checkLimit(Request $request)
    {
        $request->validate([
            'member_id' => 'required',
            'type' => 'required|in:consultation-gp,consultation-specialist,medicine'
        ]);

        if ($request->type == 'consultation-gp') {
            $benefitCode = 'OPCONS1';
        }
        if ($request->type == 'consultation-specialist') {
            $benefitCode = 'OPCONS2';
        }
        if ($request->type == 'medicine') {
            $benefitCode = 'OPMEDI1';
        }

        $member = Member::where('member_id', $request->member_id)->with(['currentCorporate', 'currentPolicy', 'currentPlan'])->first();

        $limits = ClaimService::showMemberBenefitLimit($member, $benefitCode);

        return Helper::responseJson(data: $limits);
    }
}
