<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class ExpenseEstimateExport implements FromArray, ShouldAutoSize, WithHeadings, WithStyles, WithTitle
{
    public function __construct(
        private readonly array $report
    ) {}

    /**
     * 시트 제목
     */
    public function title(): string
    {
        return '지출예상내역서';
    }

    /**
     * 헤더 정의
     */
    public function headings(): array
    {
        return [
            ['지출 예상 내역서 - '.$this->report['year_month']],
            [],
            ['예상 지출 합계', number_format($this->report['total_estimate']).'원'],
            ['계좌 잔액', number_format($this->report['account_balance']).'원'],
            ['예상 잔액', number_format($this->report['expected_balance']).'원'],
            [],
            ['예상 지급일', '품목', '지출금액', '거래처', '계좌'],
        ];
    }

    /**
     * 데이터 배열
     */
    public function array(): array
    {
        $rows = [];

        foreach ($this->report['items'] as $item) {
            $rows[] = [
                $item['expected_date'],
                $item['item_name'],
                number_format($item['amount']),
                $item['client_name'],
                $item['account_name'],
            ];
        }

        // 빈 줄 추가
        $rows[] = [];

        // 월별 합계
        $rows[] = ['[월별 합계]', '', '', '', ''];

        foreach ($this->report['monthly_summary']['by_month'] as $month) {
            $rows[] = [
                $month['month'].' 계',
                '',
                number_format($month['total']),
                '',
                '',
            ];
        }

        // 최종 합계
        $rows[] = [];
        $rows[] = [
            '지출 합계',
            '',
            number_format($this->report['monthly_summary']['total_expense']),
            '',
            '',
        ];
        $rows[] = [
            '계좌 잔액',
            '',
            number_format($this->report['monthly_summary']['account_balance']),
            '',
            '',
        ];
        $rows[] = [
            '최종 차액',
            '',
            number_format($this->report['monthly_summary']['final_difference']),
            '',
            '',
        ];

        return $rows;
    }

    /**
     * 스타일 정의
     */
    public function styles(Worksheet $sheet): array
    {
        return [
            1 => ['font' => ['bold' => true, 'size' => 14]],
            3 => ['font' => ['bold' => true]],
            4 => ['font' => ['bold' => true]],
            5 => ['font' => ['bold' => true]],
            7 => [
                'font' => ['bold' => true],
                'fill' => [
                    'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
                    'startColor' => ['rgb' => 'E0E0E0'],
                ],
            ],
        ];
    }
}
