<?php

namespace App\Exports;

use App\Models\Chat;
use App\Models\Message;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class MessagesExport implements FromCollection, WithHeadings, WithMapping
{
    protected $chatId;
    public function __construct($chatId)
    {
        $this->chatId = $chatId;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return Message::with('sender', 'receiver')->where('chat_id', $this->chatId)->get();
    }

    public function headings(): array
    {
        return [
            'Company ID',
            'Company Name',
            'Message',
            'File',
            'Date', 
        ];
    }

    public function map($msg): array
    {
        return [
            $msg->sender->id,
            $msg->sender->name,
            $msg->message,
            $msg->file_path,
            $msg->created_at->format('Y-m-d H:i'),
        ];
    }
}
