<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;

/**
 * Editing Suppliers
 */
class ServiceCancel extends FormRequest
{
	/**
	 * Determine if the user is authorized to make this request.
	 *
	 * @return bool
	 */
	public function authorize()
	{
		return Gate::allows('view',$this->route('o'));
	}

	/**
	 * Get the validation rules that apply to the request.
	 *
	 * @return array<string, mixed>
	 */
	public function rules()
	{
		//dd(request()->post());
		return [
			'stop_at'=> [
				'required',
				'date',
				'after:today',
				'exclude_unless:extra_charges,null',
				function($attribute,$value,$fail) {
					if ($this->route('o')->cancel_date->greaterThan($value))
						$fail(sprintf('Service cannot be cancelled before: %s',$this->route('o')->cancel_date->format('Y-m-d')));
				}
			],
			'extra_charges_amount' => [
				'nullable',
				'exclude_unless:extra_charges,null',
				function($attribute,$value,$fail) {
					if ($this->route('o')->cancel_date->greaterThan(request('stop_at')) && (request('extra_charges') !== 1))
						$fail('Extra charges must be accepted if cancelling before contract end');
				},
			],
			'extra_charges' => 'sometimes|required|accepted',
			'notes' => 'nullable|min:5',
		];
	}
}