<?php

namespace App\Exports;

use App\Models\ManageFeedback;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class FeedbakExport implements FromCollection, WithHeadings
{
    public function collection()
    {
        return ManageFeedback::with(['principal', 'feedbackReaction', 'restaurant'])->get()->map(function ($feedback) {
            $feedbackType = 'N/A';
            if ($feedback->is_app_feedback) {
                $feedbackType = 'App Feedback';
            } elseif ($feedback->is_restaurant_feedback) {
                $feedbackType = 'Restaurant Feedback';
            }

            return [
                'Customer Id' => $feedback->principal ? $feedback->principal->id : 'N/A',
                'First Name' => $feedback->principal ? $feedback->principal->first_name : 'N/A',
                'Last Name' => $feedback->principal ? $feedback->principal->last_name : 'N/A',
                'Feedback Reaction' => $feedback->feedbackReaction ? $feedback->feedbackReaction->feedback_reaction_title : 'N/A',
                'Comment' => $feedback->comment,
                'Date Received' => \Carbon\Carbon::parse($feedback->created_at)->format('m/d/Y'),
                'Feedback Type' => $feedbackType,
                'Restaurant Name' => $feedback->restaurant ? $feedback->restaurant->name : 'N/A',
            ];
        });
    }

    public function headings(): array
    {
        return [
            'Customer Id',
            'First Name',
            'Last Name',
            'Feedback Reaction',
            'Comment',
            'Date Received',
            'App Feedback',
            'Restaurant Feedback',
            'Restaurant Name'


        ];
    }
}
