<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Customer;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class CustomerController extends Controller
{
    public function index(): View
    {
        return view('admin.customers.index', [
            'customers' => Customer::query()
                ->withCount(['cartItems', 'favorites'])
                ->latest()
                ->paginate(15),
        ]);
    }

    public function show(Customer $customer): View
    {
        $customer->load(['cartItems.product', 'favorites.product']);

        return view('admin.customers.show', [
            'customer' => $customer,
        ]);
    }

    public function toggle(Customer $customer): RedirectResponse
    {
        $customer->update(['is_active' => ! $customer->is_active]);

        return back()->with('status', 'Customer status updated.');
    }

    public function destroy(Customer $customer): RedirectResponse
    {
        $customer->delete();

        return redirect()
            ->route('admin.customers.index')
            ->with('status', 'Customer deleted.');
    }
}
