<?php

namespace App\Exports;

use App\Facades\Site;
use App\SalesChannelType;
use App\Traits\UsesSalesChannelRules;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Mtc\ContentManager\Models\MediaUse;
use Mtc\MercuryDataModels\Vehicle;

class VehiclesInVideoExport implements FromCollection, WithHeadings
{
    use UsesSalesChannelRules;

    public function collection()
    {
        return Vehicle::query()
            ->with([
                'mediaUses.media',
                'dealership.attributeValues',
                'make',
                'model',
                'transmission',
                'fuelType',
                'bodyStyle',
            ])
            ->when(
                $this->hasSalesChannelRules(SalesChannelType::VEHICLES_IN_VIDEO),
                fn ($query) => $this->applyQueryConditionsForChannel($query, SalesChannelType::VEHICLES_IN_VIDEO)
            )
            ->get()
            ->map(fn (Vehicle $vehicle) => [
                'garage_ref_no' => $vehicle->dealership?->additionalField('vehicles-in-video-garage-ref'),
                'registration' => $vehicle->registration_number,
                'make' => $vehicle->make?->name,
                'model' => $vehicle->model?->name,
                'price' => $vehicle->price,
                'vin' => $vehicle->vin,
                'colour' => $vehicle->colour,
                'fuel' => $vehicle->fuelType?->name,
                'mileage' => $vehicle->odometer_mi,
                'transmission' => $vehicle->transmission?->name,
                'doors' => $vehicle->door_count,
                'cylinders' => '',
                'date_of_registration' => $vehicle->first_registration_date?->format('Y-m-d'),
                'year_of_manufacture' => $vehicle->manufacture_year,
                'body_style' => $vehicle->bodyStyle?->name,
                'description' => $vehicle->description,
                'images' => $vehicle->mediaUses
                    ->filter(fn (MediaUse $mediaUse) => $mediaUse->media)
                    ->map(fn (MediaUse $mediaUse) => $mediaUse->getUrl('large'))
                    ->implode(','),
                'forward_action_button_1_url' => $this->forwardActionUrl($vehicle),
                'forward_action_button_2_url' => $this->forwardActionUrl($vehicle),
                'forward_action_button_3_url' => $this->forwardActionUrl($vehicle),
            ]);
    }

    public function headings(): array
    {
        return [
            'garage_ref_no',
            'registration',
            'make',
            'model',
            'price',
            'vin',
            'colour',
            'fuel',
            'mileage',
            'transmission',
            'doors',
            'cylinders',
            'date_of_registration',
            'year_of_manufacture',
            'body_style',
            'description',
            'images',
            'forward_action_button_1_url',
            'forward_action_button_2_url',
            'forward_action_button_3_url',
        ];
    }

    private function forwardActionUrl(Vehicle $vehicle): string
    {
        return Site::vehicleUrl($vehicle) ?? '';
    }
}
