<?php

namespace Modules\Product\Exports;

use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use Modules\Product\Enums\ProductStockStatusEnum;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use function view;

class StockLedgerExport implements FromView, WithEvents
{
    protected $data;

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

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

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function(AfterSheet $event) {
                $sheet = $event->sheet->getDelegate();

                // Set RTL direction
                $sheet->setRightToLeft(true);

                // Get dimensions
                $highestRow = $sheet->getHighestRow();
                $highestColumn = $sheet->getHighestColumn();

                // Set larger cell sizes
                $sheet->getDefaultRowDimension()->setRowHeight(25); // Increased row height

                // Set column widths (wider columns)
                $sheet->getColumnDimension('A')->setWidth(18); // التاريخ
                $sheet->getColumnDimension('B')->setWidth(25); // الصنف
                $sheet->getColumnDimension('C')->setWidth(22); // الكمية المضافة
                $sheet->getColumnDimension('D')->setWidth(22); // الكمية المنصرفة
                $sheet->getColumnDimension('E')->setWidth(18); // الرصيد
                $sheet->getColumnDimension('F')->setWidth(25); // رقم المستند
                $sheet->getColumnDimension('G')->setWidth(20); // New column

                // Header style with FDF2CC background
                $sheet->getStyle('A1:G1')->applyFromArray([
                    'font' => [
                        'bold' => true,
                        'size' => 12,
                    ],
                    'alignment' => [
                        'horizontal' => Alignment::HORIZONTAL_CENTER,
                        'vertical' => Alignment::VERTICAL_CENTER,
                        'wrapText' => true,
                    ],
                    'fill' => [
                        'fillType' => Fill::FILL_SOLID,
                        'startColor' => ['rgb' => 'FDF2CC'],
                    ],
                    'borders' => [
                        'allBorders' => [
                            'borderStyle' => Border::BORDER_MEDIUM,
                            'color' => ['rgb' => '2770c0'],
                        ],
                    ],
                ]);

                // Apply borders to all cells in the table
                $sheet->getStyle('A1:G'.$highestRow)->applyFromArray([
                    'borders' => [
                        'allBorders' => [
                            'borderStyle' => Border::BORDER_THIN,
                            'color' => ['rgb' => '2770c0'],
                        ],
                    ],
                ]);

                // Data cells style
                $sheet->getStyle('A2:G'.$highestRow)->applyFromArray([
                    'alignment' => [
                        'horizontal' => Alignment::HORIZONTAL_RIGHT,
                        'vertical' => Alignment::VERTICAL_CENTER,
                    ],
                    'font' => [
                        'size' => 11,
                    ]
                ]);

                // Make header row taller
                $sheet->getRowDimension(1)->setRowHeight(30);
            }
        ];
    }
}
