<?php

namespace App\Exports;

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

// class restaurant_export implements FromCollection, WithHeadings
// {
//     /**
//      * @return \Illuminate\Support\Collection
//      */
//     public function collection()
//     {
//         $restaurant_users = IamPrincipal::where('principal_type_xid', 4)
//             ->with('restaurant')
//             ->select(
//                 'id',
//                 'first_name',
//                 'last_name',
//                 'email_address',
//                 'date_of_birth',
//                 'state_xid',
//                 'phone_number'
//             )
//             ->get();

//         $serial = 1;
//         return $restaurant_users->map(function ($restaurant_user) use (&$serial) {
//             return [
//                 'Sr No.' => $serial++, // Increment serial number
//                 'id' =>$restaurant_user->id,
//                 'first_name' => $restaurant_user->first_name,
//                 'last_name' => $restaurant_user->last_name,
//                 'email_address' => $restaurant_user->email_address,
//                 'date_of_birth' => \Carbon\Carbon::parse($restaurant_user->date_of_birth)->format('m/d/Y'),
//                 'state_xid' =>$restaurant_user->state->name ?? '', // Access the state name and handle null
//                 'phone_number' => $restaurant_user->phone_number,

//             ];
//         });
//     }

//     // Function to provide the headings in Excel
//     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 implements FromCollection, WithHeadings
{
    public function collection()
    {
        // Fetch restaurant users with their related restaurant roles and state
        $restaurant_users = IamPrincipal::where('principal_type_xid', 4)
            ->with(['getresturant.restaurant', 'state'])
            ->get();

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

    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',
        ];
    }
}
