<?php

namespace App\Livewire\Participants;

use App\Domain\Attendance\Enums\AttendanceStatus;
use App\Domain\Attendance\Models\AttendanceRecord;
use App\Domain\Financial\Models\Payment;
use App\Domain\Financial\Models\Wallet;
use App\Domain\Participant\Models\Participant;
use App\Domain\Participant\Services\ParticipantService;
use App\Domain\Shared\Exceptions\DomainException;
use App\Domain\Training\Models\Enrollment;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Layout('layouts.app')]
#[Title('ملف المشترك')]
class ParticipantShow extends Component
{
    public Participant $participant;
    public string $activeTab = 'info';

    // Status change modal
    public bool $showStatusModal = false;
    public string $newStatus = '';
    public string $statusReason = '';

    public function mount(Participant $participant): void
    {
        $this->participant = $participant->load(['person', 'primaryActivity', 'primaryGuardian', 'guardians', 'creator']);
    }

    public function openStatusModal(string $status): void
    {
        $this->newStatus = $status;
        $this->statusReason = '';
        $this->showStatusModal = true;
    }

    public function closeStatusModal(): void
    {
        $this->showStatusModal = false;
        $this->newStatus = '';
        $this->statusReason = '';
    }

    public function changeStatus(ParticipantService $service): void
    {
        $this->validate([
            'statusReason' => 'required|string|min:3',
        ], [
            'statusReason.required' => 'سبب تغيير الحالة مطلوب',
            'statusReason.min' => 'السبب يجب أن يكون 3 أحرف على الأقل',
        ]);

        try {
            $service->changeStatus($this->participant, $this->newStatus, $this->statusReason, auth()->user());
            $this->participant = $this->participant->fresh(['person', 'primaryActivity', 'primaryGuardian', 'guardians', 'creator']);
            $this->showStatusModal = false;
            $this->newStatus = '';
            $this->statusReason = '';
            session()->flash('success', __('تم تغيير حالة المشترك بنجاح'));
        } catch (DomainException $e) {
            session()->flash('error', $e->getMessage());
        }
    }

    public function render()
    {
        $currentStatus = $this->participant->status->value ?? $this->participant->status;

        $validTransitions = match ($currentStatus) {
            'registered' => ['active', 'withdrawn'],
            'active' => ['frozen', 'suspended', 'inactive', 'graduated', 'withdrawn'],
            'frozen' => ['active', 'withdrawn', 'inactive'],
            'suspended' => ['active', 'withdrawn'],
            'inactive' => ['active', 'withdrawn'],
            'graduated' => ['active'],
            'withdrawn' => ['active'],
            default => [],
        };

        $statusLabels = [
            'registered' => 'مسجل',
            'active' => 'نشط',
            'frozen' => 'مجمد',
            'suspended' => 'موقوف',
            'inactive' => 'غير نشط',
            'graduated' => 'متخرج',
            'transferred' => 'محول',
            'withdrawn' => 'منسحب',
            'blacklisted' => 'محظور',
        ];

        // Attendance summary
        $participantClass = \App\Domain\Participant\Models\Participant::class;
        $attendanceQuery = AttendanceRecord::where('subject_type', $participantClass)
            ->where('subject_id', $this->participant->id);

        $totalSessions = (clone $attendanceQuery)->count();
        $presentCount = (clone $attendanceQuery)->where('status', AttendanceStatus::Present)->count();
        $lateCount = (clone $attendanceQuery)->where('status', AttendanceStatus::Late)->count();
        $absentCount = (clone $attendanceQuery)->whereIn('status', [AttendanceStatus::Absent, AttendanceStatus::NoShow])->count();
        $partialCount = (clone $attendanceQuery)->where('status', AttendanceStatus::Partial)->count();
        $cancelledExempt = (clone $attendanceQuery)->whereIn('status', [AttendanceStatus::Cancelled, AttendanceStatus::Exempt])->count();

        $attendanceDenominator = $totalSessions - $cancelledExempt;
        $attendanceRate = $attendanceDenominator > 0
            ? round(($presentCount + $lateCount + $partialCount) / $attendanceDenominator * 100, 1)
            : 0;

        // Payment history (last 10)
        $recentPayments = Payment::where('payer_type', $participantClass)
            ->where('payer_id', $this->participant->id)
            ->orderByDesc('payment_date')
            ->limit(10)
            ->get();

        // Active enrollments with group and program details
        $activeEnrollments = Enrollment::where('participant_id', $this->participant->id)
            ->where('status', 'active')
            ->with(['group.headTrainer', 'group.schedules', 'program'])
            ->get();

        // Wallet balance
        $wallet = Wallet::where('owner_type', $participantClass)
            ->where('owner_id', $this->participant->id)
            ->first();

        return view('livewire.participants.participant-show', [
            'validTransitions' => $validTransitions,
            'statusLabels' => $statusLabels,
            'totalSessions' => $totalSessions,
            'presentCount' => $presentCount,
            'lateCount' => $lateCount,
            'absentCount' => $absentCount,
            'partialCount' => $partialCount,
            'attendanceRate' => $attendanceRate,
            'recentPayments' => $recentPayments,
            'activeEnrollments' => $activeEnrollments,
            'wallet' => $wallet,
        ]);
    }
}
