<?php

namespace App\Http\Controllers;

use App\Models\Appointment;
use App\Models\BDTDevice;
use App\Models\CareMonth;
use App\Models\Client;
use App\Models\ClientBDTDevice;
use App\Models\ClientInfoLine;
use App\Models\Erx;
use App\Models\Facility;
use App\Models\Handout;
use App\Models\IncomingReport;
use App\Models\MBClaim;
use App\Models\MBPayer;
use App\Models\Note;
use App\Models\NoteTemplate;
use App\Models\Pro;
use App\Models\Product;
use App\Models\ProProAccess;
use App\Models\SectionTemplate;
use App\Models\Shipment;
use App\Models\SupplyOrder;
use App\Models\Ticket;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;

use Illuminate\Support\Facades\Http;
use PDF;

class DnaController extends Controller
{

    public function patients(Request $request)
    {
        $filters = $request->all();
        $patients = Client::whereNull('shadow_pro_id')->where('default_na_pro_id', $this->performer->pro->id);

        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 . '%');
                });
            }
        }

        $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2');
        $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
        $this->filterMultiQuery($request, $patients, 'usual_bmi_max', 'bmi_category', 'bmi_value_1', 'bmi_value_2');
        $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;
        }
        $patients = $patients->orderBy('created_at', 'DESC')->paginate(20);
        return view('app.dna.patients', compact('patients', 'filters'));
    }

    public function encounters(Request $request)
    {
        $filters = $request->all();
        $notes = Note::query();
        $notes = $notes->where('ally_pro_id', $this->performer->pro->id);
        $this->filterMultiQuery($request, $notes, 'effective_time', 'date_category', 'date_value_1', 'date_value_2');
        $this->filterSimpleQuery($request, $notes, 'new_or_fu_or_na', 'new_or_fu_or_na');
        $notes = $notes->orderBy('created_at', 'DESC')->paginate(20);
        return view('app.dna.encounters', compact('notes', 'filters'));
    }

    public function notes(Request $request)
    {
        $data = [];
        return view('app.dna.notes', $data);
    }

    public function appointments(Request $request)
    {
        $filters = $request->all();
        $appointments = Appointment::where('pro_id', $this->performer->pro->id);
        $this->filterMultiQuery($request, $appointments, 'raw_date', 'date_category', 'date_value_1', 'date_value_2');
        $this->filterSimpleQuery($request, $appointments, 'status', 'status');
        $appointments = $appointments->orderBy('end_time', 'DESC')->paginate(20);
        return view('app.dna.appointments', compact('appointments', 'filters'));
    }

    public function bills(Request $request)
    {
        $data = [];
        return view('app.dna.bills', $data);
    }

    public function erx_and_orders(Request $request)
    {
        $data = [];
        return view('app.dna.erx_and_orders', $data);
    }

    public function reports(Request $request)
    {
        $data = [];
        return view('app.dna.reports', $data);
    }

    public function supply_orders(Request $request)
    {
        $data = [];
        return view('app.dna.supply_orders', $data);
    }

    public function new_patients_awaiting_visit(Request $request){
        $data = [];
        return view('app.dna.new_patients_awaiting_visit', $data);
    }
    public function notes_pending_signature(Request $request){
        $data = [];
        return view('app.dna.notes_pending_signature', $data);
    }
    public function notes_pending_billing(Request $request){
        $data = [];
        return view('app.dna.notes_pending_billing', $data);
    }
    public function reports_pending_signature(Request $request){
        $data = [];
        return view('app.dna.reports_pending_signature', $data);
    }
    public function patients_without_appointments(Request $request){
        $data = [];
        return view('app.dna.patients_without_appointments', $data);
    }
    public function patients_overdue_for_visit(Request $request){
        $data = [];
        return view('app.dna.patients_overdue_for_visit', $data);
    }
    public function cancelled_appointments_pending_review(Request $request){
        $data = [];
        return view('app.dna.cancelled_appointments_pending_review', $data);
    }
    public function cancelled_bills_pending_review(Request $request){
        $data = [];
        return view('app.dna.cancelled_bills_pending_review', $data);
    }
    public function cancelled_supply_orders_pending_review(Request $request){
        $data = [];
        return view('app.dna.cancelled_supply_orders_pending_review', $data);
    }
    public function erx_and_orders_pending_signature(Request $request){
        $data = [];
        return view('app.dna.erx_and_orders_pending_signature', $data);
    }
    public function supply_orders_pending_signature(Request $request){
        $data = [];
        return view('app.dna.supply_orders_pending_signature', $data);
    }

}
