<?php

namespace App\Exports;

use App\Models\Candidate;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class CandidatesExport implements FromCollection, WithHeadings, WithMapping
{
    protected string $type;

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

    public function collection()
    {
        return Candidate::with(['politicalParty', 'department'])
            ->where('type', $this->type)
            ->orderBy('political_party_id')
            ->orderBy('number')
            ->get();
    }

    public function headings(): array
    {
        return [
            'Tipo',
            'N°',
            'Nombres',
            'Apellidos',
            'Partido Político',
            'Siglas',
            'Departamento',
        ];
    }

    public function map($candidate): array
    {
        $types = [
            'presidential' => 'Presidencial',
            'national_senator' => 'Senador Nacional',
            'regional_senator' => 'Senador Regional',
            'regional_deputy' => 'Diputado Regional',
            'andean_parliament' => 'Parlamento Andino',
        ];

        return [
            $types[$candidate->type] ?? $candidate->type,
            $candidate->number ?? '',
            $candidate->first_name,
            $candidate->last_name,
            $candidate->politicalParty?->name ?? '',
            $candidate->politicalParty?->acronym ?? '',
            $candidate->department?->name ?? '',
        ];
    }
}
