<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\InquiryService;
use Illuminate\Http\Request;

class InquiryController extends Controller
{
    protected $inquiryService;

    public function __construct(InquiryService $inquiryService)
    {
        $this->inquiryService = $inquiryService;
    }

    public function index(Request $request)
    {
        $inquiries = $this->inquiryService->getAllInquiries($request->search);
        
        if ($request->has('search')) {
            $inquiries->appends(['search' => $request->search]);
        }

        return view('admin.inquiries.index', compact('inquiries'));
    }

    public function show($id)
    {
        $inquiry = \App\Models\Inquiry::with(['car', 'customer'])->findOrFail($id);

        return view('admin.inquiries.show', compact('inquiry'));
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'status' => 'required|in:new,contacted,closed',
            'notes' => 'nullable|string',
        ]);

        $this->inquiryService->updateInquiryStatus($id, $request->status, $request->notes);

        return redirect()->back()->with('success', 'Inquiry updated successfully.');
    }
}
