<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ContactQuery;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class AdminQueryController extends Controller
{
    public function index(): View
    {
        $queries = ContactQuery::latest()->get();

        return view('admin.pages.queries', compact('queries'));
    }

    public function show(ContactQuery $query): View
    {
        if ($query->status === 'Unread') {
            $query->update(['status' => 'Read']);
        }

        return view('admin.pages.query-show', compact('query'));
    }

    public function toggleStatus(ContactQuery $query): RedirectResponse
    {
        $query->update([
            'status' => $query->status === 'Unread' ? 'Read' : 'Unread',
        ]);

        return back()->with('status', 'Query status updated successfully.');
    }
}
