<?php

namespace App\Classes\Modules\Exports\Services;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\BusinessType;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Company;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class ExportsNullDebtors implements FromQuery, WithHeadings, WithHeadingRow, WithMapping, ShouldAutoSize
{

    use Exportable;


    public function headings(): array
    {
        return [
            'Code',
            'DebtorControlAcc',
            'ControlAccount',
            'CompanyName',
            'Desc2',
            'DebtorType',
            'DisplayTerm',
            'CurrencyCode',
            'RegisterNo',
            'Address1',
            'Address2',
            'Address3',
            'PostCode',
            'DeliverAddr1',
            'DeliverAddr2',
            'DeliverAddr3',
            'DeliverPostCode',
            'EmailAddress',
            'Attention',
            'Phone1',
            'Phone2',
            'Fax1'
        ];
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function query()
    {
        return Company::where(function($query){
            $query->whereNull('debtor')->orWhere('debtor', '');
        })->whereNotIn('id', [2207, 2248, 2029])->where('business_type', BusinessType::IMPORTER)->where('status', ApprovalStatus::APPROVED)->where(function($query){
            $query->whereHas('transactions', function($query){
                return $query->whereIn('type', [TransactionType::PAYMENT, TransactionType::TOP_UP])->whereIn('transactions.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED, ApprovalStatus::PENDING_VERIFICATION]);
            })->orWhereHas('wallets', function($query){
                return $query->whereHas('transactions');
            });
        });
    }

    /**
     * @param Company $company
     *
     * @return array
     */
    public function map($company): array
    {
        return [
            '<<New>>',
            '300-0000',
            '300-0000',
            $company->name.' (PURCHASE)',
            $company->reference,
            '',
            'PIA',
            'MYR',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            '',//EmailAddress
            '',
            '',
            '',
            ''
        ];
    }
}