<?php

namespace App\Exports;

use App\Models\Renewal;
use Illuminate\Database\Eloquent\Builder;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class RenewalsExport implements FromQuery, WithHeadings, WithMapping, WithStyles
{
    public function query(): Builder
    {
        return Renewal::with('client')->orderBy('next_due_date');
    }

    public function headings(): array
    {
        return [
            'ID', 'Cliente', 'Próx. Vencimiento', 'Estado',
            'Dominio', 'Costo Dom.', 'Hosting', 'Costo Host.',
            'Total', 'Moneda', 'F. Pago',
        ];
    }

    public function map($renewal): array
    {
        return [
            $renewal->id,
            $renewal->client?->name,
            $renewal->next_due_date?->format('d/m/Y'),
            $renewal->status?->label(),
            $renewal->domain_enabled ? 'Sí' : 'No',
            number_format((float) ($renewal->domain_amount ?? 0), 2),
            $renewal->hosting_enabled ? 'Sí' : 'No',
            number_format((float) ($renewal->hosting_amount ?? 0), 2),
            number_format($renewal->total_amount, 2),
            $renewal->currency?->value,
            $renewal->paid_date?->format('d/m/Y'),
        ];
    }

    public function styles(Worksheet $sheet): array
    {
        return [1 => ['font' => ['bold' => true]]];
    }
}
