<?php

namespace App\Http\Requests\Admin\Users\Student;

use App\Enums\Gender;
use App\Enums\StudentStatus;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StudentRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $userId = $this->route('user')?->id;

        return [
            'username' => [
                'required',
                'string',
                'max:20',
                'min:5',
                'alpha_dash',
                $userId === null
                    ? Rule::unique('users')
                    : Rule::unique('users')->ignore($userId),
            ],
            'email' => [
                'required',
                'string',
                'email',
                'max:255',
                $userId === null
                    ? Rule::unique('users')
                    : Rule::unique('users')->ignore($userId),
            ],

            // Profile
            'full_name' => ['required', 'string', 'max:150'],
            'phone_number' => ['required', 'string', 'max:20'],
            'address' => ['required', 'string'],
            'gender' => ['required', 'string', Rule::in(Gender::values())],
            'birth_date' => ['required', 'date'],
            'birth_place' => ['required', 'string', 'max:100'],

            // Student fields
            'student_number' => [
                'required',
                'string',
                'max:10',
                Rule::unique('students')->ignore($userId, 'user_id'),
            ],
            'department_id' => [
                'required',
                'integer',
                Rule::exists('departments', 'id'),
            ],
            'enrollment_year' => [
                'required',
                'integer',
                'digits:4',
            ],
            'academic_advisor_id' => [
                'required',
                'integer',
                Rule::exists('lecturers', 'id'),
            ],
            'status' => [
                'nullable',
                'string',
                Rule::in(StudentStatus::values()),
            ],
        ];
    }
}
