<?php

use App\Enums\ContactStatus;
use App\Models\ContactSubmission;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Title;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;

return new #[Title('Contact Submissions')] class extends Component {
    use WithPagination;

    #[Url(as: 'status')]
    public string $status = 'all';

    #[Url(as: 'q')]
    public string $search = '';

    #[Url(as: 'per_page')]
    public int $perPage = 50;

    /**
     * Reset pagination whenever the status filter changes.
     */
    public function updatingStatus(): void
    {
        $this->resetPage();
    }

    /**
     * Reset pagination whenever the search term changes.
     */
    public function updatingSearch(): void
    {
        $this->resetPage();
    }

    /**
     * Reset pagination whenever the per-page selection changes.
     */
    public function updatingPerPage(): void
    {
        $this->resetPage();
    }

    /**
     * @return LengthAwarePaginator<int, ContactSubmission>
     */
    #[Computed]
    public function submissions(): LengthAwarePaginator
    {
        return ContactSubmission::query()
            ->status($this->normalizedStatus())
            ->when($this->search !== '', function ($query) {
                $query->where(function ($query) {
                    $query->where('name', 'like', "%{$this->search}%")
                        ->orWhere('email', 'like', "%{$this->search}%")
                        ->orWhere('phone', 'like', "%{$this->search}%");
                });
            })
            ->latestFirst()
            ->paginate($this->normalizedPerPage());
    }

    /**
     * Stats are computed over the whole table, unaffected by the current
     * filter/search, matching the legacy admin panel's stats row.
     *
     * @return array<string, int>
     */
    #[Computed]
    public function stats(): array
    {
        return [
            'total' => ContactSubmission::query()->count(),
            'new' => ContactSubmission::query()->where('status', ContactStatus::New)->count(),
            'contacted' => ContactSubmission::query()->where('status', ContactStatus::Contacted)->count(),
            'converted' => ContactSubmission::query()->where('status', ContactStatus::Converted)->count(),
            'today' => ContactSubmission::query()->where('created_at', '>=', now()->startOfDay())->count(),
        ];
    }

    /**
     * Any unrecognised status value silently falls back to "all", matching
     * the legacy admin panel's behaviour.
     */
    private function normalizedStatus(): string
    {
        return $this->status === 'all' || ContactStatus::tryFrom($this->status) !== null
            ? $this->status
            : 'all';
    }

    /**
     * Any unrecognised per-page value falls back to the default of 50.
     */
    private function normalizedPerPage(): int
    {
        return in_array($this->perPage, [25, 50, 100], true) ? $this->perPage : 50;
    }

    protected function view($data = [])
    {
        return app('view')->file('/Users/dineshmali/code/pushpvatikaflorist1/storage/framework/views/livewire/views/711a7ed2.blade.php', $data);
    }
}; 