<?php

namespace App\Modules\Pilkades\Policies;

use App\Models\User;
use App\Modules\Pilkades\Models\Candidate;

class CandidatePolicy
{
    public function viewAny(User $user): bool
    {
        return $user->isSuperAdmin() || $user->isAdminDesa();
    }

    public function view(User $user, Candidate $candidate): bool
    {
        return $this->ownsOrSuperAdmin($user, $candidate);
    }

    public function create(User $user): bool
    {
        return $user->isAdminDesa() || $user->isSuperAdmin();
    }

    public function update(User $user, Candidate $candidate): bool
    {
        return $this->ownsOrSuperAdmin($user, $candidate);
    }

    public function delete(User $user, Candidate $candidate): bool
    {
        return $this->ownsOrSuperAdmin($user, $candidate);
    }

    protected function ownsOrSuperAdmin(User $user, Candidate $candidate): bool
    {
        return $user->isSuperAdmin() || ($user->isAdminDesa() && $user->desa_id === $candidate->desa_id);
    }
}
