<?php

namespace App\Http\Requests;

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

class StoreQuestionnaireRequest extends FormRequest
{
    protected function prepareForValidation(): void
    {
        $this->merge([
            'country_code' => filled($this->input('country_code'))
                ? strtoupper((string) $this->input('country_code'))
                : $this->input('country_code'),
            'salary_currency' => filled($this->input('salary_currency'))
                ? strtoupper((string) $this->input('salary_currency'))
                : $this->input('salary_currency'),
        ]);
    }

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return $this->user() !== null;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        $categories = [
            'tech',
            'design',
            'marketing',
            'sales',
            'writing',
            'support',
            'product',
            'operations',
            'virtual-assistant',
            'finance',
            'hr',
            'data',
            'ai',
            'general',
        ];
        $roles = [
            'Software Developer',
            'UI/UX Designer',
            'Digital Marketer',
            'Sales Representative',
            'Content Writer',
            'Customer Support Specialist',
            'Product Manager',
            'Operations Coordinator',
            'Virtual Assistant',
            'Finance Analyst',
            'HR Recruiter',
            'Data Analyst',
            'AI Specialist',
            'General Remote Assistant',
        ];
        $skills = [
            'Remote communication',
            'Project management',
            'Customer support',
            'Sales outreach',
            'SEO',
            'Content writing',
            'Figma',
            'Laravel',
            'React',
            'Excel',
            'Data analysis',
            'Recruiting',
            'Bookkeeping',
            'AI tools',
            'Operations',
            'CRM',
            'Email support',
            'Copywriting',
            'Product strategy',
            'Virtual assistance',
        ];
        $timezones = ['Asia/Kolkata', 'UTC', 'America/New_York', 'America/Los_Angeles', 'Europe/London', 'Europe/Berlin', 'Asia/Singapore', 'Australia/Sydney'];
        $countryCodes = ['IN', 'US', 'GB', 'CA', 'AU', 'DE', 'SG', 'AE'];

        return [
            'preferred_categories' => ['required', 'array', 'min:1', 'max:8'],
            'preferred_categories.*' => ['string', Rule::in($categories)],
            'preferred_roles' => ['required', 'array', 'min:1', 'max:12'],
            'preferred_roles.*' => ['string', 'max:120', Rule::in($roles)],
            'skills' => ['required', 'array', 'min:1', 'max:30'],
            'skills.*' => ['string', 'max:80', Rule::in($skills)],
            'experience_level' => ['required', 'string', Rule::in(['entry', 'junior', 'mid', 'senior', 'lead', 'manager', 'executive'])],
            'availability' => ['required', 'string', Rule::in(['immediate', 'two_weeks', 'one_month', 'flexible'])],
            'preferred_timezone' => ['required', 'string', Rule::in($timezones)],
            'country_code' => ['required', 'string', 'size:2', Rule::in($countryCodes)],
            'salary_currency' => ['required', 'string', 'size:3', Rule::in(['USD', 'INR', 'EUR', 'GBP'])],
            'salary_min' => ['required', 'integer', Rule::in([0, 1000, 2500, 5000, 8000])],
            'open_to_contract' => ['required', 'boolean'],
            'open_to_full_time' => ['required', 'boolean'],
            'answers' => ['required', 'array'],
        ];
    }
}
