<?php

namespace App\Exports;

use App\Models\Company;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class UserExport implements FromCollection, WithHeadings, WithMapping
{

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return Company::with('category', 'user')->get();
    }

    public function headings(): array
    {
        return [
            'User Name',
            'Company Name',
            'Email',
            'Category', 
            'Address Line 1',
            'Address Line 2',
            'Post Code',
            'Country',
            'Registerd Date',
        ];
    }

    public function map($company): array
    {
        return [
            $company->user?->name,
            $company->name,
            $company->user?->email,
            $company->type,
            $company->address,
            $company->city,
            $company->post_code,
            $company->country,
            $company->created_at->format('Y-m-d'),
        ];
    }
}
