<?php

use Illuminate\Support\Facades\Cache;

use Modules\Main\Models\Company\Company;
use Modules\Main\Models\Company\CompanyAdmin;
use Modules\CityManager\Models\CityManager;
use Modules\Main\Models\City;
use Modules\Main\Models\Agent\Agent;
use Modules\Main\Models\Complex;
use Modules\User\Models\Role;
use Modules\User\Models\User;
use Modules\User\Models\UserRole;

if (!function_exists('AdminCompanyOfUser')) {
    function AdminCompanyOfUser()
    {
        if ($adminAccount = CompanyAdmin::where('user_id', auth()->user()->id)->first()) {
            return $adminAccount->company;
        } else {
            return false;
        }
    }
}

if (!function_exists(function: 'AgentCompanyOfUser')) {
    function AgentCompanyOfUser(User $user = null)
    {
        if (!$user) {
            $user = auth()->user();
        }
        $city = false;
        if ($adminAccount = Agent::where('user_id', $user->id)->first()) {
            return $adminAccount->company;
        } else {
            return false;
        }
    }
}

if (!function_exists('GetAvailableAgents')) {
    function GetAvailableAgents($resultType = 'Collection')
    {
        $agents = false;
        if ($adminCompany = AdminCompanyOfUser()) {
            $agents = Agent::where('company_id', $adminCompany->id);
        } else {
            $cityManager = CityManager::where('user_id', auth()->user()->id);
            if ($cityManager->count()) {
                $companies = Company::whereIn('city_id', $cityManager->pluck('city_id'));
                if ($companies->count()) {
                    $agents = Agent::whereIn('company_id', $companies->get()->pluck('id'));
                }
            } else {
                $agents = Agent::where('user_id', auth()->user()->id);
            }
        }
        if ($agents === false) {
            $agents = Agent::where('id', 0);
        }
        $agents->with('company:id,name');
        $agents->with('user:id,name');
        $agents->orderBy('company_id');
        if ($resultType == 'Collection') {
            return $agents->get();
        } else {
            return $agents;
        };
    }
}

if (!function_exists('GetAvailableCompanies')) {
    function GetAvailableCompanies($resultType = 'Collection')
    {
        $companiesIds = [];
        if (UserRole::where('user_id', auth()->user()->id)->where('role_id', Role::SUPER_ADMIN)->count() == 1) {
            $companies = Company::all()->pluck('id');
            $companiesIds = array_merge($companiesIds, $companies->all());
        } else {
            if ($adminCompany = AdminCompanyOfUser()) {
                $companiesIds[] = $adminCompany->id;
            }
            if ($cityManager = CityManager::where('user_id', auth()->user()->id)) {
                if ($cityManager->count()) {
                    $companies = Company::whereIn('city_id', $cityManager->pluck('city_id'));
                    if ($companies->count()) {
                        $companies = $companies->get()->pluck('id');
                        $companiesIds = array_merge($companiesIds, $companies->all());
                    }
                }
            }
        }
        $companiesIds = array_unique($companiesIds);
        $companies = Company::whereIn('id', $companiesIds);
        if ($resultType == 'Collection') {
            return $companies->get();
        } else {
            return $companies;
        };
    }
}

if (!function_exists('GetAvailableComplexes')) {
    function GetAvailableComplexes()
    {
        $complexes = false;
        if ($adminCompany = AdminCompanyOfUser()) {
            $complexes = Complex::where('city_id', $adminCompany->city_id);
        } else {
            $cityManager = CityManager::where('user_id', auth()->user()->id);
            if ($cityManager->count()) {
                $complexes = Complex::where('city_id', $cityManager->first()->city_id);
            } else {
                if ($agent = Agent::where('user_id', auth()->user()->id)->first()) {
                    $complexes = Complex::where('city_id', $agent->company?->city_id);
                }
            }
        }
        if ($complexes) {
            return $complexes->get();
        }
        return [];
    }
}

if (!function_exists('GetAvailableCities')) {
    function GetAvailableCities()
    {
        $user = auth()->user();
        return Cache::remember('helper_available_cities_of_user_' . $user->id, now()->addMinutes(10), function () use ($user) {
            if ($user->hasRole(Role::SUPER_ADMIN)) {
                return City::all();
            }
            if ($user->hasRole(Role::CITY_MANAGER)) {
                $cititesOfManager = CityManager::where('user_id', $user->id)->pluck('city_id');
                if ($cititesOfManager->count()) {
                    return City::whereIn('id', $cititesOfManager)->get();
                }
                return false;
            }
            if ($user->hasRole(Role::COMPANY_ADMIN)) {
                $adminCompany = AdminCompanyOfUser();
                if ($adminCompany->count()) {
                    return City::where('id', $adminCompany->city_id)->get();
                }
                return false;
            }
            if ($user->hasRole(Role::AGENT)) {
                $agentCompany = AgentCompanyOfUser();
                if ($agentCompany->count()) {
                    return City::where('id', $agentCompany->city_id)->get();
                }
                return false;
            }
            return false;
        });


        /*$agents = [];
        if ($adminCompany = AdminCompanyOfUser()) {
            $agents = Agent::where('company_id', $adminCompany->id);
        } else {
            $cityManager = CityManager::where('user_id', auth()->user()->id);
            if ($cityManager->count()) {
                $agents = Agent::whereIn('company_id', Company::where('city_id', $cityManager->city_id)->get()->pluck('company_id'));
            } else {
                $agents = Agent::where('user_id', auth()->user()->id);
            }
        }
        $agents->with('company:id,name');
        $agents->with('user:id,name');
        return $agents->get();*/
    }
}
