<?php

namespace App\Exports;

use App\SalesChannelType;
use App\Traits\UsesSalesChannelRules;
use Illuminate\Support\Collection;
use Mtc\ContentManager\Models\MediaUse;
use Mtc\MercuryDataModels\Vehicle;
use Spatie\ArrayToXml\ArrayToXml;

class OutAndAboutExport
{
    use UsesSalesChannelRules;

    private const VEHICLE_TYPE_CAMPERVAN = 'Campervan';
    private const VEHICLE_TYPE_CARAVAN = 'Caravan';
    private const VEHICLE_TYPE_MOTORHOME = 'Motorhome';

    public function getExportData(): string
    {
        return ArrayToXml::convert(
            ['Product' => $this->getExportVehicles()->toArray()],
            'Products'
        );
    }

    private function getExportVehicles(): Collection
    {
        return Vehicle::query()
                ->with([
                    'mediaUses.media',
                    'make',
                    'model',
                    'transmission',
                    'fuelType',
                    'bodyStyle',
                ])
                ->exportable()
                ->when(
                    $this->hasSalesChannelRules(SalesChannelType::OUT_AND_ABOUT),
                    fn($query) => $this->applyQueryConditionsForChannel($query, SalesChannelType::OUT_AND_ABOUT)
                )
                ->get()
                ->map(function (Vehicle $vehicle) {
                    return collect([
                        'StockNumber' => $vehicle->uuid,
                        'Type' => $this->getType($vehicle->type),
                        'Manufacturer' => $vehicle->make?->name,
                        'Range' => $vehicle->getCustom('range') ?? '',
                        'Model' => $vehicle->model?->name,
                        'RegistrationYear' => $vehicle->first_registration_date?->format('Y'),
                        'Mileage' => $vehicle->odometer_mi,
                        'Price' => $vehicle->price,
                        'Condition' => empty($vehicle->is_new) ? 'Used' : 'New',
                        'Width' => $vehicle->getCustom('width') ?? '',
                        'Height' => $vehicle->getCustom('height') ?? '',
                        'Length' => $vehicle->getCustom('length') ?? '',
                        'Berths' => $vehicle->getCustom('berths') ?? '',
                        'SteeringPosition' => $this->getSteeringPosition($vehicle->getCustom('driver-side') ?? ''),
                        'Fuel' => $vehicle->fuelType?->name,
                        'Gears' => $vehicle->transmission?->name,
                        'Images' => $vehicle->mediaUses
                            ->filter(fn($mediaUse) => $mediaUse->media)
                            ->map(fn(MediaUse $mediaUse) => $mediaUse->getUrl('large'))
                            ->implode(';'),
                    ]);
                });
    }

    private function getSteeringPosition(string $steering_position): string
    {
        return stripos($steering_position, 'left') === false
            ? 'Right'
            : 'Left';
    }

    private function getType(?string $type): string
    {
        if (stripos($type, 'camper') !== false) {
            return self::VEHICLE_TYPE_CAMPERVAN;
        }

        if (stripos($type, 'caravan') !== false) {
            return self::VEHICLE_TYPE_CARAVAN;
        }

        return self::VEHICLE_TYPE_MOTORHOME;
    }
}
