<?php

namespace App\Http\Requests;

use App\Models\Investment;
use Illuminate\Foundation\Http\FormRequest;

class InitiateInvestmentRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true; // Public endpoint
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'investor_name' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'phone' => 'nullable|string|max:50',
            'country' => 'nullable|string|max:100',
            'investor_type' => 'nullable|in:individual,company,fund,family_office,institution',
            'investment_tier' => 'required|in:growth,strategic,anchor',
            'investment_amount' => 'required|numeric|min:1',
            'currency' => 'nullable|string|in:USD,KES',
            'investment_term' => 'nullable|integer|min:1|max:10',
            'terms_accepted' => 'required|boolean|accepted',
            'memorandum_read' => 'required|boolean|accepted',
            'risks_understood' => 'required|boolean|accepted',
            'risk_tolerance_confirmed' => 'required|boolean|accepted',
            'newsletter_subscribed' => 'nullable|boolean',
            'call_requested' => 'nullable|boolean',
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'investor_name.required' => 'Please enter your full name.',
            'email.required' => 'Please enter your email address.',
            'email.email' => 'Please enter a valid email address.',
            'investment_tier.required' => 'Please select an investment tier.',
            'investment_tier.in' => 'Invalid investment tier selected.',
            'investment_amount.required' => 'Please enter the investment amount.',
            'investment_amount.min' => 'Investment amount must be greater than 0.',
            'terms_accepted.accepted' => 'You must accept the terms and conditions.',
            'memorandum_read.accepted' => 'You must confirm reading the investment memorandum.',
            'risks_understood.accepted' => 'You must confirm understanding the investment risks.',
            'risk_tolerance_confirmed.accepted' => 'You must confirm your risk tolerance.',
        ];
    }

    /**
     * Prepare the data for validation.
     */
    protected function prepareForValidation(): void
    {
        $this->merge([
            'email' => strtolower($this->email ?? ''),
            'currency' => strtoupper($this->currency ?? 'USD'),
        ]);
    }

    /**
     * Configure the validator instance.
     */
    public function withValidator($validator): void
    {
        $validator->after(function ($validator) {
            $tier = $this->input('investment_tier');
            $amount = $this->input('investment_amount');

            if ($tier && $amount) {
                $currency = $this->input('currency', 'USD');
                
                if ($currency === 'KES') {
                    $minimum = Investment::TIER_MINIMUMS_KES[$tier] ?? 0;
                    if ($amount < $minimum) {
                        $validator->errors()->add(
                            'investment_amount',
                            "Minimum investment for {$tier} tier is KES " . number_format($minimum)
                        );
                    }
                } else {
                    $minimum = Investment::TIER_MINIMUMS[$tier] ?? 0;
                    if ($amount < $minimum) {
                        $validator->errors()->add(
                            'investment_amount',
                            "Minimum investment for {$tier} tier is {$minimum} USD."
                        );
                    }
                }
            }
        });
    }
}
