<?php

namespace App\Exports;

use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class UsersExport implements FromCollection, WithHeadings
{
    /**
     * Headings for the export.
     */
    public function headings(): array
    {
        return ['Sr.', 'Name', 'Email', 'Picture', 'Status'];
    }

    /**
     * Collection of rows for Excel export.
     */
    public function collection()
    {
        $users = User::select('id', 'name', 'email', 'picture', 'status')
            ->where('id', '!=', 1)
            ->where('id', '!=', Auth::id())
            ->orderByDesc('id')
            ->get();

        // Add serial numbers
        $sr = 1;
        $data = $users->map(function ($u) use (&$sr) {
            return [
                $sr++,
                $u->name,
                $u->email,
                $u->picture ?? '',
                $u->status ?? '',
            ];
        });

        return $data;
    }
}
