<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Portal extends Model
{
    use HasFactory;

    protected $fillable = [
        'key',
        'name',
        'description',
        'url',
        'icon',
        'enabled',
        'show_in_user_dashboard',
        'order',
    ];

    protected $casts = [
        'enabled' => 'boolean',
        'show_in_user_dashboard' => 'boolean',
        'order' => 'integer',
    ];

    /**
     * Users who have access to this portal.
     */
    public function users()
    {
        return $this->belongsToMany(User::class, 'portal_user')
            ->withPivot('role_scope')
            ->withTimestamps();
    }

    /**
     * Get users with admin access to this portal.
     */
    public function admins()
    {
        return $this->belongsToMany(User::class, 'portal_user')
            ->wherePivot('role_scope', 'portal_admin')
            ->withPivot('role_scope')
            ->withTimestamps();
    }

    /**
     * Get regular users of this portal.
     */
    public function regularUsers()
    {
        return $this->belongsToMany(User::class, 'portal_user')
            ->wherePivot('role_scope', 'user')
            ->withPivot('role_scope')
            ->withTimestamps();
    }
}
