<?php

namespace App\Http\Livewire\Profile;

use App\Models\Admin;
use App\Models\Bank;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
use Livewire\Component;

class SelectProfile extends Component
{
    public $label = '';
    public $placeholder = '';
    public $typesAvailable = [];
    public $search;
    public $searchResults = [];
    public $showDropdown = false;
    public $selected = [];

    protected $listeners = [
        'resetForm',
        'organizerExists'
    ];

    public function mount()
    {
        $this->placeholder = implode(', ', array_map(function($class) {
            // Convert to lowercase and use as translation key
            return __((strtolower(class_basename($class))));
        }, $this->typesAvailable));
    }  

    public function inputBlur()
    {
        $this->showDropdown = false;
        $this->search = '';
    }

    
    public function resetForm()
    {
        $this->reset();
    }


    public function selectProfile($value)
    {
        $this->selected = collect($this->searchResults)->where('id', '=', $value)->first();
        $this->showDropdown = false;
        $this->search = '';
        $this->dispatch('selectedProfile', $this->selected);
    }


    /**
     * updatedSearch: Search available profiles
     *
     * @param  mixed $newValue
     * @return void
     */
    public function updatedSearch($newValue)
    {
        $this->showDropdown = true;
        $search = $this->search;
        $results = collect();
    
        // Loop through typesAvailable and query each model
        foreach ($this->typesAvailable as $type) {
            if ($type === User::class) {
                $users = User::where('name', 'like', '%' . $search . '%')
                    ->select('id', 'name', 'profile_photo_path')
                    ->get()
                    ->map(function ($item) {
                        return [
                            'id' => $item['id'],
                            'type' => User::class,
                            'name' => $item['name'],
                            'description' => '',
                            'profile_photo_path' => url(Storage::url($item['profile_photo_path']))
                        ];
                    });
                $results = $results->merge($users);
            }
            if ($type === Organization::class) {
                $organizations = Organization::where('name', 'like', '%' . $search . '%')
                    ->select('id', 'name', 'profile_photo_path')
                    ->get()
                    ->map(function ($item) {
                        return [
                            'id' => $item['id'],
                            'type' => Organization::class,
                            'name' => $item['name'],
                            'description' => __('Organization'),
                            'profile_photo_path' => url(Storage::url($item['profile_photo_path']))
                        ];
                    });
                $results = $results->merge($organizations);
            }
            if (class_exists('App\\Models\\Bank') && $type === Bank::class) {
                $banks = Bank::where('name', 'like', '%' . $search . '%')
                    ->select('id', 'name', 'profile_photo_path')
                    ->get()
                    ->map(function ($item) {
                        return [
                            'id' => $item['id'],
                            'type' => \App\Models\Bank::class,
                            'name' => $item['name'],
                            'description' => __('Bank'),
                            'profile_photo_path' => url(Storage::url($item['profile_photo_path']))
                        ];
                    });
                $results = $results->merge($banks);
            }
            if (class_exists('App\\Models\\Admin') && $type === Admin::class) {
                $admins = Admin::where('name', 'like', '%' . $search . '%')
                    ->select('id', 'name', 'profile_photo_path')
                    ->get()
                    ->map(function ($item) {
                        return [
                            'id' => $item['id'],
                            'type' => \App\Models\Admin::class,
                            'name' => $item['name'],
                            'description' => __('Admin'),
                            'profile_photo_path' => url(Storage::url($item['profile_photo_path']))
                        ];
                    });
                $results = $results->merge($admins);
            }
        }
    
        $this->searchResults = $results->take(6)->values();
    }


    public function removeSelectedProfile()
    {
        $this->selected = [];
        $this->dispatch('selectedProfile', null);
    }


    public function render()
    {
        return view('livewire.profile.select-profile');
    }
}
