<?php

namespace App\Domains\Workspace\Policies;

use App\Domains\Workspace\Models\Workspace;
use App\Models\User;

class WorkspacePolicy
{
    public function view(User $user, Workspace $workspace): bool
    {
        return $this->hasRole($user, $workspace, ['owner', 'admin', 'editor', 'viewer']);
    }

    public function create(User $user): bool
    {
        return true;
    }

    public function update(User $user, Workspace $workspace): bool
    {
        return $this->hasRole($user, $workspace, ['owner', 'admin']);
    }

    public function delete(User $user, Workspace $workspace): bool
    {
        return $this->hasRole($user, $workspace, ['owner']);
    }

    public function manageBilling(User $user, Workspace $workspace): bool
    {
        return $this->hasRole($user, $workspace, ['owner']);
    }

    /** @param array<int, string> $roles */
    private function hasRole(User $user, Workspace $workspace, array $roles): bool
    {
        return $workspace->members()
            ->where('user_id', $user->id)
            ->whereIn('role', $roles)
            ->exists();
    }
}
