<?php

namespace App\Http\Controllers\Public;

use App\Http\Controllers\Controller;
use App\Models\Vehicle;
use App\Models\SeoMeta;
use App\Services\SEO\SchemaGenerator;
use Illuminate\Http\Request;

class InventoryController extends Controller
{
    public function index(Request $request)
    {
        $query = Vehicle::available();

        if ($request->filled('make')) {
            $query->where('make', $request->make);
        }
        if ($request->filled('model')) {
            $query->where('model', 'like', '%' . $request->model . '%');
        }
        if ($request->filled('year')) {
            $query->where('year', $request->year);
        }
        if ($request->filled('body_type')) {
            $query->where('body_type', $request->body_type);
        }
        if ($request->filled('fuel_type')) {
            $query->where('fuel_type', 'like', '%' . $request->fuel_type . '%');
        }
        if ($request->filled('transmission')) {
            $query->where('transmission', 'like', '%' . $request->transmission . '%');
        }
        if ($request->filled('grade')) {
            $query->where('auction_grade', $request->grade);
        }
        if ($request->filled('price_min')) {
            $query->where('final_price_bdt', '>=', (float) $request->price_min);
        }
        if ($request->filled('price_max')) {
            $query->where('final_price_bdt', '<=', (float) $request->price_max);
        }
        if ($request->filled('mileage_max')) {
            $query->where('mileage', '<=', (int) $request->mileage_max);
        }

        // Sorting
        $sortBy = $request->get('sort', 'newest');
        match ($sortBy) {
            'price_asc' => $query->orderBy('final_price_bdt', 'asc'),
            'price_desc' => $query->orderBy('final_price_bdt', 'desc'),
            'mileage_asc' => $query->orderBy('mileage', 'asc'),
            'year_desc' => $query->orderBy('year', 'desc'),
            default => $query->orderBy('id', 'desc'),
        };

        $vehicles = $query->paginate(12)->withQueryString();

        // Facet data
        $makes = Vehicle::distinct()->pluck('make')->filter();
        $bodyTypes = Vehicle::distinct()->pluck('body_type')->filter();
        $fuelTypes = Vehicle::distinct()->pluck('fuel_type')->filter();
        $grades = Vehicle::distinct()->orderBy('auction_grade', 'desc')->pluck('auction_grade')->filter();
        $years = Vehicle::distinct()->orderBy('year', 'desc')->pluck('year')->filter();

        $seo = SeoMeta::where('url_path', '/inventory')->first();

        return view('public.inventory', compact(
            'vehicles',
            'makes',
            'bodyTypes',
            'fuelTypes',
            'grades',
            'years',
            'seo'
        ));
    }

    public function show(string $slug)
    {
        $vehicle = Vehicle::where('slug', $slug)
            ->orWhere('id', $slug)
            ->firstOrFail();

        $vehicle->increment('views_count');

        $similarVehicles = Vehicle::where('id', '!=', $vehicle->id)
            ->where(function ($q) use ($vehicle) {
                $q->where('make', $vehicle->make)
                  ->orWhere('body_type', $vehicle->body_type);
            })
            ->take(4)
            ->get();

        $schemaJson = SchemaGenerator::vehicle($vehicle);

        return view('public.inventory-show', compact('vehicle', 'similarVehicles', 'schemaJson'));
    }
}
