<?php

namespace App\Http\Controllers;

use App\Models\FeedbackReport;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class AdminFeedbackController extends Controller
{
    public function index(Request $request): View
    {
        $query = FeedbackReport::query()->orderByDesc('created_at');

        if ($status = $request->get('status')) {
            $query->where('status', $status);
        }

        $reports = $query->paginate(15)->withQueryString();
        $unreadCount = FeedbackReport::where('status', 'baru')->count();

        return view('admin.feedback.index', compact('reports', 'unreadCount'));
    }

    public function markRead(string $id): RedirectResponse
    {
        FeedbackReport::findOrFail($id)->update(['status' => 'dibaca']);
        return back()->with('status', 'Laporan telah ditandakan sebagai dibaca.');
    }

    public function markResolved(string $id): RedirectResponse
    {
        FeedbackReport::findOrFail($id)->update(['status' => 'selesai']);
        return back()->with('status', 'Laporan telah ditandakan sebagai selesai.');
    }
}
