<?php

namespace App\Http\Controllers;

use App\Http\Requests\ClienteRequest;
use App\Models\Cliente;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class ClienteController extends Controller
{
    public function index(): View
    {
        $clientes = Cliente::orderBy('nombre')->paginate(15);

        return view('catalogos.clientes.index', compact('clientes'));
    }

    public function create(): View
    {
        return view('catalogos.clientes.create');
    }

    public function store(ClienteRequest $request): RedirectResponse
    {
        Cliente::create($request->validated());

        return redirect()->route('clientes.index')->with('status', 'Cliente registrado correctamente.');
    }

    public function edit(Cliente $cliente): View
    {
        return view('catalogos.clientes.edit', compact('cliente'));
    }

    public function update(ClienteRequest $request, Cliente $cliente): RedirectResponse
    {
        $cliente->update($request->validated());

        return redirect()->route('clientes.index')->with('status', 'Cliente actualizado correctamente.');
    }

    public function toggle(Cliente $cliente): RedirectResponse
    {
        $cliente->update(['active' => ! $cliente->active]);

        return redirect()->route('clientes.index')->with('status', 'Estado del cliente actualizado.');
    }
}
