<?php

declare(strict_types=1);

namespace Drupal\flowdrop_runtime;

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_runtime\Constants\Permissions;

/**
 * Access control handler for FlowDrop Workflow Snapshot entities.
 *
 * Handles permission checks for viewing, editing, and deleting snapshots
 * with support for ownership-based access control.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
class FlowDropWorkflowSnapshotAccessControlHandler 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_SNAPSHOT)) {
      return AccessResult::allowed()->cachePerPermissions();
    }

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

    switch ($operation) {
      case 'view':
        if ($account->hasPermission(Permissions::VIEW_ANY_SNAPSHOT)) {
          return AccessResult::allowed()->cachePerPermissions();
        }
        if ($account->hasPermission(Permissions::VIEW_OWN_SNAPSHOT)) {
          $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_SNAPSHOT)) {
          return AccessResult::allowed()->cachePerPermissions();
        }
        if ($account->hasPermission(Permissions::EDIT_OWN_SNAPSHOT)) {
          $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_SNAPSHOT)) {
          return AccessResult::allowed()->cachePerPermissions();
        }
        if ($account->hasPermission(Permissions::DELETE_OWN_SNAPSHOT)) {
          $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 {
    // Snapshots are typically created programmatically, not via UI.
    if ($account->hasPermission(FlowDropPermissions::ADMINISTER_FLOWDROP)) {
      return AccessResult::allowed()->cachePerPermissions();
    }

    return AccessResult::allowedIfHasPermission($account, Permissions::ADMINISTER_SNAPSHOT);
  }

}
