<?php

namespace App\Http\Controllers;

use App\Models\CareMonth;
use App\Models\Client;
use App\Models\ClientInfoLine;
use App\Models\Facility;
use App\Models\NoteTemplate;
use App\Models\Pro;
use App\Models\SectionTemplate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class PatientController extends Controller
{
    public function dashboard(Request $request, Client $patient )
    {
        $mcpPros = Pro::where('is_enrolled_as_mcp', true)->get();
        $facilities = Facility::where('is_active', true)->get();
        return view('app.patient.dashboard', compact('patient', 'facilities'));
    }

    public function actionItems(Request $request, Client $patient )
    {
        $facilities = Facility::where('is_active', true)->get();
        return view('app.patient.action-items', compact('patient', 'facilities'));
    }

    public function intake(Request $request, Client $patient )
    {
        $files = File::allFiles(resource_path('views/app/intake-templates'));
        $templates = [];
        foreach ($files as $file) {
            $templates[] = str_replace(".blade.php", "", $file->getFilename());
        }
        return view('app.patient.intake', compact('patient', 'templates'));
    }

    public function carePlan(Request $request, Client $patient )
    {
        return view('app.patient.care-plan', compact('patient'));
    }

    public function medications(Request $request, Client $patient )
    {
        $infoLines = ClientInfoLine::where('client_id', $patient->id)
            ->where('category', 'rx')
            ->where('is_removed', false)
            ->orderBy('content_text', 'asc')
            ->get();
        return view('app.patient.medications', compact('patient', 'infoLines'));
    }

    public function dxAndFocusAreas(Request $request, Client $patient )
    {
        $infoLines = ClientInfoLine::where('client_id', $patient->id)
            ->where('category', 'dx')
            ->where('is_removed', false)
            ->get();
        return view('app.patient.dx-and-focus-areas', compact('patient', 'infoLines'));
    }

    public function careTeam(Request $request, Client $patient )
    {
        $infoLines = ClientInfoLine::where('client_id', $patient->id)
            ->where('category', 'care_team')
            ->where('is_removed', false)
            ->get();
        return view('app.patient.care-team', compact('patient', 'infoLines'));
    }

    public function measurements(Request $request, Client $patient )
    {
        return view('app.patient.measurements', compact('patient'));
    }

    public function labsAndStudies(Request $request, Client $patient )
    {
        return view('app.patient.labs-and-studies', compact('patient'));
    }

    public function history(Request $request, Client $patient )
    {
        $infoLines = ClientInfoLine::where('client_id', $patient->id)
            ->where('category', 'LIKE', 'history_%')
            ->where('is_removed', false)
            ->get();
        return view('app.patient.history', compact('patient', 'infoLines'));
    }

    public function sms(Request $request, Client $patient )
    {
        return view('app.patient.sms', compact('patient'));
    }

    public function smsNumbers(Request $request, Client $patient )
    {
        return view('app.patient.sms-numbers', compact('patient'));
    }

    public function immunizations(Request $request, Client $patient )
    {
        return view('app.patient.immunizations', compact('patient'));
    }

    public function allergies(Request $request, Client $patient )
    {
        $infoLines = ClientInfoLine::where('client_id', $patient->id)
            ->where('category', 'allergy')
            ->where('is_removed', false)
            ->get();
        return view('app.patient.allergies', compact('patient', 'infoLines'));
    }

    public function notes(Request $request, Client $patient )
    {
        $pros = Pro::all();
        $sectionTemplates = SectionTemplate::all();
        return view('app.patient.notes', compact('patient','pros', 'sectionTemplates'));
    }

    public function flowSheets(Request $request, Client $patient )
    {
        return view('app.patient.flowsheets', compact('patient'));
    }

    public function demographics(Request $request, Client $patient )
    {
        return view('app.patient.demographics', compact('patient'));
    }

    public function account(Request $request, Client $patient )
    {
        return view('app.patient.account', compact('patient'));
    }

    public function careChecklist(Request $request, Client $patient )
    {
        return view('app.patient.care-checklist', compact('patient'));
    }

    public function documents(Request $request, Client $patient )
    {
        return view('app.patient.documents', compact('patient'));
    }

    public function education(Request $request, Client $patient )
    {
        return view('app.patient.education', compact('patient'));
    }

    public function messaging(Request $request, Client $patient )
    {
        return view('app.patient.messaging', compact('patient'));
    }

    public function duplicate(Request $request, Client $patient )
    {
        return view('app.patient.duplicate', compact('patient'));
    }

    public function careMonths(Request $request, Client $patient )
    {
        $careMonths = CareMonth::where('client_id', $patient->id)->orderBy('start_date', 'desc')->get();
        return view('app.patient.care-months', compact('patient', 'careMonths'));
    }

    public function presence(Request $request, Client $patient )
    {
        return json_encode([
            "online" => $patient->is_online
        ]);
    }

    public function embedSection(Request $request, Client $patient, $section, $selectable) {
        return view('app.patient.partials.' . $section, compact('patient', 'selectable'));
    }
}
