<?php

namespace Modules\Billing\Http\Requests\Service;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Modules\Billing\Enums\ServiceTypeEnum;
use Modules\Core\Enums\AmountTypeEnum;
use Modules\Core\Enums\PriceTypeEnum;
use Modules\Core\Enums\VatTypeEnum;
use Modules\Core\Enums\StatusEnum;
use Modules\Core\Traits\FailedValidation;

class StoreServiceOfferingRequest extends FormRequest
{
    use FailedValidation;

    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:255'],
            'service_code' => ['nullable', 'string', 'max:20', 'regex:/.*\d+$/', 'unique:service_offerings,service_code'],
            'service_department_id' => ['required', 'exists:service_departments,id'],
            'service_type' => ['required', Rule::in(ServiceTypeEnum::casesAsArray())],
            'base_fee' => ['required', 'numeric', 'min:0'],
            'vat_price_type' => ['required', Rule::in(PriceTypeEnum::casesAsArray())],
            'vat_type' => ['required', Rule::in(VatTypeEnum::casesAsArray())],
            'vat_rate' => ['required', 'numeric', 'min:0'],
            'subsidy_type' => ['required', Rule::in(AmountTypeEnum::casesAsArray())],
            'subsidy_value' => ['nullable', 'numeric', 'min:0'],
            'status' => ['required', Rule::in(StatusEnum::casesAsArray())],
        ];
    }

    public function attributes(): array
    {
        return [
            'service_code' => 'service code',
        ];
    }

    public function messages(): array
    {
        return [
            'service_code.regex' =>
                'The service code must end with numeric digits.',
        ];
    }

    public function authorize(): bool
    {
        return true;
    }
}
