<?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 ProductExport implements FromCollection, WithHeadings, WithMapping
{
    protected $companyId;
    public function __construct($companyId)
    {
        $this->companyId = $companyId;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return Product::with('category', 'productImages')
        ->where('company_id', $this->companyId)
        ->where('status', 1)
        ->get();
    }

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

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

    // public function drawings()
    // {
    //     $drawings = [];
    //     $products = $this->collection();
    //     $rowIndex = 2; // Start from row 2 as row 1 has the headings

    //     foreach ($products as $index => $product) {
    //         if(!empty($product->productImages[0]) && file_exists($product->productImages[0]->file)){
    //             $drawing = new Drawing();
    //             $drawing->setName('Product Image');
    //             $drawing->setDescription('Product Image');
    //             $drawing->setPath($product->productImages[0]->file); // Path to the image
    //             $drawing->setHeight(50); // Height of the image
    //             $drawing->setCoordinates('D' . ($index + 2)); // Set the column and row (starting after the header)
    //             $drawings[] = $drawing;
    //         }
            
    //     }
    //     return $drawings;
    // }
}
