<?php

use App\Enums\ConsultationStatus;
use App\Models\Consultation;
use App\Models\User;
use Livewire\Volt\Component;
use Livewire\WithPagination;

new class extends Component
{
    use WithPagination;

    public User $user;

    public function mount(User $user): void
    {
        $this->user = $user;
    }

    public function with(): array
    {
        return [
            'consultations' => Consultation::query()
                ->where('user_id', $this->user->id)
                ->orderBy('booking_date', 'desc')
                ->orderBy('booking_time', 'desc')
                ->paginate(15),
            'statistics' => [
                'total' => Consultation::where('user_id', $this->user->id)->count(),
                'completed' => Consultation::where('user_id', $this->user->id)
                    ->where('status', ConsultationStatus::Completed)->count(),
                'cancelled' => Consultation::where('user_id', $this->user->id)
                    ->where('status', ConsultationStatus::Cancelled)->count(),
                'no_show' => Consultation::where('user_id', $this->user->id)
                    ->where('status', ConsultationStatus::NoShow)->count(),
            ],
        ];
    }
}; ?>

<div class="max-w-5xl mx-auto">
    <div class="mb-6">
        @if($user->user_type->value === 'individual')
            <flux:button href="{{ route('admin.clients.individual.show', $user) }}" variant="ghost" icon="arrow-left" wire:navigate>
                {{ __('common.back') }}
            </flux:button>
        @else
            <flux:button href="{{ route('admin.clients.company.show', $user) }}" variant="ghost" icon="arrow-left" wire:navigate>
                {{ __('common.back') }}
            </flux:button>
        @endif
    </div>

    <div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4 mb-6">
        <div>
            <flux:heading size="xl">{{ __('admin.client_consultations') }}</flux:heading>
            <p class="text-sm text-zinc-500 dark:text-zinc-400 mt-1">{{ $user->full_name }}</p>
        </div>
    </div>

    <!-- Statistics -->
    <div class="grid grid-cols-2 sm:grid-cols-4 gap-4 mb-6">
        <div class="bg-white dark:bg-zinc-800 rounded-lg p-4 border border-zinc-200 dark:border-zinc-700 text-center">
            <p class="text-2xl font-bold text-zinc-900 dark:text-zinc-100">{{ $statistics['total'] }}</p>
            <p class="text-sm text-zinc-500 dark:text-zinc-400">{{ __('admin.total_consultations') }}</p>
        </div>
        <div class="bg-white dark:bg-zinc-800 rounded-lg p-4 border border-zinc-200 dark:border-zinc-700 text-center">
            <p class="text-2xl font-bold text-green-600 dark:text-green-400">{{ $statistics['completed'] }}</p>
            <p class="text-sm text-zinc-500 dark:text-zinc-400">{{ __('admin.completed_consultations') }}</p>
        </div>
        <div class="bg-white dark:bg-zinc-800 rounded-lg p-4 border border-zinc-200 dark:border-zinc-700 text-center">
            <p class="text-2xl font-bold text-red-600 dark:text-red-400">{{ $statistics['cancelled'] }}</p>
            <p class="text-sm text-zinc-500 dark:text-zinc-400">{{ __('admin.cancelled_consultations') }}</p>
        </div>
        <div class="bg-white dark:bg-zinc-800 rounded-lg p-4 border border-zinc-200 dark:border-zinc-700 text-center">
            <p class="text-2xl font-bold text-amber-600 dark:text-amber-400">{{ $statistics['no_show'] }}</p>
            <p class="text-sm text-zinc-500 dark:text-zinc-400">{{ __('admin.no_show_consultations') }}</p>
        </div>
    </div>

    <!-- Consultations List -->
    <div class="space-y-4">
        @forelse($consultations as $consultation)
            <div wire:key="consultation-{{ $consultation->id }}" class="bg-white dark:bg-zinc-800 rounded-lg p-4 border border-zinc-200 dark:border-zinc-700">
                <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
                    <div class="flex-1">
                        <div class="flex items-center gap-3 mb-2">
                            <span class="font-medium text-zinc-900 dark:text-zinc-100">
                                {{ $consultation->booking_date->translatedFormat('l, d M Y') }}
                            </span>
                            <span class="text-zinc-500 dark:text-zinc-400">
                                {{ \Carbon\Carbon::parse($consultation->booking_time)->format('g:i A') }}
                            </span>
                        </div>

                        <div class="flex flex-wrap items-center gap-2">
                            @php
                                $statusVariant = match($consultation->status) {
                                    \App\Enums\ConsultationStatus::Pending => 'warning',
                                    \App\Enums\ConsultationStatus::Approved => 'primary',
                                    \App\Enums\ConsultationStatus::Completed => 'success',
                                    \App\Enums\ConsultationStatus::Cancelled => 'danger',
                                    \App\Enums\ConsultationStatus::NoShow => 'danger',
                                    \App\Enums\ConsultationStatus::Rejected => 'danger',
                                };
                            @endphp
                            <flux:badge variant="{{ $statusVariant }}" size="sm">
                                {{ $consultation->status->label() }}
                            </flux:badge>

                            <flux:badge variant="{{ $consultation->consultation_type === \App\Enums\ConsultationType::Paid ? 'primary' : 'outline' }}" size="sm">
                                {{ $consultation->consultation_type->label() }}
                            </flux:badge>

                            @if($consultation->consultation_type === \App\Enums\ConsultationType::Paid)
                                @php
                                    $paymentVariant = match($consultation->payment_status) {
                                        \App\Enums\PaymentStatus::Pending => 'warning',
                                        \App\Enums\PaymentStatus::Received => 'success',
                                        default => 'outline',
                                    };
                                @endphp
                                <flux:badge variant="{{ $paymentVariant }}" size="sm">
                                    {{ $consultation->payment_status->label() }}
                                </flux:badge>
                            @endif
                        </div>

                        @if($consultation->problem_summary)
                            <p class="mt-2 text-sm text-zinc-600 dark:text-zinc-400 line-clamp-2">
                                {{ Str::limit($consultation->problem_summary, 150) }}
                            </p>
                        @endif
                    </div>

                    <flux:button
                        href="{{ route('admin.consultations.show', $consultation) }}"
                        variant="ghost"
                        size="sm"
                        wire:navigate
                    >
                        {{ __('common.edit') }}
                    </flux:button>
                </div>
            </div>
        @empty
            <div class="text-center py-12 text-zinc-500 dark:text-zinc-400 bg-white dark:bg-zinc-800 rounded-lg border border-zinc-200 dark:border-zinc-700">
                <flux:icon name="inbox" class="w-12 h-12 mx-auto mb-4" />
                <p>{{ __('admin.no_consultations') }}</p>
            </div>
        @endforelse
    </div>

    <div class="mt-6">
        {{ $consultations->links() }}
    </div>
</div>
