<?php

namespace App\Http\Requests;

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

class RegisterProviderRequest extends FormRequest
{
  public function rules(): array
  {
    $rules = [
      'email'      => 'sometimes|email',
      'phone'      => 'sometimes|string|nullable|max:20',
      'name'       => 'required|string|max:255',
      'code'       => 'required|string|max:6',
      'document'   => ['required', 'string', 'max:20'],
      'rg'         => 'required|string|max:20',
      'birth_date' => 'required|date|before:today',

      'recipient_name'        => 'sometimes|string|max:255',
      'recipient_email'       => 'sometimes|email|max:255',
      'recipient_description' => 'sometimes|string',
      'recipient_document'    => 'sometimes|string|max:20',
      'recipient_type'        => ['sometimes', Rule::in(['individual', 'company'])],

      'recipient_payment_mode' => ['sometimes', Rule::in(['bank_transfer'])],

      'recipient_default_bank_account'                     => 'sometimes|array',
      'recipient_default_bank_account.holder_name'         => 'sometimes|string|max:255',
      'recipient_default_bank_account.holder_type'         => ['sometimes', Rule::in(['individual', 'company'])],
      'recipient_default_bank_account.holder_document'     => 'sometimes|string|max:20',
      'recipient_default_bank_account.bank'                => 'sometimes|string|max:3',
      'recipient_default_bank_account.branch_number'       => 'sometimes|string|max:20',
      'recipient_default_bank_account.branch_check_digit'  => 'sometimes|nullable|string|max:10',
      'recipient_default_bank_account.account_number'      => 'sometimes|string|max:20',
      'recipient_default_bank_account.account_check_digit' => 'sometimes|string|max:10',
      'recipient_default_bank_account.type'                => ['sometimes', Rule::in(['checking', 'savings'])],
      'recipient_default_bank_account.metadata'            => 'sometimes|array',
      'recipient_default_bank_account.pix_key'             => 'sometimes|nullable|string|max:255',

      'recipient_metadata' => 'sometimes|array',

      'zip_code'       => 'required|string|max:20',
      'address'        => 'required|string|max:255',
      'has_complement' => 'sometimes|boolean',
      'complement'     => 'nullable|string|max:255',
      'nickname'       => 'nullable|string|max:255',
      'instructions'   => 'nullable|string',
      'address_type'   => ['required', Rule::in(['home', 'commercial', 'other'])],
      'city'           => 'nullable|string|max:255',
      'state'          => 'nullable|string|max:2',

      'daily_price_8h' => 'required|numeric|min:100|max:500',
      'daily_price_6h' => 'required|numeric|min:0',
      'daily_price_4h' => 'required|numeric|min:0',
      'daily_price_2h' => 'required|numeric|min:0',

      'services_types_ids' => 'sometimes|array',

      'services_types_ids.*' => [
        'integer',
        Rule::exists('service_types', 'id')->where(function ($query) {
          $query->whereNull('deleted_at')->where('is_active', true);
        }),
      ],

      'service_types_ids' => 'sometimes|array',

      'service_types_ids.*' => [
        'integer',
        Rule::exists('service_types', 'id')->where(function ($query) {
          $query->whereNull('deleted_at')->where('is_active', true);
        }),
      ],

      'working_days'          => 'required|array|min:1',
      'working_days.*.day'    => 'required|integer|min:0|max:6',
      'working_days.*.period' => ['required', Rule::in([WorkingPeriodEnum::MORNING->value, WorkingPeriodEnum::AFTERNOON->value])],

      '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',
    ];

    if (!$this->has('email')) {
      $rules['phone'] = 'required|string|max:20';
      $rules['email'] = 'nullable';
    }

    if (!$this->has('phone')) {
      $rules['email'] = 'required|email';
      $rules['phone'] = 'nullable';
    }

    return $rules;
  }
}
