<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class DailyClosingReportExport implements FromCollection, WithHeadings, WithMapping
{
    protected $reports;

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

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

    public function headings(): array
    {
        return [
            'Date',
            'Start Date',
            'Start Time',
            'End Date',
            'End Time',
            'Restaurant',
            'Branch',
            'Available Cash',
            'Gross Sales',
            'Online/Card Payments',
            'Food Bills',
            'Expenses Paid',
            'Advance Payments',
            'Daily Wages',
            'Total Deductions',
            'Remaining Cash',
            'Difference',
            'Has Difference',
            'Comments',
            'Created By',
            'Created At',
        ];
    }

    public function map($report): array
    {
        return [
            $report->closing_date->format('Y-m-d'),
            $report->start_date ? $report->start_date->format('Y-m-d') : '',
            $report->start_time ?? '',
            $report->end_date ? $report->end_date->format('Y-m-d') : '',
            $report->end_time ?? '',
            $report->restaurant ? $report->restaurant->name : 'N/A',
            $report->branch ? $report->branch->name : 'N/A',
            number_format($report->available_cash, 2),
            number_format($report->gross_sales, 2),
            number_format($report->online_card_payments, 2),
            number_format($report->food_bills_total, 2),
            number_format($report->expenses_paid, 2),
            number_format($report->advance_payments, 2),
            number_format($report->daily_wages, 2),
            number_format($report->total_deductions, 2),
            number_format($report->remaining_cash, 2),
            number_format($report->difference, 2),
            $report->has_difference ? 'Yes' : 'No',
            $report->comments ?? '',
            $report->creator ? $report->creator->name : 'N/A',
            $report->created_at->format('Y-m-d H:i:s'),
        ];
    }
}
