<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;

class StoreAddressRequest extends FormRequest
{
    public function authorize(): bool
    {
        return (bool) $this->user();
    }

    public function rules(): array
    {
        return [
            'name' => [
                'required',
                'string',
                function ($attribute, $value, $fail) {
                    if (mb_strlen((string) $value) > 255) {
                        $fail('The name field must not exceed 255 characters.');
                    }
                },
            ],
            'mobile' => ['required', 'string', 'regex:/^\+91[6-9]\d{9}$/'],
            'country_id' => ['required', 'integer', Rule::exists('countries', 'id')->where('status', 1)],
            'state_id' => [
                'required',
                'integer',
                Rule::exists('states', 'id')
                    ->where('status', 1)
                    ->where(fn ($q) => $q->where('country_id', $this->input('country_id'))),
            ],
            'city_id' => [
                'required',
                'integer',
                Rule::exists('cities', 'id')
                    ->where('status', 1)
                    ->where(fn ($q) => $q->where('state_id', $this->input('state_id'))),
            ],
            'line1' => [
                'required',
                'string',
                function ($attribute, $value, $fail) {
                    if (mb_strlen((string) $value) > 255) {
                        $fail('The address line 1 field must not exceed 255 characters.');
                    }
                },
            ],
            'line2' => [
                'nullable',
                'string',
                function ($attribute, $value, $fail) {
                    if ($value !== null && mb_strlen((string) $value) > 255) {
                        $fail('The address line 2 field must not exceed 255 characters.');
                    }
                },
            ],
            'city' => [
                'nullable',
                'string',
                function ($attribute, $value, $fail) {
                    if ($value !== null && mb_strlen((string) $value) > 100) {
                        $fail('The city field must not exceed 100 characters.');
                    }
                },
            ],
            'state' => [
                'nullable',
                'string',
                function ($attribute, $value, $fail) {
                    if ($value !== null && mb_strlen((string) $value) > 100) {
                        $fail('The state field must not exceed 100 characters.');
                    }
                },
            ],
            'pin' => 'required|string|regex:/^\d{6}$/',
            'is_default' => 'nullable|boolean',
        ];
    }

    protected function prepareForValidation(): void
    {
        if ($this->has('mobile')) {
            $mobile = preg_replace('/\s+/', '', (string) $this->input('mobile'));
            if (!str_starts_with($mobile, '+91')) {
                $mobile = '+91' . ltrim($mobile, '+');
            }
            $this->merge([
                'mobile' => $mobile,
            ]);
        }
    }
}
