<?php

declare(strict_types=1);

namespace Drupal\flowdrop_job;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\flowdrop\Constants\Permissions as FlowDropPermissions;
use Drupal\user\EntityOwnerInterface;
use Drupal\flowdrop_job\Constants\Permissions;

/**
 * Defines the access control handler for the flowdrop job entity type.
 *
 * Handles permission checks for viewing, editing, and deleting jobs
 * with support for ownership-based access control.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
final class FlowDropJobAccessControlHandler extends EntityAccessControlHandler {

  /**
   * {@inheritdoc}
   */
  protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account): AccessResultInterface {
    $operation = (string) $operation;

    // Global super-admin bypass.
    if ($account->hasPermission(FlowDropPermissions::ADMINISTER_FLOWDROP)) {
      return AccessResult::allowed()->cachePerPermissions();
    }

    // Entity type admin bypass.
    if ($account->hasPermission($this->entityType->getAdminPermission())) {
      return AccessResult::allowed()->cachePerPermissions();
    }

    if (!$entity instanceof EntityOwnerInterface) {
      return AccessResult::neutral();
    }

    switch ($operation) {
      case 'view':
        if ($account->hasPermission(Permissions::VIEW_ANY)) {
          return AccessResult::allowed()->cachePerPermissions();
        }
        if ($account->hasPermission(Permissions::VIEW_OWN)) {
          $is_owner = ((int) $entity->getOwnerId()) === ((int) $account->id());
          return AccessResult::allowedIf($is_owner)
            ->cachePerPermissions()
            ->cachePerUser()
            ->addCacheableDependency($entity);
        }
        return AccessResult::forbidden()->cachePerPermissions();

      case 'update':
        if ($account->hasPermission(Permissions::EDIT_ANY)) {
          return AccessResult::allowed()->cachePerPermissions();
        }
        if ($account->hasPermission(Permissions::EDIT_OWN)) {
          $is_owner = ((int) $entity->getOwnerId()) === ((int) $account->id());
          return AccessResult::allowedIf($is_owner)
            ->cachePerPermissions()
            ->cachePerUser()
            ->addCacheableDependency($entity);
        }
        return AccessResult::forbidden()->cachePerPermissions();

      case 'delete':
        if ($account->hasPermission(Permissions::DELETE_ANY)) {
          return AccessResult::allowed()->cachePerPermissions();
        }
        if ($account->hasPermission(Permissions::DELETE_OWN)) {
          $is_owner = ((int) $entity->getOwnerId()) === ((int) $account->id());
          return AccessResult::allowedIf($is_owner)
            ->cachePerPermissions()
            ->cachePerUser()
            ->addCacheableDependency($entity);
        }
        return AccessResult::forbidden()->cachePerPermissions();

      default:
        return AccessResult::neutral();
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL): AccessResultInterface {
    if ($account->hasPermission(FlowDropPermissions::ADMINISTER_FLOWDROP)) {
      return AccessResult::allowed()->cachePerPermissions();
    }

    return AccessResult::allowedIfHasPermissions($account, [Permissions::CREATE, Permissions::ADMINISTER_TYPES], 'OR');
  }

}
