<?php

namespace Modules\VetPortal\Http\Controllers\Portal;

use Illuminate\Routing\Controller;
use Modules\VetClinic\Entities\VcAppointment;

class AppointmentController extends Controller
{
    public function index()
    {
        $account = auth()->user();

        $base = VcAppointment::where('business_id', $account->business_id)
            ->where('contact_id', $account->contact_id)
            ->with(['pet', 'doctor', 'department']);

        $upcoming = (clone $base)
            ->whereDate('appointment_date', '>=', today())
            ->whereNotIn('status', ['cancelled', 'no_show', 'completed'])
            ->orderBy('appointment_date')->orderBy('appointment_time')
            ->get();

        $past = (clone $base)
            ->where(function ($q) {
                $q->whereDate('appointment_date', '<', today())
                  ->orWhereIn('status', ['completed', 'cancelled', 'no_show']);
            })
            ->orderByDesc('appointment_date')->orderByDesc('appointment_time')
            ->limit(60)->get();

        return view('vetPortal::portal.appointments', compact('upcoming', 'past'));
    }
}
