<?php

namespace App\Http\Controllers\Api;

use App\Models\Tenant\Vehicle;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class VehicleApiController
{
    public function index(Request $request): JsonResponse
    {
        $q = Vehicle::query()->with('client:id,name,phone');
        if ($s = $request->query('search')) {
            $q->where(function ($qq) use ($s) {
                $qq->where('plate', 'like', "%{$s}%")
                   ->orWhere('vin', 'like', "%{$s}%")
                   ->orWhere('brand', 'like', "%{$s}%")
                   ->orWhere('model', 'like', "%{$s}%");
            });
        }
        return response()->json($q->orderBy('plate')->paginate(min((int) $request->query('per_page', 25), 100)));
    }

    public function show(Vehicle $vehicle): JsonResponse
    {
        return response()->json($vehicle->load(['client', 'workOrders' => fn ($q) => $q->latest()->limit(10)]));
    }

    public function store(Request $request): JsonResponse
    {
        $data = $request->validate([
            'client_id' => 'required|exists:clients,id',
            'plate' => 'required|string|max:30',
            'vin' => 'nullable|string|max:30',
            'brand' => 'nullable|string|max:60',
            'model' => 'nullable|string|max:60',
            'year' => 'nullable|integer|min:1900|max:2100',
            'mileage' => 'nullable|integer|min:0',
            'fuel' => 'nullable|string|max:30',
            'engine' => 'nullable|string|max:60',
            'gearbox' => 'nullable|string|max:30',
            'color' => 'nullable|string|max:30',
            'notes' => 'nullable|string',
        ]);
        $v = Vehicle::create($data);
        return response()->json($v, 201);
    }

    public function update(Request $request, Vehicle $vehicle): JsonResponse
    {
        $data = $request->validate([
            'plate' => 'sometimes|string|max:30',
            'mileage' => 'nullable|integer|min:0',
            'notes' => 'nullable|string',
        ]);
        $vehicle->update($data);
        return response()->json($vehicle);
    }

    public function destroy(Vehicle $vehicle): JsonResponse
    {
        $vehicle->delete();
        return response()->json(['ok' => true]);
    }
}
