<?php

namespace Modules\Main\Http\Policies;

use Illuminate\Auth\Access\Response;
use Modules\Main\Models\City;
use Modules\User\Models\User;
use Modules\CityManager\Models\CityManager;

class ContentPolicy
{
    public function before(User $user, string $ability): bool|null
    {
        if ($user->isAdmin()) {
            return true;
        }

        return null;
    }

    /**
     * Determine whether the user can CRUD content (posts, agents, documents)
     */
    public function manageContent(User $user, City $city): bool
    {
        if (!$cityManagersOfUser = CityManager::where('user_id', $user->id)->get()) {
            return false;
        }
        foreach ($cityManagersOfUser as $cityManager) {
            if ($cityManager->city_id == $city->id) {
                return true;
            }
        }
        return false;
    }
}
