<?php

namespace App\Exports;

use App\Models\Transaction;
use Carbon\Carbon;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class TransactionsExport implements FromCollection, WithHeadings, WithMapping
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return Transaction::where('user_id', auth()->id())
            ->searchable(['trx', 'user:username,system_id'])
            ->filter([ 'remark'])
            ->dateFilter()
            ->orderBy('id', 'desc')
            ->get();
    }

    public function map($transaction): array
    {
        return [
            showDateTime($transaction->created_at),
            $transaction->trx,
            $transaction->amount,
            $transaction->charge,
            $transaction->payment_mode,
            $transaction->details,
        ];
    }

    public function headings(): array
    {
        return [
            'Date',
            'TRX',
            'Amount',
            'Charge',
            'Mode of payment',
            'Detail',
        ];
    }
}
