<?php

namespace App\Policies;

use App\Models\BackupSchedule;
use App\Models\Tenant;
use App\Models\User;
use App\Support\TenantRole;
use Illuminate\Auth\Access\HandlesAuthorization;

class BackupSchedulePolicy
{
    use HandlesAuthorization;

    protected function resolveRole(User $user): ?TenantRole
    {
        $tenant = Tenant::current();

        return $user->tenantRole($tenant);
    }

    public function viewAny(User $user): bool
    {
        return $this->resolveRole($user) !== null;
    }

    public function view(User $user, BackupSchedule $schedule): bool
    {
        return $this->resolveRole($user) !== null;
    }

    public function create(User $user): bool
    {
        return $this->resolveRole($user)?->canManageBackupSchedules() ?? false;
    }

    public function update(User $user, BackupSchedule $schedule): bool
    {
        return $this->resolveRole($user)?->canManageBackupSchedules() ?? false;
    }

    public function delete(User $user, BackupSchedule $schedule): bool
    {
        return $this->resolveRole($user)?->canManageBackupSchedules() ?? false;
    }
}
