<?php

namespace App\Exports;

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

class DebtFreeExport implements FromCollection, WithHeadings, WithMapping
{
    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        $users = User::inDebt()->where('debt_cleared_at', '!=', null)->searchable(['username', 'email', 'system_id'])
            ->dateFilter('debt_cleared_at')
            ->latest()->get();

        return $users;
    }

    public function map($user): array
    {
        return [
            $user->system_id,
            $user->username,
            $user->email,
            $user->mobile,
            $user->current_debt_value,
            showDateTime($user->debt_cleared_at),
        ];
    }
    public function headings(): array
    {
        return [
            'User ID',
            'UserName',
            'Email Id',
            'Contact Number',
            'Total Debt',
            'Debt Cleared At',
        ];
    }
}
