<?php

namespace App\Exports;

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

class AdminRewardExport implements FromCollection,  WithHeadings, WithMapping
{
    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        return Reward::searchable(['user:username,system_id'])
            ->dateFilter('created_at')
            ->filter(['status', 'rank'])
            ->with(['user', 'transaction'])
            ->get();
    }

    public function map($reward): array
    {
        return [
            $reward->user->system_id,
            $reward->user->fullname,
            $reward->user->email,
            $reward->user->mobile,
            $reward->rank,
            $reward->prize,
            $reward->transaction ? $reward->transaction->amount : 'N.A',
            $reward->transaction ? $reward->transaction->charge : 'N.A',
            $reward->transaction ? $reward->transaction->trx : 'N.A',
            $reward->status_changed_at ? $reward->status_changed_at->format('d-M-Y') : 'N/A',
        ];
    }

    public function headings(): array
    {
        return [
            trans('User ID'),
            trans('Name'),
            trans('Email'),
            trans('Mobile'),
            trans('Level'),
            trans('Reward'),
            trans('Amount'),
            trans('Charge'),
            trans('TRX'),
            trans('Achieved at')
        ];
    }
}
