<?php

namespace App\Livewire\Receptionist;

use App\Domain\Attendance\Enums\AttendanceStatus;
use App\Domain\Attendance\Models\AttendanceRecord;
use App\Domain\Financial\Models\Payment;
use App\Domain\Participant\Models\Participant;
use App\Domain\Training\Models\TrainingSession;
use App\Domain\Shared\Traits\UsesBranchScope;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Layout('layouts.app')]
#[Title('مكتب الاستقبال')]
class ReceptionistDashboard extends Component
{
    use UsesBranchScope;

    public function mount(): void
    {
        $this->authorize('participants.list');

        // A reception desk stands in one branch: refuse the screen in
        // all-branches mode rather than showing a mixed day. The id is not kept
        // on the component — every figure below was filtered by it and nothing
        // else, so a public property holding it was a one-line way to read
        // another branch's day.
        $this->getActiveBranchIdOrFail();
    }

    public function render()
    {
        $today = now()->toDateString();

        // Every model below carries BranchScope, so these counts are already
        // the active branch's and only the active branch's. The explicit
        // `where('branch_id', ...)` these lines used to carry is gone rather
        // than duplicated: the branch now comes from the request context, not
        // from a component property the browser could rewrite.
        $stats = [
            'registrations_today' => Participant::whereDate('created_at', $today)->count(),
            // direction: a refund is an outbound payment row. Without this
            // filter money handed back to a parent is counted as money taken in.
            'payments_today' => Payment::where('status', 'confirmed')
                ->where('direction', 'inbound')
                ->whereDate('created_at', $today)->sum('amount'),
            'payments_count' => Payment::where('status', 'confirmed')
                ->where('direction', 'inbound')
                ->whereDate('created_at', $today)->count(),
            // whereHas('group') is kept for what it always meant — a session
            // attached to a group — not for the branch, which both models now
            // enforce themselves.
            'sessions_in_progress' => TrainingSession::whereHas('group')
                ->where('session_date', $today)
                ->where('status', 'in_progress')->count(),
            'sessions_today' => TrainingSession::whereHas('group')
                ->where('session_date', $today)->count(),
        ];

        // Attendance summary for today
        $todaySessions = TrainingSession::whereHas('group')
            ->where('session_date', $today)->pluck('id');

        $stats['checked_in'] = AttendanceRecord::whereIn('training_session_id', $todaySessions)
            ->whereIn('status', [AttendanceStatus::Present, AttendanceStatus::Late])
            ->count();
        $stats['expected_today'] = AttendanceRecord::whereIn('training_session_id', $todaySessions)
            ->where('status', AttendanceStatus::Expected)
            ->count();

        return view('livewire.receptionist.dashboard', [
            'stats' => $stats,
        ]);
    }
}
