<?php

namespace App\Http\Requests\Concerns;

use App\LeadExports\LeadExportFieldType;
use App\Models\LeadExport;
use Illuminate\Validation\Rule;

/**
 * Shared validation rules for the field_map / config structure used by both
 * LeadExportTemplate and LeadExport requests. In mapped mode each field_map entry
 * declares an outbound destination (dot-notation JSON path), an optional label and
 * an optional value type; the field list is required only when the mode is mapped.
 */
trait ValidatesLeadExportStructure
{
    /**
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    protected function structureRules(): array
    {
        $requiredWhenMapped = Rule::requiredIf(
            fn () => $this->input('mode') === LeadExport::MODE_MAPPED
        );

        return [
            'mode' => ['required', 'string', Rule::in(LeadExport::MODES)],
            'field_map' => [$requiredWhenMapped, 'array'],
            'field_map.*.destination' => ['required', 'string', 'max:255'],
            'field_map.*.label' => ['nullable', 'string', 'max:255'],
            'field_map.*.type' => ['nullable', 'string', Rule::in(LeadExportFieldType::TYPES)],
            'config' => ['nullable', 'array'],
        ];
    }
}
