<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class MouvementsExport implements FromCollection, WithHeadings
{
    /**
     * @param \Illuminate\Support\Collection $mouvements
     */
    public function __construct(
        protected \Illuminate\Support\Collection $mouvements
    ) {
    }

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        return $this->mouvements->map(function ($m) {
            return [
                $m->reference ?? '-',
                $m->created_at?->format('d/m/Y') ?? '-',
                $m->created_at?->format('H:i') ?? '-',
                $m->type ?? '-',
                $m->magasin?->nom ?? '-',
                $m->produit?->designation ?? '-',
                $m->quantite,
            ];
        });
    }

    /**
     * @return array
     */
    public function headings(): array
    {
        return ['Référence', 'Date', 'Heure', 'Type', 'Magasin', 'Produit', 'Quantité'];
    }
}
