<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Inquiry;
use App\Models\Product;
use Illuminate\Http\Request;

class InquiryController extends Controller
{
    public function index(Request $request)
    {
        $query = Inquiry::with('product');
        
        // Filter by status
        if ($request->has('status') && $request->status) {
            $query->where('status', $request->status);
        }
        
        // Filter by product
        if ($request->has('product_id') && $request->product_id) {
            $query->where('product_id', $request->product_id);
        }
        
        // Filter by date
        if ($request->has('date_from') && $request->date_from) {
            $query->whereDate('created_at', '>=', $request->date_from);
        }
        if ($request->has('date_to') && $request->date_to) {
            $query->whereDate('created_at', '<=', $request->date_to);
        }
        
        // Sorting
        $sortColumn = $request->get('sort', 'created_at');
        $sortDirection = $request->get('direction', 'desc');
        
        // Validate sort column
        $allowedSorts = ['name', 'email', 'created_at', 'status'];
        if (!in_array($sortColumn, $allowedSorts)) {
            $sortColumn = 'created_at';
        }
        
        // Validate sort direction
        if (!in_array($sortDirection, ['asc', 'desc'])) {
            $sortDirection = 'desc';
        }
        
        $query->orderBy($sortColumn, $sortDirection);
        
        $inquiries = $query->paginate(15)->withQueryString();
        $products = Product::all();
        
        // If AJAX request, return JSON
        if ($request->ajax()) {
            $tableHtml = view('admin.inquiries.partials.table', compact('inquiries'))->render();
            $mobileHtml = view('admin.inquiries.partials.mobile-cards', compact('inquiries'))->render();
            $pagination = view('admin.inquiries.partials.pagination', compact('inquiries'))->render();
            
            return response()->json([
                'html' => $tableHtml,
                'mobileHtml' => $mobileHtml,
                'pagination' => $pagination,
                'count' => $inquiries->total()
            ]);
        }
        
        return view('admin.inquiries.index', compact('inquiries', 'products', 'sortColumn', 'sortDirection'));
    }
    
    public function show($id)
    {
        $inquiry = Inquiry::with('product')->findOrFail($id);
        return view('admin.inquiries.show', compact('inquiry'));
    }
    
    public function update(Request $request, $id)
    {
        $inquiry = Inquiry::findOrFail($id);
        
        $validated = $request->validate([
            'status' => 'required|in:pending,resolved',
        ]);
        
        $inquiry->update(['status' => $validated['status']]);
        
        return redirect()->route('admin.inquiries.index')
            ->with('success', 'Inquiry status updated successfully.');
    }
    
    public function destroy($id)
    {
        $inquiry = Inquiry::findOrFail($id);
        $inquiry->delete();
        
        return redirect()->route('admin.inquiries.index')
            ->with('success', 'Inquiry deleted successfully.');
    }
}
