<?php

namespace App\Classes\Modules\Exports\Services;

use App\Classes\ValueObjects\Constants\BusinessType;
use App\Classes\ValueObjects\Constants\CompanyType;
use App\Models\Company;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMapping;

class ExportsCustomers implements FromQuery, WithHeadingRow, WithMapping
{

    use Exportable;

    public function headings(): array
    {
        return [
            'Marking',
            'Name',
            'Business type',
            'Number of bookings',
            'Bookings total',
            'Registration Date',
            'System Registration'
        ];
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function query()
    {
        return Company::where('business_type', '=', 2);
    }

    /**
     * @param Company $company
     *
     * @return array
     */
    public function map($company): array
    {

        return [
            $company->reference,
            $company->name,
            $company->employees()->first() ? $company->employees()->first()->name : '',
            $company->contacts()->first() ? $company->contacts()->first()->phone : '',
            $company->type === CompanyType::COMPANY_BUSINESS ? 'Company' : 'individual',
            count($company->bookings),
            $company->bookings()->sum('fix_amount'),
            \PhpOffice\PhpSpreadsheet\Shared\Date::dateTimeToExcel($company->created_at),
            count($company->segments()->where('segment_id', '=', 5)->get()) ? 'Exchange 1.0' : 'Exchange 2.0'
        ];
    }
}