<?php

namespace App\Livewire\Participants;

use App\Domain\Participant\Models\Participant;
use App\Domain\Training\Models\Enrollment;
use Livewire\Component;

class EnrollmentHistory extends Component
{
    public Participant $participant;
    public bool $showAll = false;

    public function mount(Participant $participant): void
    {
        $this->participant = $participant;
    }

    public function toggleShowAll(): void
    {
        $this->showAll = !$this->showAll;
    }

    public function render()
    {
        $query = Enrollment::where('participant_id', $this->participant->id)
            ->with(['trainingGroup.program'])
            ->orderByDesc('created_at');

        $enrollments = $this->showAll ? $query->get() : $query->limit(5)->get();
        $totalCount = Enrollment::where('participant_id', $this->participant->id)->count();

        return view('livewire.participants.enrollment-history', [
            'enrollments' => $enrollments,
            'totalCount' => $totalCount,
        ]);
    }
}
