<?php

namespace Modules\Internal\Http\Controllers\Api;

use App\Models\Benefit;
use App\Models\Claim;
use App\Models\Icd;
use App\Models\Member;
use App\Services\ClaimService;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class ClaimController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index()
    {
        $claims = Claim::with([
                            'member',
                            'diagnosis',
                            'plan', 
                            'benefit'
                        ])
                        ->latest()
                        ->paginate(10);

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

    /**
     * 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)
    {
        $request->validate([
            'diagnosis_id' => 'required',
            'member_id' => 'required',
            'total_claim' => 'required',
            'benefit_id' => 'required'
        ]);

        // return response()->json($request->toArray());

        $member = Member::find($request->member_id);
        $benefit = Benefit::find($request->benefit_id);
        $diagnosis = Icd::find($request->diagnosis_id);

        // Check Eligibility
        $validation = ClaimService::checkMemberEligibility($member, $benefit, $diagnosis, $request->total_claim);

        // Store Claim
        if ($validation['isEligible']) {
            $claim = ClaimService::storeClaim($member, $diagnosis, $request->total_claim, $benefit, 'requested');
        } else {
            return response()->json([
                'data' => $validation,
                'message' => $validation['errors'][0]['message']
            ], 403);
        }

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

    /**
     * Show the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function show($id)
    {
        $claim = Claim::query()
                ->with([
                    'member', 
                    'member.currentPlan', 
                    'diagnosis',
                    'benefit',
                    'files'
                    ])
                ->findOrFail($id);

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

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

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

    public function checkLimit(Request $request)
    {
        return true;
    }
}
