<?php

namespace App\Http\Requests\Products\Addon;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;

class AddonCreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        if (auth()->user()->hasRole('administrator')) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, ValidationRule|array|string>
     */
    public function rules(): array
    {
        return [
            'name' => 'required|string|max:255',
            'description' => 'nullable|string|max:255',
            'billing_type' => 'required|in:1,2',
            'price' => 'required|numeric|min:0',
            'billing_cycle' => 'required_if:billing_type,2|nullable|in:1,2,3,4',
            'billing_cycle_count' => 'nullable|numeric|min:0',
            'trial_available' => 'boolean',
            'trial_duration' => 'required_if:trial_available,true|nullable|numeric|min:1',
            'trial_duration_unit' => 'required_if:trial_available,true|nullable|in:1,2,3,4',
            'product_level' => 'boolean',
            'plans' => 'required_without:product_level|array',
            'plans.*' => 'exists:plans,id',
        ];
    }

    public function messages(): array
    {
        return [
            'billing_type.in' => 'The selected billing type is invalid. Valid options are: ' . getConstantLabel('billing_type', 1) . ', ' . getConstantLabel('billing_type', 2) . '.',
            'billing_cycle.required_if' => 'The billing cycle is required when the billing type is ' . getConstantLabel('billing_type', 2) . '.',
            'billing_cycle_count.required_if' => 'The billing cycle count is required when the billing type is ' . getConstantLabel('billing_type', 2) . '.',
            'trial_duration.required_if' => 'The trial duration is required when trial is available.',
            'trial_duration_unit.required_if' => 'The trial duration unit is required when trial is available.',
            'plans.required_without' => 'The plans field is required when product level is not selected.',
            'plans.*.exists' => 'The selected plan is invalid.',
        ];
    }
}
