<?php

namespace App\Exports;

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

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

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        return $this->stocks->map(function ($s) {
            $statut = $s->statut;
            $statutLabel = match ($statut) {
                'ok' => 'OK',
                'alerte' => 'Alerte',
                default => 'Critique',
            };

            return [
                $s->magasin?->nom ?? '-',
                $s->produit?->designation ?? '-',
                $s->produit?->reference ?? '-',
                number_format($s->quantite ?? 0, 0, ',', ' '),
                $s->seuil_alerte ?? 0,
                $statutLabel,
            ];
        });
    }

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