<?php

namespace App\Livewire\Trainer;

use App\Domain\Scheduling\Enums\AssignmentStatus;
use App\Domain\Scheduling\Models\Assignment;
use App\Domain\Training\Enums\SessionStatus;
use App\Domain\Training\Models\TrainingGroup;
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 TrainerDashboard extends Component
{
    use UsesBranchScope;

    public ?int $branchId = null;

    public function mount(): void
    {
        $this->authorize('attendance.mark');
        $this->branchId = $this->getActiveBranchIdOrFail();
    }

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

        // Get group IDs assigned to this trainer (active assignments)
        $assignedGroupIds = Assignment::active()
            ->forUser($user->id)
            ->where('assignable_type', TrainingGroup::class)
            ->pluck('assignable_id');

        // Today's sessions for assigned groups
        $todaySessions = TrainingSession::whereIn('training_group_id', $assignedGroupIds)
            ->where('session_date', $today)
            ->whereIn('status', [SessionStatus::Scheduled, SessionStatus::InProgress, SessionStatus::Completed])
            ->with('group')
            ->orderBy('start_time')
            ->get();

        // Upcoming sessions (next 7 days, excluding today)
        $upcomingSessions = TrainingSession::whereIn('training_group_id', $assignedGroupIds)
            ->where('session_date', '>', $today)
            ->where('session_date', '<=', now()->addDays(7)->toDateString())
            ->where('status', SessionStatus::Scheduled)
            ->with('group')
            ->orderBy('session_date')
            ->orderBy('start_time')
            ->get();

        // Assigned groups with participant count
        $assignedGroups = TrainingGroup::whereIn('id', $assignedGroupIds)
            ->whereIn('status', ['active', 'forming', 'full'])
            ->orderBy('name_ar')
            ->get();

        // Monthly stats: sessions conducted this month
        $monthStart = now()->startOfMonth()->toDateString();
        $monthEnd = now()->endOfMonth()->toDateString();

        $sessionsThisMonth = TrainingSession::whereIn('training_group_id', $assignedGroupIds)
            ->whereBetween('session_date', [$monthStart, $monthEnd])
            ->where('status', SessionStatus::Completed)
            ->count();

        $totalSessionsThisMonth = TrainingSession::whereIn('training_group_id', $assignedGroupIds)
            ->whereBetween('session_date', [$monthStart, $monthEnd])
            ->whereIn('status', [SessionStatus::Scheduled, SessionStatus::InProgress, SessionStatus::Completed])
            ->count();

        return view('livewire.trainer.trainer-dashboard', [
            'todaySessions' => $todaySessions,
            'upcomingSessions' => $upcomingSessions,
            'assignedGroups' => $assignedGroups,
            'sessionsThisMonth' => $sessionsThisMonth,
            'totalSessionsThisMonth' => $totalSessionsThisMonth,
        ]);
    }
}
