<?php

namespace App\Http\Controllers\Admin\Master;

use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Concerns\ParsesDataTableQuery;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Master\SupplierRequest;
use App\Models\Supplier;
use App\Services\Master\SupplierService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class SupplierController extends Controller
{
    use FlashesEntityMessage, ParsesDataTableQuery;

    public function __construct(
        private readonly SupplierService $supplierService,
    ) {}

    public function index(Request $request): Response
    {
        $tableQuery = $this->parseDataTableQuery($request);

        return Inertia::render('admin/master/suppliers/Index', [
            'suppliers' => $this->supplierService->paginateForIndex($tableQuery),
            'filters' => $this->dataTableFilters($tableQuery),
        ]);
    }

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

        $this->flashCreated('Supplier');

        return redirect()->route('admin.master.suppliers.index');
    }

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

        $this->flashUpdated('Supplier');

        return redirect()->route('admin.master.suppliers.index');
    }

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

        $this->flashDeleted('Supplier');

        return redirect()->route('admin.master.suppliers.index');
    }
}
