<?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\Models\Lecturer;
use App\Services\Admin\Services\AcademicAdvisingLogService;
use App\Services\Admin\Users\LecturerService;
use App\Services\Admin\Users\StudentService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
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 mine(Request $request): Response
    {
        $lecturer = $this->currentLecturer($request);

        return Inertia::render('lecturer/academic-advising-logs/index', [
            'logs' => $this->service->forLecturer($lecturer),
            'advisees' => $lecturer->advisees()
                ->with('user.profile')
                ->get(['id', 'user_id', 'student_number']),
        ]);
    }

    public function store(AcademicAdvisingLogRequest $request): RedirectResponse
    {
        if ($request->routeIs('lecturer.*')) {
            $lecturer = $this->currentLecturer($request);

            $this->service->create([
                'student_id' => $request->validated('student_id'),
                'lecturer_id' => $lecturer->id,
                'topic' => $request->validated('topic'),
                'notes' => $request->validated('notes'),
                'session_date' => $request->validated('session_date'),
            ]);

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

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

        $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();
    }

    private function currentLecturer(Request $request): Lecturer
    {
        $lecturer = $request->user()->lecturer;

        abort_if(! $lecturer, 403);

        return $lecturer;
    }
}
