<?php

namespace Database\Seeders\Global;

use App\LeadExports\LeadExportFieldType;
use App\Models\LeadExport;
use App\Models\LeadExportTemplate;
use Illuminate\Database\Seeder;

class DefaultLeadExportTemplateSeed extends Seeder
{
    /**
     * Seed a default, one-to-one CRM lead export template. Contact fields are exposed
     * as destinations the admin binds form questions to; the attached vehicle-of-interest
     * and the enquiry's marketing attribution (utm_*, gclid, landing page) are mapped
     * automatically onto the vehicle.* / marketing.* destinations by the delivery handler.
     */
    public function run(): void
    {
        LeadExportTemplate::updateOrCreate(
            [
                'title' => 'Default lead (1:1)',
            ],
            [
                'is_published' => true,
                'type' => 'lead',
                'format' => 'json',
                'mode' => LeadExport::MODE_MAPPED,
                'field_map' => $this->fieldMap(),
                'config' => [],
            ]
        );
    }

    /**
     * Outbound destination fields, each with a human label and a value type.
     *
     * @return array<int, array<string, string>>
     */
    private function fieldMap(): array
    {
        $string = LeadExportFieldType::STRING;

        $contact = [
            ['destination' => 'name', 'label' => 'Full name', 'type' => $string],
            ['destination' => 'first_name', 'label' => 'First name', 'type' => $string],
            ['destination' => 'last_name', 'label' => 'Last name', 'type' => $string],
            ['destination' => 'email', 'label' => 'Email address', 'type' => $string],
            ['destination' => 'phone', 'label' => 'Phone number', 'type' => $string],
            ['destination' => 'mobile', 'label' => 'Mobile number', 'type' => $string],
            ['destination' => 'postcode', 'label' => 'Postcode', 'type' => $string],
            ['destination' => 'message', 'label' => 'Message', 'type' => $string],
            ['destination' => 'enquiry_type', 'label' => 'Enquiry type', 'type' => $string],
            [
                'destination' => 'marketing_opt_in',
                'label' => 'Marketing opt-in',
                'type' => LeadExportFieldType::BOOLEAN,
            ],
        ];

        $vehicle = [
            ['destination' => 'vehicle.make', 'label' => 'Vehicle make', 'type' => $string],
            ['destination' => 'vehicle.model', 'label' => 'Vehicle model', 'type' => $string],
            ['destination' => 'vehicle.derivative', 'label' => 'Vehicle derivative', 'type' => $string],
            ['destination' => 'vehicle.registration', 'label' => 'Vehicle registration', 'type' => $string],
            ['destination' => 'vehicle.vin', 'label' => 'Vehicle VIN', 'type' => $string],
            ['destination' => 'vehicle.price', 'label' => 'Vehicle price', 'type' => LeadExportFieldType::FLOAT],
            ['destination' => 'vehicle.fuel_type', 'label' => 'Vehicle fuel type', 'type' => $string],
            ['destination' => 'vehicle.transmission', 'label' => 'Vehicle transmission', 'type' => $string],
            ['destination' => 'vehicle.year', 'label' => 'Vehicle year', 'type' => LeadExportFieldType::INTEGER],
        ];

        $marketing = [
            ['destination' => 'marketing.utm_source', 'label' => 'UTM source', 'type' => $string],
            ['destination' => 'marketing.utm_medium', 'label' => 'UTM medium', 'type' => $string],
            ['destination' => 'marketing.utm_campaign', 'label' => 'UTM campaign', 'type' => $string],
            ['destination' => 'marketing.utm_term', 'label' => 'UTM term', 'type' => $string],
            ['destination' => 'marketing.utm_content', 'label' => 'UTM content', 'type' => $string],
            ['destination' => 'marketing.gclid', 'label' => 'Google click ID (gclid)', 'type' => $string],
            ['destination' => 'marketing.landing_page', 'label' => 'Landing page URL', 'type' => $string],
        ];

        return array_merge($contact, $vehicle, $marketing);
    }
}
