<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use App\Helpers\ReportHelper;

class ProfitLossExport implements FromCollection, WithHeadings, WithTitle, WithStyles
{
    protected $admin_id;
    protected $start_date;
    protected $end_date;
    
    public function __construct($admin_id, $start_date = null, $end_date = null)
    {
        $this->admin_id = $admin_id;
        $this->start_date = $start_date ?: date('Y-m-01');
        $this->end_date = $end_date ?: date('Y-m-t');
    }
    
    public function collection()
    {
        // Get P&L data from your ReportHelper
        $data = [
            'sales' => ReportHelper::sales($this->start_date, $this->end_date, $this->admin_id),
            'purchase' => ReportHelper::purchase($this->start_date, $this->end_date, $this->admin_id),
            'expenses' => ReportHelper::expenses($this->start_date, $this->end_date, $this->admin_id),
            'sales_returns' => ReportHelper::salesReturn($this->start_date, $this->end_date, $this->admin_id),
            'purchase_returns' => ReportHelper::purchaseReturn($this->start_date, $this->end_date, $this->admin_id),
        ];
        
        // Calculate totals
        $gross_profit = $data['sales']
                      - $data['purchase']
                      + $data['purchase_returns']
                      - $data['sales_returns'];
                      
        $net_profit = $gross_profit - $data['expenses'];
        
        // Build the P&L structure
        $rows = collect([
            // Income Section
            ['INCOME', '', ''],
            ['Sales', $data['sales'], ''],
            ['Less: Sales Returns', $data['sales_returns'], ''],
            ['Total Income', $data['sales'] - $data['sales_returns'], ''],
            
            // COGS Section
            ['COST OF GOODS SOLD', '', ''],
            ['Purchases', $data['purchase'], ''],
            ['Less: Purchase Returns', $data['purchase_returns'], ''],
            ['Total COGS', $data['purchase'] - $data['purchase_returns'], ''],
            
            // Gross Profit
            ['GROSS PROFIT', $gross_profit, ''],
            
            // Expenses
            ['EXPENSES', '', ''],
        ]);
        
        
        $rows->push(['Total Expenses', $data['expenses'], '']);
        $rows->push(['NET PROFIT', $net_profit, '']);
        
        return $rows;
    }
    
    public function headings(): array
    {
        return [
            'Profit & Loss Statement',
            'Amount (' . config('app.currency') . ')',
            'Notes'
        ];
    }
    
    public function title(): string
    {
        return 'Profit and Loss ' . $this->start_date . ' to ' . $this->end_date;
    }
    
    public function styles(Worksheet $sheet)
    {
        return [
            // Style the first row as bold text
            1 => ['font' => ['bold' => true]],
            
            // Style gross profit and net profit rows
            'A10' => ['font' => ['bold' => true]],
            'A' . (count($this->collection()) - 1) => ['font' => ['bold' => true]],
        ];
    }
}