<?php

namespace App\Http\Controllers\Admin\Services;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Services\AcademicAdvisingLogRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\AcademicAdvisingLog;
use App\Services\Admin\Services\AcademicAdvisingLogService;
use App\Services\Admin\Users\LecturerService;
use App\Services\Admin\Users\StudentService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class AcademicAdvisingLogController extends Controller
{
    public function __construct(
        private readonly AcademicAdvisingLogService $service,
        private readonly StudentService $studentService,
        private readonly LecturerService $lecturerService,
    ) {}

    public function index(PaginatedRequest $request): Response
    {
        return Inertia::render('admin/services/academic-advising-logs/index', [
            'logs' => $this->service->paginated(
                ...$request->validatedWithDefaults(),
                lecturerId: $request->validated('lecturer_id'),
            ),
            'students' => $this->studentService->getAllForSelect(),
            'lecturers' => $this->lecturerService->getAllForSelect(),
            'filters' => $request->only(['lecturer_id']),
        ]);
    }

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

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

        return to_route('admin.services.academic-advising-logs.index');
    }

    public function update(AcademicAdvisingLogRequest $request, AcademicAdvisingLog $academicAdvisingLog): RedirectResponse
    {
        $this->service->update($academicAdvisingLog, $request->validated());

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

        return to_route('admin.services.academic-advising-logs.index');
    }

    public function destroy(AcademicAdvisingLog $academicAdvisingLog): RedirectResponse
    {
        $this->service->delete($academicAdvisingLog);

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