<?php

namespace App\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class UsersExport implements FromCollection, WithHeadings, WithStyles, ShouldAutoSize
{
    public function __construct(private Collection $rows) {}

    public function collection(): Collection
    {
        return $this->rows->map(fn ($r) => [
            $r->username ?? '',
            $r->first_name ?? '',
            $r->last_name ?? '',
            $r->mobile ?? '',
            $r->email ?? '',
            $r->role_key ?? '',
            $r->status ?? '',
        ]);
    }

    public function headings(): array
    {
        return ['نام کاربری', 'نام', 'نام خانوادگی', 'موبایل', 'ایمیل', 'نقش', 'وضعیت'];
    }

    public function styles(Worksheet $sheet): array
    {
        return [
            1 => ['font' => ['bold' => true]],
        ];
    }
}
