<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreClientRequest;
use App\Models\Client;
use App\Services\ActivityLogger;
use Illuminate\Http\Request;
class ClientController extends Controller
{
    public function index(Request $request) {
        $clients = Client::when($request->filled('q'), fn($q) => $q->where('name', 'like', '%'.$request->q.'%'))->latest()->paginate(15);
        return view('admin.clients.index', compact('clients'));
    }
    public function create() { return view('admin.clients.create'); }
    public function edit(Client $client) { return view('admin.clients.edit', compact('client')); }
    public function store(StoreClientRequest $request)
    {
        $client = Client::create($request->validated());
        ActivityLogger::log(auth()->id(), 'clients', 'create', Client::class, $client->id, 'Cliente criado', $request->ip(), $request->userAgent(), $request->validated());
        return redirect()->route('admin.clients.index');
    }
    public function update(StoreClientRequest $request, Client $client)
    {
        $client->update($request->validated());
        ActivityLogger::log(auth()->id(), 'clients', 'update', Client::class, $client->id, 'Cliente atualizado', $request->ip(), $request->userAgent(), $request->validated());
        return redirect()->route('admin.clients.index');
    }
    public function destroy(Request $request, Client $client)
    {
        $client->delete();
        ActivityLogger::log(auth()->id(), 'clients', 'delete', Client::class, $client->id, 'Cliente removido logicamente', $request->ip(), $request->userAgent());
        return redirect()->route('admin.clients.index');
    }
}
