<?php

namespace App\Http\Requests\Promotions;

use App\Http\Requests\ApiFormRequest;
use App\Models\StudentPromotionRecord;
use Illuminate\Validation\Rule;

class AdminListPromotionsRequest extends ApiFormRequest
{
    public function rules(): array
    {
        return [
            'status' => ['nullable'],
            'status.*' => ['string', Rule::in(StudentPromotionRecord::ALL_STATUSES)],
            'next_school_year' => ['nullable', 'string', 'max:9'],
            'current_school_year' => ['nullable', 'string', 'max:9'],
            'parent_id' => ['nullable', 'integer', 'min:1'],
            'student_id' => ['nullable', 'integer', 'min:1'],
            'search' => ['nullable', 'string', 'max:191'],
            'parent_action_required' => ['nullable', 'boolean'],
            'page' => ['nullable', 'integer', 'min:1'],
            'per_page' => ['nullable', 'integer', 'min:1', 'max:200'],
        ];
    }

    public function filters(): array
    {
        $data = $this->validated();
        $status = $data['status'] ?? null;
        if (is_string($status) && $status !== '') {
            $status = [$status];
        } elseif (!is_array($status)) {
            $status = null;
        }

        return [
            'status' => $status,
            'next_school_year' => $data['next_school_year'] ?? null,
            'current_school_year' => $data['current_school_year'] ?? null,
            'parent_id' => isset($data['parent_id']) ? (int) $data['parent_id'] : null,
            'student_id' => isset($data['student_id']) ? (int) $data['student_id'] : null,
            'search' => $data['search'] ?? null,
            'parent_action_required' => isset($data['parent_action_required'])
                ? (bool) $data['parent_action_required']
                : false,
            'page' => isset($data['page']) ? (int) $data['page'] : 1,
            'per_page' => isset($data['per_page']) ? (int) $data['per_page'] : 50,
        ];
    }
}
