<?php

namespace App\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithTitle;

class ReportsExport implements FromCollection, WithTitle, WithHeadings, WithMapping
{
    use Exportable;

    protected $data;

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

    public function collection()
    {
        $exportRows = collect();

        // Add period information header
        $exportRows->push([
            'section' => 'PERIOD',
            'name' => __('Period'),
            'start_balance' => $this->data['period']['from_date_formatted'] . ' - ' . $this->data['period']['to_date_formatted'],
            'end_balance' => '',
            'difference' => '',
            'type' => 'header'
        ]);

        // Add empty row
        $exportRows->push([
            'section' => '',
            'name' => '',
            'start_balance' => '',
            'end_balance' => '',
            'difference' => '',
            'type' => 'separator'
        ]);

        // Add account balances section header
        $exportRows->push([
            'section' => 'ACCOUNTS',
            'name' => __('Account Balances'),
            'start_balance' => '',
            'end_balance' => '',
            'difference' => '',
            'type' => 'header'
        ]);

        // Add individual accounts
        foreach ($this->data['accounts'] as $account) {
            $exportRows->push([
                'section' => 'ACCOUNT',
                'name' => $account['name'],
                'start_balance' => $account['start_balance_formatted'],
                'end_balance' => $account['end_balance_formatted'],
                'difference' => $account['difference_formatted'],
                'type' => 'account'
            ]);
        }

        // Add totals row
        $exportRows->push([
            'section' => 'TOTAL',
            'name' => __('TOTAL'),
            'start_balance' => $this->data['totals']['start_balance_formatted'],
            'end_balance' => $this->data['totals']['end_balance_formatted'],
            'difference' => $this->data['totals']['difference_formatted'],
            'type' => 'total'
        ]);

        // Add empty row
        $exportRows->push([
            'section' => '',
            'name' => '',
            'start_balance' => '',
            'end_balance' => '',
            'difference' => '',
            'type' => 'separator'
        ]);

        // Add transaction types section header
        $exportRows->push([
            'section' => 'TRANSACTION_TYPES',
            'name' => __('Transaction Types'),
            'start_balance' => '',
            'end_balance' => '',
            'difference' => '',
            'type' => 'header'
        ]);

        // Add transaction type breakdown
        foreach ($this->data['transaction_types'] as $transactionType) {
            $exportRows->push([
                'section' => 'TYPE',
                'name' => $transactionType['type_name'],
                'start_balance' => $transactionType['incoming_formatted'] . ' (' . __('In') . ')',
                'end_balance' => $transactionType['outgoing_formatted'] . ' (' . __('Out') . ')',
                'difference' => $transactionType['net_formatted'] . ' (' . __('Net') . ')',
                'type' => 'transaction_type'
            ]);
        }

        return $exportRows;
    }

    public function headings(): array
    {
        return [
            __('Section'),
            __('Name'),
            __('Start Balance / Incoming'),
            __('End Balance / Outgoing'),
            __('Difference / Net'),
        ];
    }

    public function map($row): array
    {
        return [
            $row['section'],
            $row['name'],
            $row['start_balance'],
            $row['end_balance'],
            $row['difference'],
        ];
    }

    public function title(): string
    {
        return __('Account Report');
    }
}