<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Carbon\Carbon;

class DashboardExportUser implements FromArray, WithHeadings
{
    protected $data;

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

    public function array(): array
    {
        return array_map(function ($transaction) {
            return [
                'ID' => $transaction['id'] ?? 'N/A',
                'Customer Name' => ($transaction['iam_principal']['first_name'] ?? 'Deleted') . ' ' . ($transaction['iam_principal']['last_name'] ?? 'User'),
                'Customer ID' => $transaction['iam_principal']['id'] ?? 'Deleted User',
                'Amount' => '$' . number_format($transaction['amount'] ?? 0, 2),
                'Product Name' => $transaction['subscription_product']['product_name'] ?? 'N/A',
                'Product Details' => $transaction['subscription_product']['product_details'] ?? 'N/A',
                'Subscription Status' => $transaction['status'] ?? 'Unknown',
                'Subscription Period Start' => isset($transaction['current_period_start']) ? Carbon::parse($transaction['current_period_start'])->format('m/d/Y h:i A') : 'N/A',
                'Subscription Period End' => isset($transaction['current_period_end']) ? Carbon::parse($transaction['current_period_end'])->format('m/d/Y h:i A') : 'N/A',
                'Next Payment Date' => isset($transaction['next_payment_date']) ? Carbon::parse($transaction['next_payment_date'])->format('m/d/Y h:i A') : 'N/A',
            ];

        }, $this->data);
    }

    public function headings(): array
    {
        return [
            'ID',
            'Customer Name',
            'Customer ID',
            'Amount',
            'Product Name',
            'Product Details',
            'Subscription Status',
            'Subscription Period Start',
            'Subscription Period End',
            'Next Payment Date'
        ];
    }
}
