<?php

namespace App\Classes\Modules\Exports\Services;

use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Illuminate\Http\Request;
use App\Classes\Modules\Companies\Services\ListsCompanies;

class ExportsLeadsTransactions implements FromQuery, WithHeadings, WithHeadingRow, WithMapping, ShouldAutoSize
{
    use Exportable;

    private $request;

    public function __construct(Request $request, ListsCompanies $listsCompanies)
    {
        $this->request = $request;
        $this->listsCompanies = $listsCompanies;
    }

    public function headings(): array
    {
        return [
            'Agent Name',
            'User Name',
            'Contact Number',
            'Email',
            'Customer Code',
            'Register Date'
        ];
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function query()
    {
        $query = $this->listsCompanies->execute(
            [
                'business_type' => 2,
                'with_bookings' => true,
                'without_confirmed_payments' => true,
                'does_not_have_segments' => [5],
                'order_by_created_at_desc_with_limit' => true,
            ]
        );

        return $query->toQuery();
    }

    /**
     * @param $row
     * @return array
     */
    public function map($row): array
    {
        return [
            '',
            $row->name,
            strval($row->contacts->first()->phone),
            // $row->contacts->first()->email,
            $row->employees()->first() == null ? '' : $row->employees()->first()->email,
            $row->reference,
            $row->created_at->format('d-m-Y')
        ];
    }
}
