<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

use App\Models\{Charge,InvoiceItem};

class ChargeAdd extends FormRequest
{
	/**
	 * Determine if the user is authorized to make this request.
	 */
	public function authorize(): bool
	{
		return Auth::user()
			->accounts_all
			->contains(request()->post('account_id'));
	}

	/**
	 * Get the validation rules that apply to the request.
	 *
	 * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
	 */
	public function rules(): array
	{
		Session::put('charge_add',true);

		return [
			'id' => 'sometimes|exists:charges,id',
			'account_id' => 'required|exists:accounts,id',
			'charge_at' => 'required|date',
			'service_id' => 'required|exists:services,id',
			'site_id' => 'required|exists:sites,id',
			'quantity' => 'required|numeric|not_in:0',
			'amount' => 'required|numeric|min:0.01',
			'sweep_type' => 'required|numeric|in:'.implode(',',array_keys(Charge::sweep)),
			'type' => 'required|numeric|in:'.implode(',',array_keys(InvoiceItem::type)),
			'taxable' => 'nullable|boolean',
			'description' => 'nullable|string|min:5|max:128',
		];
	}
}