<?php

declare(strict_types=1);

namespace App\Livewire\Admin\ReviewedWebsites;

use App\Models\ReviewedWebsiteReview;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Layout;
use Livewire\Component;
use Livewire\WithPagination;

#[Layout('layouts.admin', ['title' => 'Website review moderation'])]
final class ReviewsModerate extends Component
{
    use WithPagination;

    public string $statusFilter = ReviewedWebsiteReview::STATUS_PENDING;

    #[Computed]
    public function reviews()
    {
        return ReviewedWebsiteReview::query()
            ->with(['reviewedWebsite:id,name,slug', 'user:id,name'])
            ->when($this->statusFilter !== 'all', fn ($q) => $q->where('status', $this->statusFilter))
            ->latest()
            ->paginate(20);
    }

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

    public function approve(int $id): void
    {
        $review = ReviewedWebsiteReview::query()->findOrFail($id);
        $review->update(['status' => ReviewedWebsiteReview::STATUS_APPROVED]);
        session()->flash('success', 'Review approved.');
    }

    public function reject(int $id): void
    {
        $review = ReviewedWebsiteReview::query()->findOrFail($id);
        $review->update(['status' => ReviewedWebsiteReview::STATUS_REJECTED]);
        session()->flash('success', 'Review rejected.');
    }

    public function render()
    {
        return view('livewire.admin.reviewed-websites.reviews-moderate', [
            'verdictLabels' => ReviewedWebsiteReview::userVerdictLabels(),
        ]);
    }
}
