<?php

namespace App\Http\Requests\Admin\Services;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class AcademicAdvisingLogRequest extends FormRequest
{
    public function authorize(): bool
    {
        if ($this->routeIs('lecturer.*')) {
            return (bool) $this->user()->lecturer;
        }

        return $this->user()->can($this->isMethod('post') ? 'create-academic-advising-logs' : 'update-academic-advising-logs');
    }

    public function rules(): array
    {
        if ($this->routeIs('lecturer.*')) {
            $lecturer = $this->user()->lecturer;
            $adviseeIds = $lecturer ? $lecturer->advisees()->pluck('id') : collect();

            return [
                'student_id' => ['required', 'integer', Rule::in($adviseeIds)],
                'topic' => ['nullable', 'string', 'max:150'],
                'notes' => ['nullable', 'string'],
                'session_date' => ['nullable', 'date'],
            ];
        }

        return [
            'student_id' => ['required', 'integer', Rule::exists('students', 'id')],
            'lecturer_id' => ['required', 'integer', Rule::exists('lecturers', 'id')],
            'topic' => ['nullable', 'string', 'max:150'],
            'notes' => ['nullable', 'string'],
            'session_date' => ['nullable', 'date'],
        ];
    }

    public function messages(): array
    {
        return [
            'student_id.in' => 'Mahasiswa yang dipilih bukan mahasiswa bimbingan Anda.',
        ];
    }
}
