<?php

namespace App\Http\Controllers\Secretary;

use App\Http\Controllers\Controller;
use App\Models\Queue;
use App\Models\Appointment;

class DashboardController extends Controller
{
    public function index()
    {
        $doctorId = session('secretary_doctor_id');

        $stats = [
            'waiting'      => Queue::where('doctor_id', $doctorId)->today()->where('status', 'waiting')->count(),
            'in_progress'  => Queue::where('doctor_id', $doctorId)->today()->where('status', 'in_progress')->count(),
            'completed'    => Queue::where('doctor_id', $doctorId)->today()->where('status', 'completed')->count(),
            'total_today'  => Queue::where('doctor_id', $doctorId)->today()->count(),
            'appointments' => Appointment::where('doctor_id', $doctorId)
                                ->whereDate('appointment_date', today())
                                ->whereIn('status', ['confirmed', 'pending'])->count(),
        ];

        $queue = Queue::with(['patient.user'])
            ->where('doctor_id', $doctorId)
            ->today()
            ->whereIn('status', ['waiting', 'called', 'in_progress'])
            ->orderBy('position')
            ->get();

        $todayAppointments = Appointment::with(['patient.user'])
            ->where('doctor_id', $doctorId)
            ->whereDate('appointment_date', today())
            ->whereIn('status', ['confirmed', 'pending'])
            ->orderBy('appointment_time')
            ->get();

        return view('secretary.dashboard.index', compact('stats', 'queue', 'todayAppointments'));
    }
}
