<?php

namespace App\Http\Requests;

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

/**
 * StoreHealthInstitutionRequest
 *
 * Validates health institution creation data.
 * Supports multi-step registration workflow.
 *
 * Requirements: 1.1, 1.4, 10.1
 */
class StoreHealthInstitutionRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true; // Public registration allowed
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            // Basic Information
            'name' => [
                'required',
                'string',
                'max:191',
                'min:3',
            ],
            'short_name' => [
                'nullable',
                'string',
                'max:100',
            ],
            'logo' => [
                'nullable',
                'image',
                'mimes:jpeg,jpg,png,webp,svg',
                'max:2048', // 2MB max
            ],

            // Location Information
            'city' => [
                'required',
                'string',
                'max:100',
            ],
            'address' => [
                'required',
                'string',
                'max:191',
            ],
            'country' => [
                'required',
                'string',
                'max:100',
            ],

            // Contact Information
            'email' => [
                'required',
                'email',
                'max:191',
            ],
            'phone' => [
                'required',
                'string',
                'max:50',
                'regex:/^[+]?[0-9\s\-\(\)]{7,20}$/',
            ],
            'website_url' => [
                'nullable',
                'url',
                'max:191',
            ],

            // Content
            'description' => [
                'required',
                'string',
                'min:50',
                'max:5000',
            ],
            'services' => [
                'nullable',
                'array',
            ],
            'services.*' => [
                'string',
                'max:191',
            ],

            // Images
            'images' => [
                'nullable',
                'array',
                'max:10',
            ],
            'images.*' => [
                'image',
                'mimes:jpeg,jpg,png,webp',
                'max:2048',
            ],

            // Contact Person (for registration)
            'contact_name' => [
                'required',
                'string',
                'max:191',
            ],
            'contact_email' => [
                'required',
                'email',
                'max:191',
            ],
            'contact_phone' => [
                'required',
                'string',
                'max:50',
            ],
            'contact_password' => [
                'required',
                'string',
                'min:8',
                'confirmed',
            ],
            // Terms must be accepted
            'terms' => [
                'required',
                'accepted',
            ],
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'name.required' => 'The institution name is required.',
            'name.min' => 'The institution name must be at least 3 characters.',
            'city.required' => 'Please select a city.',
            'email.required' => 'A contact email is required.',
            'email.email' => 'Please enter a valid email address.',
            'phone.required' => 'A phone number is required.',
            'phone.regex' => 'Please enter a valid phone number.',
            'description.min' => 'Please provide a more detailed description (at least 50 characters).',
            'services.*.max' => 'Each service name must not exceed 191 characters.',
            'images.max' => 'You can upload a maximum of 10 images.',
            'images.*.max' => 'Each image must not exceed 2MB.',
            'logo.max' => 'Logo must not exceed 2MB.',
            'contact_name.required' => 'The contact person name is required.',
            'contact_email.required' => 'The contact person email is required.',
            'contact_phone.required' => 'The contact person phone is required.',
            'contact_password.required' => 'A password is required for your account.',
            'contact_password.min' => 'Password must be at least 8 characters.',
            'contact_password.confirmed' => 'The password confirmation does not match.',
            'terms.required' => 'You must accept the Terms of Service to continue.',
            'terms.accepted' => 'You must accept the Terms of Service to continue.',
        ];
    }

    /**
     * Prepare the data for validation.
     */
    protected function prepareForValidation(): void
    {
        // Sanitize text inputs
        $this->merge([
            'name' => strip_tags($this->name),
            'short_name' => $this->short_name ? strip_tags($this->short_name) : null,
            'city' => strip_tags($this->city),
            'address' => strip_tags($this->address),
            'country' => strip_tags($this->country),
            'description' => strip_tags($this->description, '<p><br><strong><em><ul><ol><li>'),
        ]);

        // Normalize phone number
        if ($this->phone) {
            $this->merge([
                'phone' => preg_replace('/[^0-9+]/', '', $this->phone),
            ]);
        }
    }
}
