<?php

namespace App\Exports;

use Illuminate\Support\Enumerable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Mtc\MercuryDataModels\Dealership;

class AaCarsDealerExport implements FromCollection, WithHeadings
{
    public function collection(): Enumerable
    {
        return Dealership::query()
            ->has('vehicles')
            ->get()
            ->map(fn (Dealership $dealership) => [
                'feed_id' => $dealership->data['aa-cars-dealer-id'] ?? null,
                'dealername' => $dealership->name,
                'address' => $dealership->address1 . ' ' . $dealership->address2 . ' ' . $dealership->city,
                'postcode' => $dealership->postcode,
                'phone_number' => $dealership->contact_no,
                'email' => $dealership->email,
            ]);
    }

    /**
     * Heading row
     *
     * @return string[]
     */
    public function headings(): array
    {
        return [
            'feed_id',
            'dealername',
            'address',
            'postcode',
            'phone_number',
            'email',
        ];
    }
}
