<?php

namespace Modules\Client\Http\Controllers\Api;

use App\Helpers\Helper;
use App\Models\Claim;
use App\Services\ClaimService;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Client\Transformers\ClaimShowResource;

class ClaimController extends Controller
{
    public function __construct(public ClaimService $claimService)
    {
    }

    public function status($corporate_id)
    {
        $claims = $this->claimService->getCountClaimRequestPerStatus($corporate_id);

        dd($claims);

        return Helper::responseJson([
            'count_requested' => 0,
            'count_approval' => 0,
            'count_disbrushment' => 0,
            'count_rejected' => 0,
        ]);
    }

    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index(Request $request, $corporate_id)
    {
        $claims = Claim::query()
            ->when($request->search ?? null, function ($query, $search) {
                $query->where('code', 'LIKE', '%' . $search . '%');
            })
            ->with([
                'member',
                'diagnoses' => function ($diagnosis) {
                    return $diagnosis->where('type', 'primary');
                },
                'diagnoses.icd',
                'plan',
                'benefit',
                'claimRequest',
                'claimRequest.service'
            ])
            ->paginate(10);

        return Helper::responseJson($claims);
    }

    /**
     * Show the form for creating a new resource.
     * @return Renderable
     */
    public function create()
    {
        return view('client::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)
    {
        $claim = Claim::query()
            ->with([
                'member',
                'member.currentPlan',
                'member.currentPlan.benefits',
                'member.currentCorporate',
                'member.currentPolicy',
                'diagnosis',
                'diagnoses',
                'benefit',
                'files',
                'claimRequest',
                'claimRequest.files',
                'items',
                'items.claim_itemable',
            ])
            ->findOrFail($id);

        return Helper::responseJson(ClaimShowResource::make($claim));
    }

    /**
     * Show the form for editing the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function edit($id)
    {
        return view('client::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)
    {
        //
    }
}
