<?php

namespace App\Exports;

use App\Models\Product;
use Maatwebsite\Excel\Concerns\FromCollection;
// use Maatwebsite\Excel\Concerns\WithDrawings;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
// use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

class AllProductExport implements FromCollection, WithHeadings, WithMapping
{
    public function __construct(){}

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return Product::with('company', 'category')->orderBy('id', 'desc')->get();;
    }

    public function headings(): array
    {
        return [
            'ID',
            'Name', 
            'Category', 
            'Company', 
            'Unit Type',
            'Size',
            'Packing',
            'Qunatity',
            'Price',
            'Stock Location',
            'Type',
            'Best Before',
            'Description',
            'Status'  
        ];
    }

    public function map($product): array
    {
        return [
            $product->id,
            $product->name,
            $product->category->name ?? 'N/A',
            $product->company->name ?? 'N/A',
            $product->unit_type ?? "",
            $product->unit_size ?? "",
            $product->unit_per_case ?? 1,
            $product->quantity ?? 1,
            $product->is_contact_company ? 'Contact for Price' : ($product->currency.$product->price),
            $product->stock_location,
            $product->type,
            $product->expiry_date ?? "N/A",
            $product->description ?? 'N/A',
            $product->status == 1 ? 'Enabled' : 'Disabled'
        ];
    }
}
