<?php

namespace App\Modules\AntiTheft\Http;

use App\Modules\AntiTheft\Domain\SignalType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class SecuritySignalRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user() !== null;
    }

    public function rules(): array
    {
        return [
            'device_uuid' => ['required', 'uuid', 'exists:devices,uuid'],
            'signal_type' => ['required', Rule::enum(SignalType::class)],
            'occurred_at' => ['required', 'date', 'before_or_equal:now'],
            'confidence' => ['required', 'numeric', 'between:0,1'],
            'magnitude' => ['nullable', 'numeric', 'min:0', 'max:1000'],
            'context' => ['nullable', 'array', 'max:20'],
            'context.latitude' => ['nullable', 'numeric', 'between:-90,90'],
            'context.longitude' => ['nullable', 'numeric', 'between:-180,180'],
            'context.address' => ['nullable', 'string', 'max:255'],
            'context.battery_percentage' => ['nullable', 'integer', 'between:0,100'],
            'context.network_status' => ['nullable', 'string', 'max:30'],
            'context.gps_status' => ['nullable', 'string', 'max:30'],
            'context.wifi_status' => ['nullable', 'boolean'],
            'context.mobile_data_status' => ['nullable', 'boolean'],
            'context.sensor' => ['nullable', 'string', 'max:50'],
            'context.sample_count' => ['nullable', 'integer', 'between:1,10000'],
        ];
    }

    public function idempotencyKey(): string
    {
        return trim((string) $this->header('Idempotency-Key'));
    }
}
