<?php

namespace Modules\Client\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 Modules\Client\Transformers\MemberResources;

class CorporateMemberController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index(Request $request, $corporate_id)
    {
        $limit = $request->has('per_page') ? $request->per_page : 10;

        $members = Member::query()
            ->whereHas('employeds', function ($corporateEmployee) use ($corporate_id) {
                $corporateEmployee->where('corporate_id', $corporate_id);
            })->when($request->input('search'), function ($query, $search) {
                $query->where('member_id', 'like', "%" . $search . "%")
                    ->orWhere('name', 'like', "%" . $search . "%");
            });

        if ($request->input('claimMember') === 'false') {
            $members = $members->when($request->input('division'), function ($division, $division_id) {
                $division->whereHas('division', function ($corporateEmployee) use ($division_id) {
                    $corporateEmployee->where('division_id', $division_id);
                });
            })->when($request->has('orderBy'), function ($query) use ($request) {
                $query->orderBy($request->orderBy, $request->order);
            });
        }
        //  else {
        //     $members = $members->get();

        //     return response()->json(MemberResources::collection($members));
        // }
        $members->with('currentPlan');
        $members->withSum('claims', 'total_claim');

        $members = $members->paginate($limit);
        // return $members;
        
        return response()->json(Helper::paginateResources(MemberResources::collection($members)));
    }

    /**
     * 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)
    {
        return view('client::show');
    }

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