<?php

namespace App\Exports;

use App\Models\Inventory\InventoryLine;
use Maatwebsite\Excel\Concerns\FromQuery;
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 VarianceInventoryLinesExport implements FromQuery, WithHeadings, WithMapping, WithStyles, WithColumnWidths
{
    protected $inventorySessionWarehouseId;

    /**
     * Constructor to accept the session warehouse ID
     */
    public function __construct($inventorySessionWarehouseId)
    {
        $this->inventorySessionWarehouseId = $inventorySessionWarehouseId;
    }

    /**
     * Query to fetch inventory lines where total counts do not match expected quantities
     */
    public function query()
    {
        return InventoryLine::query()
            ->where('inventory_session_warehouse_id', $this->inventorySessionWarehouseId)
            ->with(['foundInZones', 'inventoryMaster'])
            // ->select('id', 'barcode', 'description', 'qty', 'cost')
            ->whereHas('counts', function ($query) {
                $query->havingRaw('SUM(counted_qty) != qty');
            });
    }

    /**
     * Excel Headings
     */
    public function headings(): array
    {
        return [
            'ID',
            'Barcode',
            'Description',
            'Codein',
            'Style',
            'Colour',
            'Size',
            'Expected Quantity',
            'Counted Quantity',
            'Variance',
            'Zones Found In',
        ];
    }

    /**
     * Map data for each row in the export
     */
    public function map($line): array
    {
        $zones = $line->foundInZones->pluck('number')->implode(', ');
        $inventoryMaster = $line->inventoryMaster;
        $description = $inventoryMaster ? $inventoryMaster->description : '';

        return [
            $line->id,
            $line->barcode,
            $description,
            $inventoryMaster ? $inventoryMaster->codein : '',
            $inventoryMaster ? $inventoryMaster->style : '',
            $inventoryMaster ? $inventoryMaster->colour : '',
            $inventoryMaster ? $inventoryMaster->size : '',
            $line->qty,
            $line->total_count, // Total counted quantity
            $line->qty - $line->total_count, // Variance
            $zones,
        ];
    }

    /**
     * Define column styles
     */
    public function styles(Worksheet $sheet)
    {
        return [
            // Style for the heading row
            1 => ['font' => ['bold' => true, 'size' => 12]],

            // Optional: Style for all rows
            // Can be applied dynamically based on specific rows if needed
            // 'A' => ['font' => ['color' => ['rgb' => 'FF0000']]],
        ];
    }

    /**
     * Set column widths
     */
    public function columnWidths(): array
    {
        return [
            'A' => 10, // ID
            'B' => 20, // Barcode
            'C' => 40, // Description
            'D' => 20, // Expected Quantity
            'E' => 20, // Counted Quantity
            'F' => 10, // Variance
            'G' => 20, // Zones Found In
        ];
    }
}
