<?php

namespace App\Livewire\Attendance;

use App\Domain\Attendance\Enums\AttendanceStatus;
use App\Domain\Attendance\Models\AttendanceRecord;
use App\Domain\Attendance\Services\AttendanceMarkingService;
use App\Domain\Training\Models\TrainingSession;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Layout('layouts.app')]
#[Title('تسجيل الحضور')]
class TakeAttendance extends Component
{
    public TrainingSession $session;

    public string $sessionNotes = '';

    /** @var array<int, string> Per-record notes keyed by record id */
    public array $recordNotes = [];

    public function mount(TrainingSession $session): void
    {
        $this->authorize('attendance.mark');
        $this->session = $session->load(['group']);
        $this->sessionNotes = $this->session->notes ?? '';
    }

    public function saveNotes(): void
    {
        $this->session->update(['notes' => $this->sessionNotes]);
        session()->flash('notes_saved', __('تم حفظ ملاحظات الجلسة'));
    }

    public function saveRecordNote(int $recordId): void
    {
        $record = AttendanceRecord::findOrFail($recordId);
        $note = $this->recordNotes[$recordId] ?? '';
        $record->update(['notes' => $note]);
        session()->flash("record_note_saved_{$recordId}", __('تم حفظ الملاحظة'));
    }

    public function markAs(int $recordId, string $status, AttendanceMarkingService $service): void
    {
        $record = AttendanceRecord::findOrFail($recordId);
        $statusEnum = AttendanceStatus::from($status);
        $service->markStatus($record, $statusEnum, auth()->user());
    }

    public function markPresent(int $recordId, AttendanceMarkingService $service): void
    {
        $record = AttendanceRecord::findOrFail($recordId);
        $service->markPresent($record, auth()->user());
    }

    public function markAllPresent(AttendanceMarkingService $service): void
    {
        $records = AttendanceRecord::where('training_session_id', $this->session->id)
            ->where('status', AttendanceStatus::Expected)
            ->get();

        foreach ($records as $record) {
            $service->markPresent($record, auth()->user());
        }
    }

    public function render()
    {
        $records = AttendanceRecord::where('training_session_id', $this->session->id)
            ->with('subject')
            ->orderBy('subject_type')
            ->orderBy('status')
            ->get();

        // Populate per-record notes from DB (only on first load or if not yet set)
        foreach ($records as $record) {
            if (!array_key_exists($record->id, $this->recordNotes)) {
                $this->recordNotes[$record->id] = $record->notes ?? '';
            }
        }

        $summary = [
            'total' => $records->count(),
            'present' => $records->where('status', AttendanceStatus::Present)->count(),
            'late' => $records->where('status', AttendanceStatus::Late)->count(),
            'absent' => $records->where('status', AttendanceStatus::Absent)->count(),
            'expected' => $records->where('status', AttendanceStatus::Expected)->count(),
            'excused' => $records->where('status', AttendanceStatus::Excused)->count(),
        ];

        return view('livewire.attendance.take-attendance', [
            'records' => $records,
            'summary' => $summary,
            'statuses' => AttendanceStatus::cases(),
        ]);
    }
}
