<?php

namespace Modules\VetPortal\Http\Controllers\Portal;

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

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

        $pets = VcPet::where('business_id', $account->business_id)
            ->where('contact_id', $account->contact_id)
            ->withCount('appointments')
            ->get();

        return view('vetPortal::portal.pets', compact('pets'));
    }

    public function show($id)
    {
        $account = auth()->user();

        // Scoped findOrFail — a pet owner can only ever open their own pet.
        $pet = VcPet::where('business_id', $account->business_id)
            ->where('contact_id', $account->contact_id)
            ->with([
                'vaccinations.vaccineType',
                'prescriptions.doctor',
                'conditions.condition',
                'appointments' => fn($q) => $q->with('doctor')->orderByDesc('appointment_date'),
            ])
            ->findOrFail($id);

        return view('vetPortal::portal.pet', compact('pet'));
    }
}
