<?php

namespace Database\Seeders\Global;

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

class GoogleDisplayExportMapTemplateSeed extends Seeder
{
    /**
     * Seed the central "Google Display - Vehicles" export map template.
     *
     * Standalone (sales_channel = '') so it never suppresses a legacy
     * scheduled feed via ChecksForExportMap::hasActiveExportMap().
     *
     * @return void
     */
    public function run()
    {
        ExportMapTemplate::updateOrCreate(
            [
                'title' => 'Google Display - Vehicles',
            ],
            [
                'is_published' => true,
                'sales_channel' => '',
                'type' => 'vehicle',
                'columns' => [
                    'uuid',                 // ID
                    'empty',                // ID2
                    'title',                // Item title
                    'site_url',             // Final URL
                    'image_1',              // Image URL
                    'derivative',           // Item subtitle
                    'attention_grabber',    // Item description
                    'bodyStyle.name',       // Item category
                    'price',                // Price (+ EUR via rule)
                    'empty',                // Sale price
                    'custom1',              // Contextual keywords (built via mapping rules)
                    'custom2',              // Item address (built via mapping rules)
                    'empty',                // Tracking template
                    'empty',                // Custom parameter
                    'empty',                // Final mobile URL
                    'empty',                // Android app link
                    'empty',                // iOS app link
                    'empty',                // iOS app store ID
                    'empty',                // Formatted price
                    'empty',                // Formatted sale price
                ],
                'headers' => [
                    'ID',
                    'ID2',
                    'Item title',
                    'Final URL',
                    'Image URL',
                    'Item subtitle',
                    'Item description',
                    'Item category',
                    'Price',
                    'Sale price',
                    'Contextual keywords',
                    'Item address',
                    'Tracking template',
                    'Custom parameter',
                    'Final mobile URL',
                    'Android app link',
                    'iOS app link',
                    'iOS app store ID',
                    'Formatted price',
                    'Formatted sale price',
                ],
                'mapping_rules' => [
                    // Price requires an ISO 4217 code; Windsor is EUR (Republic of Ireland).
                    [
                        'when' => [
                            'field' => 'price',
                            'op' => 'is_not_empty',
                            'value' => null,
                        ],
                        'then' => [
                            [
                                'action' => 'append',
                                'target' => 'price',
                                'value' => ' EUR',
                            ],
                        ],
                    ],
                    // Item address (custom2): comma-join the dealership address parts, skipping empties.
                    [
                        'when' => ['field' => 'uuid', 'op' => 'is_not_empty', 'value' => null],
                        'then' => $this->mergeChain('custom2', ', ', [
                            'dealership.address1',
                            'dealership.address2',
                            'dealership.city',
                            'dealership.county',
                            'dealership.postcode',
                            'dealership.country',
                        ]),
                    ],
                    // Contextual keywords (custom1): semicolon-join make/model/body/condition, and build
                    // the "<make> <model>" token in a scratch slot (kw_make_model) for the next rule.
                    [
                        'when' => ['field' => 'uuid', 'op' => 'is_not_empty', 'value' => null],
                        'then' => [
                            ...$this->mergeChain('custom1', '; ', [
                                'make.name',
                                'model.name',
                                'bodyStyle.name',
                                'condition',
                            ]),
                            ...$this->mergeChain('kw_make_model', ' ', ['make.name', 'model.name']),
                        ],
                    ],
                    // Only append "for sale" and fold the token in when make/model produced content,
                    // preserving the original behaviour of omitting it entirely when both are empty.
                    [
                        'when' => ['field' => 'kw_make_model', 'op' => 'is_not_empty', 'value' => null],
                        'then' => [
                            ['action' => 'append', 'target' => 'kw_make_model', 'value' => ' for sale'],
                            ...$this->mergeChain('custom1', '; ', ['kw_make_model']),
                        ],
                    ],
                ],
            ]
        );
    }

    /**
     * Build a list of merge actions writing each source field into the same target,
     * keeping the seeder declarative without overly long inline rule arrays.
     *
     * @param array<int, string> $sources
     * @return array<int, array<string, string>>
     */
    private function mergeChain(string $target, string $separator, array $sources): array
    {
        return array_map(
            fn (string $source) => [
                'action' => 'merge',
                'target' => $target,
                'source' => $source,
                'separator' => $separator,
            ],
            $sources
        );
    }
}
