<?php

namespace App\Http\Requests\Registration;

use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        return [
            'organization_name' => ['required', 'string', 'max:191'],
            'legal_name'        => ['nullable', 'string', 'max:191'],
            'organization_type' => ['required', 'string', 'in:government,ngo,private_sector,academic,healthcare,international,community_based,faith_based,other'],
            'description'       => ['required', 'string', 'max:3000'],
            'mission_statement' => ['nullable', 'string', 'max:2000'],
            'email'             => ['required', 'string', 'email', 'max:191', 'unique:users,email'],
            'phone'             => ['nullable', 'string', 'max:50'],
            'website'           => ['nullable', 'url', 'max:500'],
            'address'           => ['nullable', 'string', 'max:500'],
            'city'              => ['nullable', 'string', 'max:100'],
            'country'           => ['nullable', 'string', 'max:100'],
            'services_offered'  => ['nullable', 'array'],
            'services_offered.*'=> ['string', 'max:191'],
            // Primary contact person
            'contact_name'      => ['required', 'string', 'max:191'],
            'contact_email'     => ['required', 'string', 'email', 'max:191'],
            'contact_phone'     => ['nullable', 'string', 'max:50'],
            'contact_title'     => ['nullable', 'string', 'max:191'],
            // Account credentials (email above is used as login)
            'password'          => ['required', 'string', 'min:8', 'confirmed'],
            'terms_accepted'    => ['required', 'accepted'],
        ];
    }

    public function messages(): array
    {
        return [
            'organization_name.required' => 'Please enter your organization name.',
            'organization_type.required' => 'Please select your organization type.',
            'organization_type.in'       => 'Please select a valid organization type.',
            'description.required'       => 'Please provide a description of your organization.',
            'email.required'             => 'Please enter the organization email address.',
            'email.email'                => 'Please enter a valid email address.',
            'email.unique'               => 'This email is already registered. <a href="' . route('login') . '" class="underline">Login instead?</a>',
            'website.url'                => 'Please enter a valid website URL (e.g. https://example.com).',
            'contact_name.required'      => 'Please enter the primary contact person\'s name.',
            'contact_email.required'     => 'Please enter the primary contact person\'s email.',
            'contact_email.email'        => 'Please enter a valid contact email address.',
            'password.required'          => 'Please create a password for your account.',
            'password.min'               => 'Password must be at least 8 characters.',
            'password.confirmed'         => 'Password confirmation does not match.',
            'terms_accepted.required'    => 'You must accept the terms and conditions.',
            'terms_accepted.accepted'    => 'You must accept the terms and conditions.',
        ];
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'organization_name' => strip_tags(trim($this->organization_name ?? '')),
            'email'             => strtolower(trim($this->email ?? '')),
            'contact_email'     => strtolower(trim($this->contact_email ?? '')),
        ]);
    }
}
