<?php

declare(strict_types=1);

namespace App\Http\Requests\Rating;

use Illuminate\Foundation\Http\FormRequest;

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

  public function rules(): array
  {
    $rules = [
      'rate' => ['required_without:score', 'integer', 'between:1,5'],
      'score' => ['required_without:rate', 'integer', 'between:1,5'],
      'comment' => ['nullable', 'string', 'max:2000'],
      'media_item_id' => ['nullable', 'integer', 'exists:media_items,id'],
    ];

    if (! $this->route('project')) {
      $rules['rateable_type'] = ['required', 'string', 'max:255'];
      $rules['rateable_id'] = ['required', 'integer', 'min:1'];
    }

    return $rules;
  }

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