<?php

namespace App\Http\Requests;

use App\Enums\GenderEnum;
use App\Enums\WorkingPeriodEnum;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class RegisterProviderRequest extends FormRequest
{
  public function rules(): array
  {
    $dailyPriceMin = config('provider.daily_price_min');

    $rules = [
      'code' => 'required|string|max:6',

      'email' => [
        Rule::requiredIf(fn () => ! config('services.sms.enabled') || ! $this->filled('phone')),
        'string', 'email', 'max:255',
      ],

      'name'            => 'required|string|max:255',
      'phone'           => 'required|string|max:20',
      'document'        => 'required|string|max:20',
      'rg'              => 'required|string|max:20',
      'gender'          => 'required|string|in:'.implode(',', GenderEnum::toArray()),
      'birth_date'      => 'required|date|before:today',
      'zip_code'        => 'required|string|max:20',
      'number'          => 'required|string|max:20',
      'address'         => 'required|string|max:255',
      'district'        => 'required|string|max:255',
      'city'            => 'required|string|max:255',
      'state'           => 'required|string|max:2',
      'address_type'    => 'required|string|in:home,commercial,other',
      'has_complement'  => 'required|boolean',
      'daily_price_8h'  => "required|numeric|min:{$dailyPriceMin}|max:500",
      'daily_price_6h'  => 'required|numeric|min:0',
      'daily_price_4h'  => 'required|numeric|min:0',
      'daily_price_2h'  => 'required|numeric|min:0',
      'selfie'          => 'required|file|image|mimes:jpg,jpeg,png,webp|max:5120',
      'document_front'  => 'required|file|image|mimes:jpg,jpeg,png,webp|max:10240',
      'document_back'   => 'required|file|image|mimes:jpg,jpeg,png,webp|max:10240',

      'working_days'          => 'required|array|min:1',
      'working_days.*.day'    => 'required|integer|min:0|max:6',
      'working_days.*.period' => 'required|string|in:'.implode(',', array_column(WorkingPeriodEnum::cases(), 'value')),

      'complement'   => 'required|string|max:255',
      'nickname'     => 'sometimes|nullable|string|max:255',
      'latitude'     => 'sometimes|nullable|numeric',
      'longitude'    => 'sometimes|nullable|numeric',
      'instructions' => 'sometimes|nullable|string',

      //

      'recipient_email'        => 'sometimes|string|email|max:255',
      'recipient_name'         => 'sometimes|string|max:255',
      'recipient_document'     => 'sometimes|string|max:20',
      'recipient_description'  => 'sometimes|string',
      'recipient_type'         => 'sometimes|string|in:individual,company',
      'recipient_payment_mode' => 'sometimes|string|in:bank_transfer',

      'recipient_default_bank_account' => 'sometimes|array',
 
      'recipient_default_bank_account.holder_name'         => 'sometimes|string|max:255',
      'recipient_default_bank_account.holder_document'     => 'sometimes|string|max:20',
      'recipient_default_bank_account.holder_type'         => 'sometimes|string|in:individual,company',
      'recipient_default_bank_account.bank'                => 'sometimes|string|max:3',
      'recipient_default_bank_account.branch_number'       => 'sometimes|string|max:4',
      'recipient_default_bank_account.branch_check_digit'  => 'sometimes|string|nullable|max:2',
      'recipient_default_bank_account.account_number'      => 'sometimes|string|max:13',
      'recipient_default_bank_account.account_check_digit' => 'sometimes|string|max:2',
      'recipient_default_bank_account.type'                => 'sometimes|string|in:checking,savings',
      'recipient_default_bank_account.metadata'            => 'sometimes|array',

      'recipient_metadata' => 'sometimes|array',
      
      'services_types_ids'   => 'sometimes|array',
      'services_types_ids.*' => 'integer|exists:service_types,id,deleted_at,NULL,is_active,1',
    ];

    return $rules;
  }
}
