<?php

namespace App\Exports;

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

class UserExport implements FromCollection, WithHeadings, WithMapping
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {

        $scope  = request()->query('scope');

        if ($scope) {
            $users = User::$scope();
        } else {
            $users = User::query();
        }

        $users = $users->searchable(['username', 'email', 'system_id'])
        ->whereNotIn('username',['admin'])->where('dummy_person',0)
            ->dateFilter()
            ->filter(['network_status'])
            ->orderBy('id', 'desc');

        return $users->get();
    }

    public function map($user): array
    {
        return [
            $user->system_id,
            $user->username,
            $user->email,
            $user->mobile,
            $user->country_code,
            showDateTime($user->created_at),
            $user->balance,
        ];
    }
    public function headings(): array
    {
        return [
            'User Id',
            'UserName',
            'Email Id',
            'Contact Number',
            'Country Code',
            'Joined At',
            'Balance',
        ];
    }
}
