<?php

declare(strict_types=1);

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

/**
 * Access control handler for FlowDropStateCheckpoint entities.
 *
 * Handles permission checks for viewing, editing, and deleting checkpoints
 * with support for ownership-based access control.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
class FlowDropStateCheckpointAccessControlHandler 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_CHECKPOINT)) {
          return AccessResult::allowed()->cachePerPermissions();
        }
        if ($account->hasPermission(Permissions::VIEW_OWN_CHECKPOINT)) {
          $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_CHECKPOINT)) {
          return AccessResult::allowed()->cachePerPermissions();
        }
        if ($account->hasPermission(Permissions::EDIT_OWN_CHECKPOINT)) {
          $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_CHECKPOINT)) {
          return AccessResult::allowed()->cachePerPermissions();
        }
        if ($account->hasPermission(Permissions::DELETE_OWN_CHECKPOINT)) {
          $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_CHECKPOINT, Permissions::ADMINISTER], 'OR');
  }

}
