<?php

namespace App\Http\Requests\Api;

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

class CourseOfConstructionFilterRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $yearString = '';
        for ($x = 1970; $x < 2100; $x++) {
            if ($x < 2100) {
                $yearString = $yearString . $x . ',';
            } else {
                $yearString = $yearString . $x;
            }
        }

        return [
            'slug' => ['required', Rule::exists('residential_complexes', 'code')
                ->whereNull('deleted_at')],
            'course_of_constructions.*.year' => 'nullable|in:' . $yearString,
            'course_of_constructions.*.month.*'=> 'nullable|in:01,02,03,04,05,06,07,08,09,10,11,12',
        ];
    }

    public function all($keys = null)
    {
        $data = parent::all();
        $data['slug'] = $this->route('slug');

        return $data;
    }

    public function messages()
    {
        return [
            'slug.exists' => 'residential complex was not found'
        ];
    }
}
