<?php

namespace App\Http\Requests\Admin\AdminManagement;

use Illuminate\Foundation\Http\FormRequest;

class AdminManagementUpdateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return $this->user()->can('update', $this->route('admin'));
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $admin = $this->route('admin');

        return [
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:admins,email,'.$admin->id,
            'password' => ['nullable', 'confirmed', ...\App\Services\Auth\PasswordPolicy::rules()],
            'status' => 'nullable|in:active,inactive,suspended',
            'email_verified' => 'nullable|boolean',
            'roles' => 'nullable|array',
            'roles.*' => 'string|exists:roles,name',

            // Notification channel IDs
            'phone' => 'nullable|string|max:20',
            'slack_user_id' => 'nullable|string|max:255',
            'telegram_user_id' => 'nullable|string|max:255',
            'discord_webhook' => 'nullable|url|max:500',
            'teams_webhook' => 'nullable|url|max:2048', // PRC-782 — Workflows SAS URL 1000+ char
            'pushover_user_key' => 'nullable|string|max:255',

            // Per-admin notification preferences
            'notification_preferences' => 'nullable|array',
            'notification_preferences.*' => 'nullable|array',
            'notification_preferences.*.*' => 'string|in:email,slack,telegram,discord,teams,whatsapp,pushover,sms',
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'name.required' => 'İsim alanı zorunludur.',
            'email.required' => 'E-posta alanı zorunludur.',
            'email.email' => 'Geçerli bir e-posta adresi giriniz.',
            'email.unique' => 'Bu e-posta adresi zaten kullanılmaktadır.',
            'password.min' => 'Şifre en az 8 karakter olmalıdır.',
            'password.confirmed' => 'Şifre doğrulaması eşleşmiyor.',
            'roles.*.exists' => 'Seçilen rol geçerli değildir.',
        ];
    }
}
