<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class RecyclingRequestsExport implements FromCollection, WithHeadings, WithMapping, WithStyles, WithColumnWidths
{
    protected $requests;

    public function __construct($requests)
    {
        $this->requests = $requests;
    }

    public function collection()
    {
        return $this->requests;
    }

    public function headings(): array
    {
        return [
            'Request ID',
            'Date Submitted',
            'User Name',
            'User Email',
            'Product Type',
            'Product Name',
            'Product Color',
            'Quantity',
            'Weight',
            'UOM',
            'Total Points',
            'Status',
            'User Note',
            'Admin Note'
        ];
    }

    public function map($item): array
    {
        $productName = $item->product_name ?? ($item->productcolor->product->product_name ?? 'N/A');
        $colorName = $item->product_name ? 'N/A' : ($item->productcolor->color_name ?? 'N/A');
        $productType = is_null($item->product_color_id) ? 'Typed' : 'Catalog';

        return [
            $item->id,
            $item->created_at ? $item->created_at->toDateTimeString() : 'N/A',
            $item->user->name ?? 'N/A',
            $item->user->email ?? 'N/A',
            $productType,
            $productName,
            $colorName,
            $item->quantity,
            $item->weight ?? '',
            $item->uom ?? '',
            number_format($item->total_points, 2, '.', ''),
            ucfirst($item->status),
            $item->user_note ?? '',
            $item->admin_note ?? ''
        ];
    }

    public function columnWidths(): array
    {
        return [
            'A' => 12,  // Request ID
            'B' => 22,  // Date Submitted
            'C' => 25,  // User Name
            'D' => 30,  // User Email
            'E' => 18,  // Product Type
            'F' => 30,  // Product Name
            'G' => 20,  // Product Color
            'H' => 12,  // Quantity
            'I' => 15,  // Weight
            'J' => 10,  // UOM
            'K' => 15,  // Total Points
            'L' => 15,  // Status
            'M' => 30,  // User Note
            'N' => 30   // Admin Note
        ];
    }

    public function styles(Worksheet $sheet)
    {
        return [
            1 => [
                'font' => ['bold' => true],
                'fill' => [
                    'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
                    'color' => ['argb' => 'FFD9E1F2']
                ]
            ],
            'A1:N'.$sheet->getHighestRow() => [
                'borders' => [
                    'allBorders' => [
                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN
                    ]
                ]
            ]
        ];
    }
}
