<?php

namespace App\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithTitle;

class ProfileMessagesExport implements FromCollection, WithTitle, WithHeadings, WithMapping
{
    use Exportable;

    protected $data;

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

    public function collection()
    {
        return $this->data;
    }

    public function headings(): array
    {
        return [
            __('Conversation ID'),
            __('Conversation type'),
            __('Message ID'),
            __('Date'),
            __('Sender name'),
            __('Sender type'),
            __('Message'),
            __('Reply to (ID)'),
        ];
    }

    public function map($message): array
    {
        $conversationType = $message['conversation_type'] ?? '';
        $conversationType = $conversationType ? __(ucfirst($conversationType)) : '';

        return [
            $message['conversation_id'],
            $conversationType,
            $message['id'],
            $message['created_at'],
            $message['sender_name'] ?? '',
            $message['sender_type'] ?? '',
            $message['body'],
            $message['reply_id'] ?? '',
        ];
    }

    public function title(): string
    {
        return __('Messages');
    }
}
