<?php

namespace App\Livewire\Pages\Bio;

use App\Domains\Bio\Models\BioPage;
use Illuminate\View\View;
use Livewire\Attributes\On;
use Livewire\Component;
use Livewire\WithPagination;

class Index extends Component
{
    use WithPagination;

    public string $search = '';

    public function updatingSearch(): void
    {
        $this->resetPage();
    }

    #[On('bio-created')]
    #[On('bio-updated')]
    #[On('bio-deleted')]
    public function refresh(): void
    {
        $this->resetPage();
    }

    public function render(): View
    {
        $workspace = require_workspace();

        $bioPages = BioPage::where('workspace_id', $workspace->id)
            ->when($this->search, fn ($q) => $q->where('slug', 'like', "%{$this->search}%"))
            ->latest()
            ->paginate(20);

        return view('livewire.pages.bio.index', compact('bioPages'))
            ->layout('layouts.nimuli-app', ['title' => 'Link-in-Bio']);
    }
}
