<?php

namespace App\Exports;

use App\Models\Deposit;
use App\Services\ModalIndexQuey;
use Illuminate\Contracts\Support\Responsable;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Excel;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class BuyCoinExport implements FromQuery, Responsable, WithColumnFormatting, WithHeadings, WithMapping
{
    use Exportable;

    /**
     * It's required to define the fileName within
     * the export class when making use of Responsable.
     */
    private $fileName = 'buy_coins.xlsx';

    /**
     * Optional Writer Type
     */
    private $writerType = Excel::XLSX;

    /**
     * Optional headers
     */
    private $headers = [
        'Content-Type' => 'text/csv',
    ];

    public function __construct(
        public ?array $filters
    ) {}

    public function query()
    {
        // Define the query to fetch the users
        $query = Deposit::query()->with(['user:id,first_name,last_name,username,email']);

        return ModalIndexQuey::globalQuery($query);
    }

    /**
     * @param  Deposit  $deposit
     */
    public function map($deposit): array
    {
        return [
            $deposit->user->full_name.'<br>'.$deposit->user->username,
            $deposit->track,
            $deposit->user->full_name,
            amountWithSymbol(convertCurrency($deposit->amount + $deposit->charge, $deposit->currency), $deposit->currency),
            $deposit->coins,
            $deposit->status->label(),
            Date::dateTimeToExcel($deposit->created_at),
        ];
    }

    public function headings(): array
    {
        return [
            'User|ID',
            'Tracking Number',
            'Amount',
            'Coins',
            'Status',
            'Date',
        ];
    }

    public function columnFormats(): array
    {
        return [
            'G' => NumberFormat::FORMAT_DATE_DDMMYYYY,
        ];
    }
}
