<?php

namespace App\Http\Controllers\Admin\Master;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Master\DepartmentRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\Department;
use App\Services\Admin\Master\DepartmentService;
use App\Services\Admin\Users\LecturerService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class DepartmentController extends Controller
{
    public function __construct(
        private readonly DepartmentService $service,
        private readonly LecturerService $lecturerService,
    ) {}

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

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

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

        return to_route('admin.master.departments.index');
    }

    public function update(DepartmentRequest $request, Department $department): RedirectResponse
    {
        $this->service->update($department, $request->validated());

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

        return to_route('admin.master.departments.index');
    }

    public function destroy(Department $department): RedirectResponse
    {
        $this->service->delete($department);

        return Inertia::flash('toast', ['type' => 'success', 'message' => 'Jurusan berhasil dihapus.'])->back();
    }
}
