<?php

declare(strict_types=1);

namespace Drupal\flowdrop_interrupt\Access;

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\flowdrop_interrupt\Constants\Permissions;
use Drupal\user\EntityOwnerInterface;

/**
 * Access control handler for FlowDrop Interrupt entities.
 *
 * Handles permission checks for viewing, resolving, and cancelling interrupts
 * with support for ownership-based access control.
 *
 * @internal
 *   This class is not part of the public API and may change without notice.
 */
class FlowDropInterruptAccessControlHandler 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(Permissions::ADMINISTER)) {
      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 'resolve':
        if ($account->hasPermission(Permissions::RESOLVE_ANY)) {
          return AccessResult::allowed()->cachePerPermissions();
        }
        if ($account->hasPermission(Permissions::RESOLVE_OWN)) {
          $is_owner = ((int) $entity->getOwnerId()) === ((int) $account->id());
          return AccessResult::allowedIf($is_owner)
            ->cachePerPermissions()
            ->cachePerUser()
            ->addCacheableDependency($entity);
        }
        return AccessResult::forbidden()->cachePerPermissions();

      case 'cancel':
        if ($account->hasPermission(Permissions::CANCEL_ANY)) {
          return AccessResult::allowed()->cachePerPermissions();
        }
        if ($account->hasPermission(Permissions::CANCEL_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::allowedIfHasPermission($account, Permissions::ADMINISTER);
  }

}
