<?php

namespace App\Exports;

use Carbon\Carbon;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Mtc\VehicleReservations\Reservation;

class ReservationExport implements FromCollection, WithHeadings
{
    public function __construct(private readonly array $input)
    {
    }

    public function collection(): Collection
    {
        return Reservation::query()
            ->with(['vehicle' => fn($query) => $query->withTrashed()->with(['make', 'model', 'dealership'])])
            ->whereNotNull('confirmed_at')
            ->when(
                !empty($this->input['status']) && $this->input['status'] !== 'all',
                fn($query) => $query->where('status', $this->input['status'])
            )
            ->when(
                !empty($this->input['from']),
                fn($query) => $query->where('confirmed_at', '>=', Carbon::parse($this->input['from'])->startOfDay())
            )
            ->when(
                !empty($this->input['to']),
                fn($query) => $query->where('confirmed_at', '<=', Carbon::parse($this->input['to'])->endOfDay())
            )
            ->latest('confirmed_at')
            ->get()
            ->map(fn(Reservation $reservation) => $this->mapRow($reservation));
    }

    private function mapRow(Reservation $reservation): array
    {
        return [
            $reservation->reference,
            $reservation->name,
            $reservation->email,
            $reservation->contact_number,
            $reservation->status,
            $reservation->amount,
            $reservation->currency,
            $reservation->vehicle?->registration_number,
            $reservation->vehicle?->title,
            $reservation->vehicle?->make?->name,
            $reservation->vehicle?->model?->name,
            $reservation->vehicle?->dealership?->name,
            $reservation->data['customer_has_part_ex'] ?? null,
            $reservation->data['customer_requires_finance'] ?? null,
            $reservation->data['meta']['utm_source'] ?? null,
            $reservation->data['meta']['utm_medium'] ?? null,
            $reservation->data['meta']['utm_campaign'] ?? null,
            $reservation->data['meta']['utm_content'] ?? null,
            $reservation->data['meta']['utm_term'] ?? null,
            $reservation->confirmed_at?->format('Y-m-d H:i'),
            $reservation->created_at?->format('Y-m-d H:i'),
        ];
    }

    public function headings(): array
    {
        return [
            'Reference',
            'Customer Name',
            'Email',
            'Contact Number',
            'Status',
            'Amount',
            'Currency',
            'Registration Number',
            'Vehicle',
            'Make',
            'Model',
            'Dealership',
            'Has Part Exchange',
            'Requires Finance',
            'UTM Source',
            'UTM Medium',
            'UTM Campaign',
            'UTM Content',
            'UTM Term',
            'Confirmed At',
            'Created At',
        ];
    }
}
