<?php

namespace App\Exports;

use App\Models\Order;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class OrdersExportkoombiyo implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize, WithStyles
{
    protected $orders;

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

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

    public function headings(): array
    {
        return [
            'Waybill ID',
            'Order Number',
            'Receiver Name',
            'Delivery Address',
            'District Name',
            'City',
            'Receiver Phone',
            'COD',
            'Description',
            'Color',
            'Actual Value',
        ];
    }

   public function map($order): array
{
    $codAmount = 0;
    if ($order->payment_method == 'cod') {
        $codAmount = $order->total + $order->service_fee;
    }

   return [
    $order->tracking ?? '', // Waybill ID (tracking number)
    $order->order_id,
    $order->first_name,
    $order->address_1,
    $order->district,
    $order->city,
    $order->telephone,
    $order->payment_method == 'cod' ? $codAmount : '', // Show COD amount if payment is COD
    $order->productData->product_name,
    // Color code to text conversion
    (function($color) {
        $colorMap = [
            '#A78B8D' => 'Marblepink',
            '#61A8F8' => 'Babyblue',
            '#C55C82' => 'Pink',
            '#FF0000' => 'Red',
            '#CAD3D0' => 'Clear',
            '#BA9978' => 'Beige',
            '#88DB86' => 'Applegreen',
            '#DEE4EE' => 'Marblewhite',
            '#BDB8EE' => 'Marbleviolet',
            '#E9CBB9' => 'Marbleorange',
            '#FEF250' => 'Lemonyellow',
            '#f4c2c2' => 'Babypink',
            '#D2C77D' => 'Metallicgold',
            '#DAD9C6' => 'Buttermilk',
            '#E9C500' => 'Yellow',
            '#D3CBC4' => 'Graybeige',
            '#0F2D8D' => 'Blue',
            '#005E3A' => 'Green',
            '#D6F2E6' => 'Marblegreen',
            '#BF8BFF' => 'Violet',
            '#6B4B36' => 'Brown',
            '#C0C0C0' => 'Silver',
            '#808080' => 'Grey',
            '#6CD2FA' => 'Skyblue',
            '#D09305' => 'Orange',
            '#13598B' => 'Metallicblue',
            '#9582BB' => 'Metalicviolet',
            '#A286F4' => 'Purple',
            '#62ADE6' => 'Marbleblue',
            '#80355C' => 'Marblered',
            '#2FCBD4' => 'Avocadogreen',
            '#FFA07A' => 'Lightorange',
            '#DDD3D1' => 'Marblebeige',
            '#F4C430' => 'Saffron',
            '#000000' => 'Black',
            '#D4AF37' => 'Marblegold',
            '#FFFFFF' => 'Purewhite',
            '#B4B4B8' => 'Metallicsilver',
            '#58C3C2' => 'Marblegreen',
            '#006A4E' => 'Bottlegreen',
            '#6F4E37' => 'Coffee',
            '#FFFDD0' => 'Cream',
            '#A9A9A9' => 'Darkgrey',
            '#C40233' => 'Metalicred',
                   '#3D7764' => 'Metalicgreen',
            '#F8B700' => 'Metalicyellow'
        ];
        return $colorMap[strtoupper($color)] ?? $color;
    })($order->color),
    $order->total ?? 0   // Use COD amount for Actual Value if COD, else 0
];
}



    public function styles(Worksheet $sheet)
    {
        return [
            // Style the first row as bold text
            1 => [
                'font' => ['bold' => true],
                'fill' => [
                    'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
                    'color' => ['argb' => 'FFD9D9D9']
                ]
            ],
        ];
    }
}
