<?php
namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use App\Models\IamPrincipal;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;

// class restaurant_export_selected implements FromCollection, WithHeadings
// {
//     protected $ids;

//     public function __construct($ids)
//     {
//         $this->ids = is_array($ids) ? $ids : explode(',', $ids);
//     }

//     /**
//      * @return \Illuminate\Support\Collection
//      */
//     public function collection()
//     {
//         Log::info('Fetching data for IDs: ' . implode(',', $this->ids));

//         $selected_customers = IamPrincipal::whereIn('id', $this->ids)
//             ->with('state:id,name')
//             ->orderBy('id', 'desc')
//             ->select(
//                 'id',
//                 'first_name',
//                 'last_name',
//                 'email_address',
//                 'date_of_birth',
//                 'state_xid',
//                 'phone_number'
//             )
//             ->get();

//         $serial = 1;
//         $mappedCustomers = $selected_customers->map(function ($customer) use (&$serial) {
//             return [
//                 'Sr No.' => $serial++,
//                 'id' => $customer->id,
//                 'first_name' => $customer->first_name,
//                 'last_name' => $customer->last_name,
//                 'email_address' => $customer->email_address,
//                 'date_of_birth' => \Carbon\Carbon::parse($customer->date_of_birth)->format('m/d/Y'),
//                 'state_xid' => $customer->state->name ?? '',
//                 'phone_number' => $customer->phone_number,
//             ];
//         });

//         Log::info('Fetched customer data: ' . $mappedCustomers->toJson());

//         return new Collection($mappedCustomers);
//     }

//     public function headings(): array
//     {
//         return [
//             'Sr No.',
//             'User Id',
//             'First Name',
//             'Last Name',
//             'Email Address',
//             'Date of Birth',
//             'State Name',
//             'Phone Number'
//         ];
//     }
// }

class restaurant_export_selected implements FromCollection, WithHeadings
{
    protected $ids;

    public function __construct($ids)
    {
        $this->ids = is_array($ids) ? $ids : explode(',', $ids);
    }

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        Log::info('Fetching data for IDs: ' . implode(',', $this->ids));

        $selected_customers = IamPrincipal::whereIn('id', $this->ids)
            ->with(['state', 'getresturant.restaurant'])
            ->orderBy('id', 'desc')
            ->select(
                'id',
                'first_name',
                'last_name',
                'email_address',
                'date_of_birth',
                'state_xid',
                'phone_number'
            )
            ->get();

        $serial = 1;
        $mappedCustomers = $selected_customers->flatMap(function ($customer) use (&$serial) {
            return $customer->getresturant->map(function ($role) use (&$serial, $customer) {
                return [
                    'Sr No.' => $serial++,
                    'User Id' => $customer->id,
                    'First Name' => $customer->first_name,
                    'Last Name' => $customer->last_name,
                    'Email Address' => $customer->email_address,
                    'Date of Birth' => \Carbon\Carbon::parse($customer->date_of_birth)->format('m/d/Y'),
                    // 'State Name' => optional($customer->state)->name ?? 'NA',
                    'Phone Number' => $customer->phone_number,
                    'Restaurant Name' => optional($role->restaurant)->name ?? 'NA',
                    'Restaurant Address' => optional($role->restaurant)->address ?? 'NA',
                ];
            });
        });

        Log::info('Fetched customer data: ' . $mappedCustomers->toJson());

        return new Collection($mappedCustomers);
    }

    public function headings(): array
    {
        return [
            'Sr No.',
            'User Id',
            'First Name',
            'Last Name',
            'Email Address',
            'Date of Birth',
            // 'State Name',
            'Phone Number',
            'Restaurant Name',
            'Restaurant Address'
        ];
    }
}
