<?php

namespace App\Http\Controllers\Api;

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

class ClientApiController
{
    public function index(Request $request): JsonResponse
    {
        $q = Client::query();
        if ($s = $request->query('search')) {
            $q->where(function ($qq) use ($s) {
                $qq->where('name', 'like', "%{$s}%")
                   ->orWhere('phone', 'like', "%{$s}%")
                   ->orWhere('email', 'like', "%{$s}%");
            });
        }
        return response()->json($q->orderBy('name')->paginate(min((int) $request->query('per_page', 25), 100)));
    }

    public function show(Client $client): JsonResponse
    {
        return response()->json($client->load('vehicles'));
    }

    public function store(Request $request): JsonResponse
    {
        $data = $request->validate([
            'name' => 'required|string|max:120',
            'phone' => 'required|string|max:40',
            'phone_alt' => 'nullable|string|max:40',
            'email' => 'nullable|email|max:120',
            'company_name' => 'nullable|string|max:160',
            'type' => 'in:individual,company',
            'status' => 'in:new,active,vip,debtor,blocked,lost',
            'discount_pct' => 'nullable|numeric|min:0|max:100',
            'notes' => 'nullable|string',
        ]);
        $client = Client::create($data + ['type' => 'individual', 'status' => 'active']);
        return response()->json($client, 201);
    }

    public function update(Request $request, Client $client): JsonResponse
    {
        $data = $request->validate([
            'name' => 'sometimes|string|max:120',
            'phone' => 'sometimes|string|max:40',
            'phone_alt' => 'nullable|string|max:40',
            'email' => 'nullable|email|max:120',
            'company_name' => 'nullable|string|max:160',
            'status' => 'in:new,active,vip,debtor,blocked,lost',
            'discount_pct' => 'nullable|numeric|min:0|max:100',
            'notes' => 'nullable|string',
        ]);
        $client->update($data);
        return response()->json($client);
    }

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