<?php

namespace App\Http\Controllers\Api\OLDLMS;

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

class ClaimController extends Controller
{
    public function store(Request $request)
    {
        $request->validate([
            'member_id' => 'required',
            'user_id' => 'required',
            'type' => 'required|in:consultation-gp,consultation-specialist,medicine',
            'total_claim' => 'required',
            'detail' => 'required',
        ]);
        
        if ($request->type == 'consultation-gp') {
            $benefitCode = 'OPCONS1';
        }
        if ($request->type == 'consultation-specialist') {
            $benefitCode = 'OPCONS2';
        }
        if ($request->type == 'medicine') {
            $benefitCode = 'OPMEDI1';
        }

        $member = Member::query()
                        ->where('member_id', $request->member_id)
                        ->with([
                            'currentPlan',
                        ])
                        ->firstOrFail();
        $benefit = $member->currentPlan->benefits()->where('benefit_code', $benefitCode)->first();
        // $diagnosis = Icd::first();

        $claim = ClaimService::storeClaim($member, null, $request->total_claim, $benefit, 'approved');
        
        return Helper::responseJson($claim);
    }
}
