<?php

namespace Database\Seeders\Global;

use App\Models\NotificationTemplate;
use Illuminate\Database\Seeder;

class DefaultVehicleNotificationTemplateSeed extends Seeder
{
    /**
     * Seed a default, one-to-one notification template that mirrors the current
     * vehicle database structure. Each incoming payload key maps directly onto
     * the matching vehicle column; images are accepted as a nested list of
     * either direct URL strings or objects exposing a url property.
     */
    public function run(): void
    {
        NotificationTemplate::updateOrCreate(
            [
                'title' => 'Default vehicle (1:1)',
            ],
            [
                'is_published' => true,
                'type' => 'vehicle',
                'field_map' => $this->fieldMap(),
                'validation' => $this->validation(),
                'config' => $this->config(),
            ]
        );
    }

    /**
     * Source key (incoming payload) to destination vehicle column, plus the
     * value type used by the mapping engine. Source equals destination here,
     * because the default mirrors the vehicle table one-to-one.
     *
     * @return array<int, array<string, string>>
     */
    private function fieldMap(): array
    {
        $strings = [
            'stock_provider', 'uuid', 'vin', 'cap_id', 'cap_code',
            'type', 'title', 'derivative', 'registration_number', 'colour', 'trim',
            'description', 'attention_grabber', 'monthly_cost_type', 'main_video_url',
            'exterior_video_url', 'interior_video_url',
        ];

        $integers = [
            'door_count', 'seats', 'manufacture_year', 'odometer_km', 'odometer_mi',
            'engine_size_cc', 'previous_owner_count',
        ];

        $decimals = [
            'price', 'original_price', 'previous_price', 'monthly_price', 'rrp_price',
            'deposit', 'admin_fee', 'co2', 'mpg',
        ];

        $booleans = [
            'is_published', 'is_new', 'is_sold', 'is_reserved', 'is_demo',
            'is_vat_applicable', 'is_vat_qualifying', 'featured',
        ];

        $dates = [
            'first_registration_date', 'published_at', 'sold_at', 'reserved_at',
            'available_date', 'stock_arrival_date',
        ];

        // Taxonomy source (name) => destination id column, resolved via the taxonomy
        // mapping system. Model resolution is scoped to the resolved make.
        $taxonomies = [
            'make' => 'make_id',
            'model' => 'model_id',
            'fuel_type' => 'fuel_type_id',
            'body_style' => 'body_style_id',
            'transmission' => 'transmission_id',
            'drivetrain' => 'drivetrain_id',
        ];

        $map = [];
        foreach ($strings as $field) {
            $map[] = ['source' => $field, 'destination' => $field, 'type' => 'string'];
        }
        foreach ($integers as $field) {
            $map[] = ['source' => $field, 'destination' => $field, 'type' => 'integer'];
        }
        foreach ($decimals as $field) {
            $map[] = ['source' => $field, 'destination' => $field, 'type' => 'float'];
        }
        foreach ($booleans as $field) {
            $map[] = ['source' => $field, 'destination' => $field, 'type' => 'boolean'];
        }
        foreach ($dates as $field) {
            $map[] = ['source' => $field, 'destination' => $field, 'type' => 'date'];
        }
        foreach ($taxonomies as $source => $destination) {
            $map[] = ['source' => $source, 'destination' => $destination, 'type' => 'taxonomy', 'taxonomy' => $source];
        }

        // Dealership: matched by stock location or name.
        $map[] = ['source' => 'dealership', 'destination' => 'dealership_id', 'type' => 'dealership'];

        // Nested lists.
        $map[] = ['source' => 'images', 'destination' => 'images', 'type' => 'image_list'];
        $map[] = ['source' => 'equipment', 'destination' => 'equipment', 'type' => 'equipment'];
        $map[] = ['source' => 'technical_data', 'destination' => 'technical_data', 'type' => 'technical_data'];

        return $map;
    }

    /**
     * Validation rules keyed by destination column. uuid is the external stock
     * identifier used to dedupe/upsert, and type is required to create a vehicle.
     *
     * @return array<string, array<int, string>>
     */
    private function validation(): array
    {
        return [
            'uuid' => ['required', 'string'],
            'type' => ['required', 'string'],
        ];
    }

    /**
     * Global options for the template.
     *
     * @return array<string, mixed>
     */
    private function config(): array
    {
        return [
            // How to read the nested image list: items may be plain URL strings,
            // or objects where this property holds the URL.
            'images' => [
                'destination' => 'images',
                'url_key' => 'url',
            ],
        ];
    }
}
