<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    public function rules(): array
    {
        $signalType = $this->input('signal_type');

        $base = [
            'name' => ['required', 'string', 'max:255'],
            'signal_type' => ['required', Rule::in(['site', 'call', 'sms'])],
            'daily_limit_target' => ['required', 'integer', 'min:1', 'max:10000'],
            'region_mask' => ['required', 'integer', 'min:0'],
            'region_mode' => ['required', Rule::in(['include', 'exclude'])],
            'delivery_days_mask' => ['required', 'integer', 'min:1', 'max:127'],
        ];

        if ($signalType === 'site') {
            $base['signal_identifier'] = ['required', 'string', 'regex:/^[a-z0-9][a-z0-9\-]*(\.[a-z0-9][a-z0-9\-]*)*\.[a-z]{2,}$/i'];
        } elseif ($signalType === 'call') {
            $base['signal_identifier'] = ['required', 'string', 'regex:/^7\d{10}$/'];
        } elseif ($signalType === 'sms') {
            $base['sms_senders'] = ['required', 'array', 'min:1'];
            $base['sms_senders.*'] = ['string', 'max:11'];
            $base['sms_keyword'] = ['nullable', 'string', 'min:1', 'max:50'];
        }

        return $base;
    }
}
