<?php

namespace App\Exports;

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

class MontessoriMembershipApplicationsExport implements FromCollection, WithHeadings, WithMapping
{
    protected $applications;

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

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

    public function headings(): array
    {
        return [
            '#',
            'الاسم',
            'البريد الإلكتروني',
            'الهاتف',
            'البلد',
            'الدبلوم',
            'الحالة',
            'تاريخ التسجيل',
        ];
    }

    public function map($application): array
    {
        $formData = is_array($application->form_data) ? $application->form_data : json_decode($application->form_data, true);
        
        $name = '';
        $email = '';
        $phone = '';
        $country = '';
        $diploma = '';
        
        // Extract values dynamically
        if (is_array($formData)) {
            foreach ($formData as $key => $value) {
                if (!empty($value) && is_string($value) && !str_starts_with($value, '/store/')) {
                    // Try to find name
                    if (empty($name) && (str_contains($key, 'اسم') || str_contains($key, 'name') || str_contains($key, 'Name'))) {
                        $name = $value;
                    }
                    // Try to find email
                    if (empty($email) && (str_contains($value, '@') || str_contains($key, 'بريد') || str_contains($key, 'email') || str_contains($key, 'Email'))) {
                        $email = $value;
                    }
                    // Try to find phone
                    if (empty($phone) && (str_contains($key, 'تليفون') || str_contains($key, 'هاتف') || str_contains($key, 'phone') || str_contains($key, 'Phone') || str_contains($key, 'رقم'))) {
                        $phone = $value;
                    }
                    // Try to find country
                    if (empty($country) && (str_contains($key, 'بلد') || str_contains($key, 'دولة') || str_contains($key, 'country'))) {
                        $country = $value;
                    }
                    // Try to find diploma
                    if (empty($diploma) && (str_contains($key, 'دبلوم') || str_contains($key, 'diploma') || str_contains($key, 'خريج'))) {
                        $diploma = $value;
                    }
                }
            }
            
            // If name still empty, use first available value
            if (empty($name)) {
                foreach ($formData as $value) {
                    if (!empty($value) && is_string($value) && !str_starts_with($value, '/store/')) {
                        $name = $value;
                        break;
                    }
                }
            }
        }
        
        // Translate status
        $statusMap = [
            'pending' => 'قيد الانتظار',
            'approved' => 'موافق عليه',
            'rejected' => 'مرفوض'
        ];
        $status = $statusMap[$application->status] ?? $application->status;
        
        $createdAt = date('Y-m-d H:i:s', $application->created_at);
        
        return [
            $application->id,
            $name,
            $email,
            $phone,
            $country,
            $diploma,
            $status,
            $createdAt
        ];
    }
}
