<?php

namespace App\Http\Controllers\Admin\Master;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Master\CustomerRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\Customer;
use App\Services\Admin\Master\CustomerService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class CustomerController extends Controller
{
    public function __construct(
        private CustomerService $service
    ) {}

    public function index(PaginatedRequest $request): Response
    {
        return Inertia::render('admin/master/customer/index', [
            'customers' => $this->service->paginated(...$request->validatedWithDefaults()),
        ]);
    }

    public function store(CustomerRequest $request): RedirectResponse
    {
        $this->service->store($request->validated());

        Inertia::flash('toast', ['type' => 'success', 'message' => 'Customer berhasil ditambahkan.']);

        return back();
    }

    public function update(CustomerRequest $request, Customer $customer): RedirectResponse
    {
        $this->service->update($customer, $request->validated());

        Inertia::flash('toast', ['type' => 'success', 'message' => 'Customer berhasil diperbarui.']);

        return back();
    }

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

        Inertia::flash('toast', ['type' => 'success', 'message' => 'Customer berhasil dihapus.']);

        return back();
    }
}
