<?php

declare(strict_types=1);

namespace App\Http\Requests\Rating;

use Illuminate\Foundation\Http\FormRequest;

class UpdateRatingRequest extends FormRequest
{
  public function authorize(): bool
  {
    return true;
  }

  public function rules(): array
  {
    return [
      'rate' => ['sometimes', 'required_without:score', 'integer', 'between:1,5'],
      'score' => ['sometimes', 'required_without:rate', 'integer', 'between:1,5'],
      'comment' => ['nullable', 'string', 'max:2000'],
    ];
  }

  public function scoreValue(): ?int
  {
    if ($this->has('rate') || $this->has('score')) {
      return (int) ($this->input('rate') ?? $this->input('score'));
    }

    return null;
  }
}
