<?php

namespace App\Http\Controllers\Admin\Master;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Master\SupplierRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\Supplier;
use App\Services\Admin\Master\SupplierService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class SupplierController extends Controller
{
    public function __construct(
        private SupplierService $service
    ) {}

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

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

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

        return back();
    }

    public function update(SupplierRequest $request, Supplier $supplier): RedirectResponse
    {
        $this->service->update($supplier, $request->validated());

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

        return back();
    }

    public function destroy(Supplier $supplier): RedirectResponse
    {
        $this->service->destroy($supplier);

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

        return back();
    }
}
