<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        $rules = [
            'client_id'                => 'sometimes|required|exists:clients,id',
            'provider_id'              => 'sometimes|required|exists:providers,id',
            'address_id'               => 'sometimes|required|exists:addresses,id',
            'date'                     => 'nullable|date|after_or_equal:today',
            'offers_meal'              => 'sometimes|nullable|boolean',
            'period_type'              => 'sometimes|required|in:2,4,6,8',
            'schedule_type'            => 'sometimes|in:default,custom',
            'start_time'               => 'sometimes|required|date_format:H:i',
            'end_time'                 => 'sometimes|required|date_format:H:i|after:start_time',
            'total_amount'             => 'sometimes|required|numeric|min:0',
            'status'                   => 'sometimes|in:pending,accepted,rejected,paid,cancelled,started,finished',
            'code_verified'            => 'sometimes|boolean',
        ];

        if ($this->isMethod('POST')) {
            $rules['client_id']    = 'required|exists:clients,id';
            $rules['provider_id']  = 'required|exists:providers,id';
            $rules['address_id']   = 'required|exists:addresses,id';
            $rules['date']         = 'required|date|after_or_equal:today';
            $rules['period_type']  = 'required|in:2,4,6,8';
            $rules['start_time']   = 'required|date_format:H:i';
            $rules['end_time']     = 'required|date_format:H:i|after:start_time';
            $rules['total_amount'] = 'required|numeric|min:0';
            $rules['status']       = 'in:pending';
        }

        return $rules;
    }

    public function messages(): array
    {
        return [
            'client_id.required'     => __('requests.schedule.client_required'),
            'client_id.exists'       => __('requests.schedule.client_not_found'),
            'provider_id.required'   => __('requests.schedule.provider_required'),
            'provider_id.exists'     => __('requests.schedule.provider_not_found'),
            'address_id.required'    => __('requests.schedule.address_required'),
            'address_id.exists'      => __('requests.schedule.address_not_found'),
            'date.required'          => __('requests.schedule.date_required'),
            'date.date'              => __('requests.schedule.invalid_date'),
            'date.after_or_equal'    => __('requests.schedule.date_after_or_equal'),
            'period_type.required'   => __('requests.schedule.period_required'),
            'period_type.in'         => __('requests.schedule.invalid_period'),
            'start_time.required'    => __('requests.schedule.start_time_required'),
            'start_time.date_format' => __('requests.schedule.invalid_time_format'),
            'end_time.required'      => __('requests.schedule.end_time_required'),
            'end_time.date_format'   => __('requests.schedule.invalid_time_format'),
            'end_time.after'         => __('requests.schedule.end_time_after'),
            'total_amount.required'  => __('requests.schedule.total_amount_required'),
            'total_amount.numeric'   => __('requests.schedule.total_amount_numeric'),
            'total_amount.min'       => __('requests.schedule.total_amount_min'),
            'status.in'              => __('requests.schedule.invalid_status'),
        ];
    }
}
