<?php

namespace App\Http\Controllers;

use App\Models\Client;
use App\Models\ClientProAccess;
use App\Models\ClientReviewRequest;
use Illuminate\Http\Request;
class ClientProAccessController extends Controller
{

    public function dashboard(Request $request){
        $performer = $this->performer();
        $pro = $performer->pro;
        $performerProID = $performer->pro->id;

        $milliseconds = strtotime(date('Y-m-d')) . '000'; //required by the calendar
        return view('app.ca.dashboard', compact( 'milliseconds'));
    }

    public function patients(Request $request)
    {
        $filters = $request->all();
        $patients = Client::query();

        $proAccessClientIDs = ClientProAccess::where('pro_id', $this->performer->pro->id)->pluck('client_id')->toArray();
        $patients = $patients->orWhereIn('id', $proAccessClientIDs);


        if ($request->input('name')) {
            $name = trim($request->input('name'));
            if ($name) {
                $patients = $patients->where(function ($q) use ($name) {
                    $q->where('name_first', 'ILIKE', '%' . $name . '%')
                        ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
                });
            }
        }

        if ($request->input('home_address_state')) {

            if($request->input('home_address_state') == 'NONE'){
                $patients = $patients->whereNull('mailing_address_state');
            }else if($request->input('home_address_state') == 'NOT_MD'){
                $patients = $patients->where('mailing_address_state', '<>' , 'MD');
            }else{
                $patients = $patients->where('mailing_address_state', '=' , $request->input('home_address_state'));
            }
        }

        $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2', false);
        $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
        $this->filterMultiQuery($request, $patients, 'usual_bmi_max', 'bmi_category', 'bmi_value_1', 'bmi_value_2', false);
        $this->filterMultiQuery($request, $patients, 'most_recent_completed_mcp_note_date', 'last_visit_category', 'last_visit_value_1', 'last_visit_value_2'); 
        $this->filterMultiQuery($request, $patients, 'next_mcp_appointment_date', 'next_appointment_category', 'next_appointment_value_1', 'next_appointment_value_2');

        switch($request->input('status')) {
            case 'ACTIVE':
                $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', true);
                break;
            case 'AWAITING_VISIT':
                $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', false);
                break;
            case 'INACTIVE':
                $patients->where('is_active', '<>', true);
                break;
        }
        $sortBy = $request->input('sort_by') ?: 'name_first';
        $sortDir = $request->input('sort_dir') ?: 'ASC';

        $patients = $patients->orderByRaw("$sortBy $sortDir NULLS LAST");
        
        $patients = $patients->orderBy('created_at', 'DESC')->paginate(20);
        return view('app.ca.patients', compact('patients', 'filters'));
    }
    public function clientReviewRequests(Request $request){
        $hideTitle = $request->get('hideTitle');
        $performer = $this->performer();
        $pro = $performer->pro;
        $reviewRequests = ClientReviewRequest::where('pro_id', $pro->id)->where('is_active', true)->orderBy('created_at', 'DESC')->paginate(50);
        return view('app.ca.review-requests.list', compact('reviewRequests', 'hideTitle'));
    }
}
