<?php

namespace Modules\Product\Exports;

use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithEvents;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use function view;

class ReportSalesExport implements FromView, WithEvents
{
    /**
     * @return \Illuminate\Support\Collection
     */

    protected $data;

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

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

    public function view(): \Illuminate\Contracts\View\View
    {
        return view('canteen.stocks.reports.report_sales', ['report' => $this->data]);
    }

    public function registerEvents(): array
    {
        return [
            \Maatwebsite\Excel\Events\AfterSheet::class => function ($event) {
                $sheet = $event->sheet->getDelegate();
                $sheet->setRightToLeft(true);

                // Get the highest row and column
                $highestRow = $sheet->getHighestRow();
                $highestColumn = $sheet->getHighestColumn();

                // Set default row height for all rows (in points)
                for ($row = 1; $row <= $highestRow; $row++) {
                    $sheet->getRowDimension($row)->setRowHeight(25); // Increased from default 15
                }

                // Set column widths (in character units)
                $columnWidths = [
                    'A' => 25,  // First column wider for product names
                    'B' => 15,
                    'C' => 15,
                ];

                foreach ($columnWidths as $column => $width) {
                    $sheet->getColumnDimension($column)->setWidth($width);
                }

                // Header style
                $sheet->getStyle('A1:'.$highestColumn.'2')->applyFromArray([
                    'font' => ['bold' => true, 'size' => 12], // Increased font size
                    'alignment' => [
                        'horizontal' => Alignment::HORIZONTAL_CENTER,
                        'vertical' => Alignment::VERTICAL_CENTER,
                        'wrapText' => true, // Enable text wrapping
                    ],
                    'fill' => [
                        'fillType' => 'solid',
                        'startColor' => ['rgb' => 'FDF2CC'],
                    ],
                    'borders' => [
                        'allBorders' => [
                            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                            'color' => ['rgb' => '2770c0'],
                        ],
                    ],
                ]);

                // Cell style for all data cells
                $sheet->getStyle('A3:'.$highestColumn.$highestRow)->applyFromArray([
                    'font' => ['size' => 11], // Increased font size
                    'alignment' => [
                        'vertical' => Alignment::VERTICAL_CENTER,
                        'wrapText' => true, // Enable text wrapping
                    ],
                ]);

                // Apply borders to the entire table
                $sheet->getStyle('A1:'.$highestColumn.$highestRow)->applyFromArray([
                    'borders' => [
                        'allBorders' => [
                            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                            'color' => ['rgb' => '2770c0'],
                        ],
                    ],
                ]);

                // Style for the total row if exists
                if ($highestRow > 2) {
                    $sheet->getRowDimension($highestRow)->setRowHeight(30); // Extra height for totals row
                    $sheet->getStyle('A'.$highestRow.':'.$highestColumn.$highestRow)->applyFromArray([
                        'font' => ['bold' => true, 'size' => 12],
                        'fill' => [
                            'fillType' => 'solid',
                            'startColor' => ['rgb' => 'E6E6E6'],
                        ],
                    ]);
                }
            }
        ];
    }
}
