<?php

namespace App\Exports;

use App\Models\Payment;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class DonationListExport implements FromCollection, WithHeadings, WithMapping
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return Payment::searchable(['payerUser:username,id,system_id,email', 'receiverUser:username,id,system_id,email'])
        ->filter(['status'])
        ->dateFilter()
        ->with('payerUser', 'receiverUser')
        ->whereDoesntHave('receiverUser', function ($query) {
            $query->where('username', 'admin');
        })
        ->orderBy('id', 'desc')
        ->get();

        // return Payment::searchable(['payerUser:username,id,system_id,email','receiverUser:username,id,system_id,email'])->filter(['status'])->dateFilter()->with('payerUser','receiverUser')->orderBy('id', 'desc')->get();
    }

    public function map($donationList): array
    {
        return [
            $donationList->payerUser->system_id,
            $donationList->payerUser->firstname . ' ' . $donationList->payerUser->lastname,
            $donationList->payerUser->username,
            $donationList->payerUser->email,
            $donationList->amount,
            $donationList->receiverUser->system_id,
            $donationList->receiverUser->firstname . ' ' . $donationList->receiverUser->lastname,
            $donationList->receiverUser->username,
            $donationList->receiverUser->email,
            $donationList->status == 1 ? 'Approved' : ($donationList->status == 2 ? 'Rejected' : ($donationList->status == 3 ? 'Pending' : 'Unknown')),
            $donationList->payment_mode,
            $donationList->utr_no,
            $donationList->trx,
            $donationList->created_at->format('d M Y'),
        ];
    }

    public function headings(): array
    {
        return [
            trans('Payer User ID'),
            trans('Payer Full Name'),
            trans('Payer User Name'),
            trans('Payer Email'),
            trans('Amount'),
            trans('Receiver User ID'),
            trans('Receiver Full Name'),
            trans('Receiver User Name'),
            trans('Receiver Email'),
            trans('Staus'),
            trans('Payment Method'),
            trans('UTR No'),
            trans('TRX'),
            trans('Created At'),
        ];
    }
}
