<?php

namespace Modules\Main\Http\Policies;

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

class CompanyPolicy
{
    public function create(User $user): bool
    {
        if (UserRole::where('user_id', $user->id)->where('role_id', Role::SUPER_ADMIN)->count() == 1)
        {
            return true;
        }
        return false;
    }
    public function editAgentsTable(User $user): bool
    {
        dd(111);
        if (UserRole::where('user_id', $user->id)->where('role_id', Role::COMPANY_ADMIN)->count() == 1)
        {
            return true;
        }
        return false;
    }
    public function editClientsTable(User $user): bool
    {
        if (UserRole::where('user_id', $user->id)->where('role_id', Role::COMPANY_ADMIN)->count() == 1)
        {
            return true;
        }
        if (UserRole::where('user_id', $user->id)->where('role_id', Role::CITY_MANAGER)->count() == 1)
        {
            return true;
        }
        return false;
    }
    public function update(User $user, Company $company): bool
    {
        if (UserRole::where('user_id', $user->id)->where('role_id', Role::SUPER_ADMIN)->count() == 1)
        {
            return true;
        }
        if (
            CityManager::where('user_id', $user->id)->where('city_id', $company->city_id)->count() == 1
        )
        {
            return true;
        }
        if (
            CompanyAdmin::where('user_id', $user->id)->where('company_id', $company->id)->count() == 1
            && UserRole::where('user_id', $user->id)->where('role_id', Role::COMPANY_ADMIN)->count() == 1
        )
        {
            return true;
        }
        return false;
    }
}
