<?php

namespace App\Exports;

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

class QuoteExport implements FromCollection, WithHeadings
{
    protected $quotes;

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

    public function collection()
    {
        return $this->quotes->map(function ($quote) {
            $propertyInfo = $quote->property->map(function ($property) {
                return sprintf(
                    "%s (Precio: %s, Precio IT: %s, Precio Contrato: %s)",
                    $property->title,
                    $property->pivot->price,
                    $property->pivot->price_it,
                    $property->pivot->price_contrato
                );
            })->implode(', ');
            return [
                'ID' => $quote->id,
                'Nombre' => $quote->social_reason,
                'Ci/Nit' => $quote->nit,
                'Direccion' => $quote->address,
                'Telefono' => $quote->phone,
                'Email' => $quote->email,
                'Fecha de Creacion' => $quote->creation_date,
                'Fecha de Expiracion' => $quote->expiration_date,
                'Metodo de Pago' => $quote->payment_method,
                'Aprobacion de Gerencia' => $quote->status == 'APPROVED' ? 'APROBADO' : '',
                'Aprobacion de Cliente ' => $quote->status_customer == 'APPROVED' ? 'APROBADO' : '',
                'Propiedades' => $propertyInfo,
                '% Cuota inicial' => $quote->percentage_initial_fee,
                'Cuota inicial' => $quote->initial_fee,
                'Saldo' => $quote->balance,
                'Monto' => $quote->amount,
            ];
        });
    }

    public function headings(): array
    {
        return [
            'ID',
            'Nombre',
            'Ci/Nit',
            'Direccion',
            'Telefono',
            'Email',
            'Fecha de Creacion',
            'Fecha de Expiracion',
            'Metodo de Pago',
            'Aprobacion de Gerencia',
            'Aprobacion de Cliente ',
            'Propiedades',
            '% Cuota inicial',
            'Cuota inicial',
            'Saldo',
            'Monto',
        ];
    }
}
