<?php

namespace App\Http\Requests;

use App\Enums\WorkoutSource;
use App\Models\WorkoutSessions;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    /**
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        if ($this->isMethod('get')) {
            return [
                'date' => ['sometimes', 'date_format:Y-m-d'],
                'per_page' => ['sometimes', 'integer', 'min:1', 'max:100'],
            ];
        }

        return [
            'external_id' => [
                'nullable',
                'string',
                'max:255',
                Rule::unique((new WorkoutSessions)->getTable(), 'external_id')
                    ->where('source', $this->string('source', WorkoutSource::MANUAL->value)->toString()),
            ],
            'source' => ['sometimes', Rule::enum(WorkoutSource::class)],
            'type' => ['required', 'string', 'max:255'],
            'title' => ['required', 'string', 'max:255'],
            'duration_seconds' => ['required', 'integer', 'min:1'],
            'calories_burned' => ['required', 'integer', 'min:0'],
            'distance_meters' => ['required', 'integer', 'min:0'],
            'started_at' => ['nullable', 'date'],
        ];
    }
}
