<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

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

    /**
     * add / edit 用的 validation
     */
    public function rules(): array
    {
        if (!$this->isMethod('post') && !$this->isMethod('put')) {
            return [];
        }

        return [
            // 必須
            'city_id'   => ['required', 'integer'],
            'park_name' => ['required', 'string', 'max:255'],

            // 文字系
            'park_ruby'      => ['nullable', 'string', 'max:255'],
            'park_syllabary' => ['nullable', 'string', 'max:3'],
            'park_adrs'      => ['nullable', 'string', 'max:255'],
            'price_memo'     => ['nullable', 'string', 'max:1000'],

            // フラグ / 種別系（integer想定）
            'park_close_flag'               => ['nullable', 'integer'],
            'inverse_use_flag1'             => ['nullable', 'integer'], // 逆利用フラグ(一覧)
            'inverse_use_flag2'             => ['nullable', 'integer'], // 逆利用フラグ(学生)
            'parking_regulations_flag'      => ['nullable', 'integer'], // 駐輪規定フラグ
            'alert_flag'                    => ['nullable', 'integer'], // 残警告チェックフラグ

            // ★ここ：export/DB側は immediate_use_permit
            'immediate_use_permit'          => ['nullable', 'integer'], // 契約後即利用許可

            'gender_display_flag'           => ['nullable', 'integer'],
            'bd_display_flag'               => ['nullable', 'integer'],
            'securityreg_display_flag'      => ['nullable', 'integer'],
            'park_fixed_contract'           => ['nullable', 'integer'],
            'park_temporary_contract'       => ['nullable', 'integer'],
            'park_available_time_flag'      => ['nullable', 'integer'],
            'park_manager_flag'             => ['nullable', 'integer'],
            'park_roof_flag'                => ['nullable', 'integer'],
            'park_issuing_machine_flag'     => ['nullable', 'integer'],
            'reduction_guide_display_flag'  => ['nullable', 'integer'],
            'overyear_flag'                 => ['nullable', 'integer'],

            // 数値系
            'print_number'                         => ['nullable', 'integer'],
            'distance_twopoints'                   => ['nullable', 'integer'],
            'reduction_age'                        => ['nullable', 'integer'],
            'reduction_guide_display_start_month'  => ['nullable', 'integer'],

            // 日付/時刻系
            'park_day'                         => ['nullable', 'date'],
            'keep_alive'                       => ['nullable', 'date'],

            'update_grace_period_start_date'   => ['nullable', 'integer', 'between:1,31'],
            'update_grace_period_start_time'   => ['nullable', 'date_format:H:i'],
            'update_grace_period_end_date'     => ['nullable', 'integer', 'between:1,31'],
            'update_grace_period_end_time'     => ['nullable', 'date_format:H:i'],
            'parking_start_grace_period'       => ['nullable', 'integer', 'between:1,31'],

            'reminder_type'                    => ['nullable', 'integer'],
            'reminder_time'                    => ['nullable', 'date_format:H:i'],
            'park_available_time_from'         => ['nullable', 'date_format:H:i'],
            'park_available_time_to'           => ['nullable', 'date_format:H:i'],
            'park_manager_resident_from'       => ['nullable', 'date_format:H:i'],
            'park_manager_resident_to'         => ['nullable', 'date_format:H:i'],

            // 緯度/経度/電話など
            'park_latitude'                    => ['nullable', 'string', 'max:50'],
            'park_longitude'                   => ['nullable', 'string', 'max:50'],
            'park_tel'                         => ['nullable', 'string', 'max:50'],

            // 備考/自由入力
            'park_restriction'                 => ['nullable', 'string', 'max:50'],
            'park_procedure'                   => ['nullable', 'string', 'max:50'],
            'park_payment'                     => ['nullable', 'string', 'max:100'],
            'park_using_method'                => ['nullable', 'string', 'max:1000'],
            'park_contract_renewal_term'       => ['nullable', 'string', 'max:255'],
            'park_reservation'                 => ['nullable', 'string', 'max:255'],
            'park_reference'                   => ['nullable', 'string', 'max:1000'],

            // ★ここ：export/DB側は student_id_confirm_type
            'student_id_confirm_type'          => ['nullable', 'string', 'max:255'],
        ];
    }

    protected function prepareForValidation(): void
    {
        foreach ([
            'park_available_time_from',
            'park_available_time_to',
            'park_manager_resident_from',
            'park_manager_resident_to',
            'update_grace_period_start_time',
            'update_grace_period_end_time',
            'reminder_time',
        ] as $key) {
            if ($this->filled($key)) {
                $v = (string) $this->input($key);
                $this->merge([$key => substr($v, 0, 5)]); // HH:MM に統一
            }
        }
    }

    /**
     * add / edit payload
     */
    public function payload(): array
    {
        return $this->validated();
    }

    public function filters(): array
    {
        return [
            'park_name'  => $this->input('park_name'),
            'city_id'    => $this->input('city_id'),
            'sort'       => $this->input('sort'),
            'sort_type'  => $this->input('sort_type', 'asc'),
        ];
    }
}
