<?php

namespace Database\Seeders\Global;

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

class FacebookExportMapTemplatesSeed extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->seedVehicleTemplate();
        $this->seedOfferTemplate();
    }

    private function seedVehicleTemplate(): void
    {
        ExportMapTemplate::updateOrCreate(
            [
                'title' => 'Facebook - Vehicles',
            ],
            [
                'is_published' => true,
                'sales_channel' => '',
                'type' => 'vehicle',
                'columns' => [
                    "uuid",
                    "title",
                    "description",
                    "site_url",
                    "make.name",
                    "model.name",
                    "manufacture_year",
                    "odometer_mi",
                    "custom1",
                    "image_1",
                    "image_2",
                    "image_3",
                    "image_4",
                    "image_5",
                    "image_6",
                    "image_7",
                    "image_8",
                    "image_9",
                    "image_10",
                    "image_11",
                    "image_12",
                    "image_13",
                    "image_14",
                    "image_15",
                    "image_16",
                    "image_17",
                    "image_18",
                    "image_19",
                    "image_20",
                    "bodyStyle.name",
                    "price",
                    "colour",
                    "custom2",
                    "transmission.name",
                    "vin",
                    "fuelType.name",
                    "trim",
                    "condition",
                    "custom3",
                    "available_date",
                    "dealership.name",
                    "dealership.contact_no",
                    "dealership.address1",
                    "dealership.city",
                    "dealership.county",
                    "dealership.country",
                    "dealership.postcode",
                    "drivetrain.name",
                    "dealership.lat",
                    "dealership.lng",
                    "dealership_id",
                    "site_url",
                    "monthly_price",
                    "uuid",
                    "empty",
                ],
                'headers' => [
                    "vehicle_id",
                    "title",
                    "description",
                    "url",
                    "make",
                    "model",
                    "year",
                    "mileage.value",
                    "mileage.unit",
                    "image[0].url",
                    "image[1].url",
                    "image[2].url",
                    "image[3].url",
                    "image[4].url",
                    "image[5].url",
                    "image[6].url",
                    "image[7].url",
                    "image[8].url",
                    "image[9].url",
                    "image[10].url",
                    "image[11].url",
                    "image[12].url",
                    "image[13].url",
                    "image[14].url",
                    "image[15].url",
                    "image[16].url",
                    "image[17].url",
                    "image[18].url",
                    "image[19].url",
                    "body_style",
                    "price",
                    "exterior_color",
                    "state_of_vehicle",
                    "transmission",
                    "vin",
                    "fuel_type",
                    "trim",
                    "condition",
                    "availability",
                    "date_first_on_lot",
                    "dealer_name",
                    "dealer_phone",
                    "address.addr1",
                    "address.city",
                    "address.region",
                    "address.country",
                    "address.postal_code",
                    "drivetrain",
                    "latitude",
                    "longitude",
                    "dealer_id",
                    "deep_link",
                    "monthly_price",
                    "stock_number",
                    "fb_page_id",
                ],
                'mapping_rules' => [
                    // mileage.unit - always MI
                    [
                        "when" => [
                            "field" => "uuid",
                            "op" => "is_not_empty",
                            "value" => null,
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "custom1",
                                "value" => "MI",
                            ],
                        ],
                    ],
                    // state_of_vehicle - New / Used
                    [
                        "when" => [
                            "field" => "is_new",
                            "op" => "eq",
                            "value" => "Y",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "custom2",
                                "value" => "New",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "is_new",
                            "op" => "eq",
                            "value" => "N",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "custom2",
                                "value" => "Used",
                            ],
                        ],
                    ],
                    // availability - from is_published
                    [
                        "when" => [
                            "field" => "is_published",
                            "op" => "eq",
                            "value" => "Y",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "custom3",
                                "value" => "available",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "is_published",
                            "op" => "eq",
                            "value" => "N",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "custom3",
                                "value" => "not_available",
                            ],
                        ],
                    ],
                    // body_style - map to Facebook enum values
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Hatchback",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "HATCHBACK",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Saloon",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "SEDAN",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Sedan",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "SEDAN",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Estate",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "WAGON",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "SUV",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "SUV",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Coupe",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "COUPE",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Convertible",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "CONVERTIBLE",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "MPV",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "MINIVAN",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Van",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "VAN",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Pickup",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "TRUCK",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Crossover",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "CROSSOVER",
                            ],
                        ],
                    ],
                    // fuel_type - map to Facebook enum values
                    [
                        "when" => [
                            "field" => "fuelType.name",
                            "op" => "eq",
                            "value" => "Petrol",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "fuelType.name",
                                "value" => "GASOLINE",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "fuelType.name",
                            "op" => "eq",
                            "value" => "Diesel",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "fuelType.name",
                                "value" => "DIESEL",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "fuelType.name",
                            "op" => "eq",
                            "value" => "Electric",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "fuelType.name",
                                "value" => "ELECTRIC",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "fuelType.name",
                            "op" => "eq",
                            "value" => "Hybrid",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "fuelType.name",
                                "value" => "HYBRID",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "fuelType.name",
                            "op" => "eq",
                            "value" => "Plug-in Hybrid",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "fuelType.name",
                                "value" => "HYBRID",
                            ],
                        ],
                    ],
                    // drivetrain - map to Facebook enum values
                    [
                        "when" => [
                            "field" => "drivetrain.name",
                            "op" => "eq",
                            "value" => "Front Wheel Drive",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "drivetrain.name",
                                "value" => "FWD",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "drivetrain.name",
                            "op" => "eq",
                            "value" => "Rear Wheel Drive",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "drivetrain.name",
                                "value" => "RWD",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "drivetrain.name",
                            "op" => "eq",
                            "value" => "All Wheel Drive",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "drivetrain.name",
                                "value" => "AWD",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "drivetrain.name",
                            "op" => "eq",
                            "value" => "Four Wheel Drive",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "drivetrain.name",
                                "value" => "4X4",
                            ],
                        ],
                    ],
                    // transmission - map to Facebook enum values (Automatic, Manual)
                    [
                        "when" => [
                            "field" => "transmission.name",
                            "op" => "eq",
                            "value" => "Semi-Automatic",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "transmission.name",
                                "value" => "Other",
                            ],
                        ],
                    ],
                    // price - append currency code (Facebook requires "price currency_code")
                    [
                        "when" => [
                            "field" => "price",
                            "op" => "is_not_empty",
                            "value" => null,
                        ],
                        "then" => [
                            [
                                "action" => "append",
                                "target" => "price",
                                "value" => " GBP",
                            ],
                        ],
                    ],
                ],
            ]
        );
    }

    private function seedOfferTemplate(): void
    {
        ExportMapTemplate::updateOrCreate(
            [
                'title' => 'Facebook - Offers',
            ],
            [
                'is_published' => true,
                'sales_channel' => '',
                'type' => 'offer',
                'columns' => [
                    "uuid",
                    "make.name",
                    "model.name",
                    "custom1",
                    "offerType.name",
                    "name",
                    "description",
                    "cta_button1_url",
                    "t_and_cs",
                    "image_1",
                    "image_2",
                    "image_3",
                    "image_4",
                    "image_5",
                    "image_6",
                    "image_7",
                    "image_8",
                    "image_9",
                    "image_10",
                    "image_11",
                    "image_12",
                    "image_13",
                    "image_14",
                    "image_15",
                    "image_16",
                    "image_17",
                    "image_18",
                    "image_19",
                    "image_20",
                    "bodyStyle.name",
                    "price",
                    "deposit",
                    "trim",
                    "full_price",
                    "transmission.name",
                    "fuelType.name",
                    "colour",
                    "drivetrain.name",
                    "published_at",
                    "unpublished_at",
                ],
                'headers' => [
                    "vehicle_offer_id",
                    "make",
                    "model",
                    "year",
                    "offer_type",
                    "title",
                    "offer_description",
                    "url",
                    "offer_disclaimer",
                    "image[0].url",
                    "image[1].url",
                    "image[2].url",
                    "image[3].url",
                    "image[4].url",
                    "image[5].url",
                    "image[6].url",
                    "image[7].url",
                    "image[8].url",
                    "image[9].url",
                    "image[10].url",
                    "image[11].url",
                    "image[12].url",
                    "image[13].url",
                    "image[14].url",
                    "image[15].url",
                    "image[16].url",
                    "image[17].url",
                    "image[18].url",
                    "image[19].url",
                    "body_style",
                    "amount_price",
                    "downpayment",
                    "trim",
                    "price",
                    "transmission",
                    "fuel_type",
                    "exterior_color",
                    "drivetrain",
                    "start_date",
                    "end_date",
                ],
                'mapping_rules' => [
                    // offer_type - map to Facebook enum values (lowercase)
                    [
                        "when" => [
                            "field" => "offerType.name",
                            "op" => "eq",
                            "value" => "Lease",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "offerType.name",
                                "value" => "lease",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "offerType.name",
                            "op" => "eq",
                            "value" => "Finance",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "offerType.name",
                                "value" => "finance",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "offerType.name",
                            "op" => "eq",
                            "value" => "Cash",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "offerType.name",
                                "value" => "cash",
                            ],
                        ],
                    ],
                    // body_style - map to Facebook enum values
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Hatchback",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "HATCHBACK",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Saloon",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "SEDAN",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Sedan",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "SEDAN",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Estate",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "ESTATE",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "SUV",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "SUV",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Coupe",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "COUPE",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Convertible",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "CONVERTIBLE",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "MPV",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "MINIVAN",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Van",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "VAN",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Pickup",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "TRUCK",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "bodyStyle.name",
                            "op" => "eq",
                            "value" => "Crossover",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "bodyStyle.name",
                                "value" => "CROSSOVER",
                            ],
                        ],
                    ],
                    // fuel_type - map to Facebook Automotive Model Ads enum values
                    [
                        "when" => [
                            "field" => "fuelType.name",
                            "op" => "eq",
                            "value" => "Petrol",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "fuelType.name",
                                "value" => "Petrol",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "fuelType.name",
                            "op" => "eq",
                            "value" => "Diesel",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "fuelType.name",
                                "value" => "Diesel",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "fuelType.name",
                            "op" => "eq",
                            "value" => "Electric",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "fuelType.name",
                                "value" => "Electric",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "fuelType.name",
                            "op" => "eq",
                            "value" => "Hybrid",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "fuelType.name",
                                "value" => "Hybrid",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "fuelType.name",
                            "op" => "eq",
                            "value" => "Plug-in Hybrid",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "fuelType.name",
                                "value" => "PlugIn_Hybrid",
                            ],
                        ],
                    ],
                    // drivetrain - map to Facebook enum values
                    [
                        "when" => [
                            "field" => "drivetrain.name",
                            "op" => "eq",
                            "value" => "Front Wheel Drive",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "drivetrain.name",
                                "value" => "FWD",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "drivetrain.name",
                            "op" => "eq",
                            "value" => "Rear Wheel Drive",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "drivetrain.name",
                                "value" => "RWD",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "drivetrain.name",
                            "op" => "eq",
                            "value" => "All Wheel Drive",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "drivetrain.name",
                                "value" => "AWD",
                            ],
                        ],
                    ],
                    [
                        "when" => [
                            "field" => "drivetrain.name",
                            "op" => "eq",
                            "value" => "Four Wheel Drive",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "drivetrain.name",
                                "value" => "4X4",
                            ],
                        ],
                    ],
                    // transmission - map to Facebook enum values
                    [
                        "when" => [
                            "field" => "transmission.name",
                            "op" => "eq",
                            "value" => "Semi-Automatic",
                        ],
                        "then" => [
                            [
                                "action" => "set",
                                "target" => "transmission.name",
                                "value" => "Other",
                            ],
                        ],
                    ],
                    // amount_price - append currency code
                    [
                        "when" => [
                            "field" => "price",
                            "op" => "is_not_empty",
                            "value" => null,
                        ],
                        "then" => [
                            [
                                "action" => "append",
                                "target" => "price",
                                "value" => " GBP",
                            ],
                        ],
                    ],
                    // price (MSRP) - append currency code
                    [
                        "when" => [
                            "field" => "full_price",
                            "op" => "is_not_empty",
                            "value" => null,
                        ],
                        "then" => [
                            [
                                "action" => "append",
                                "target" => "full_price",
                                "value" => " GBP",
                            ],
                        ],
                    ],
                    // downpayment - append currency code
                    [
                        "when" => [
                            "field" => "deposit",
                            "op" => "is_not_empty",
                            "value" => null,
                        ],
                        "then" => [
                            [
                                "action" => "append",
                                "target" => "deposit",
                                "value" => " GBP",
                            ],
                        ],
                    ],
                ],
            ]
        );
    }
}
