export type MatrixMountCardinality =
  | 'authority-singleton'
  | 'device-singleton'
  | 'device-local-control'
  | 'queue-service'
  | 'named-instance'
  | 'replicated-read';

export interface MatrixPlacementDeclaration {
  readonly cardinality: MatrixMountCardinality;
  readonly failover?: 'manual' | 'lease-timeout';
  readonly queue?: string;
  readonly physicalMount?: string;
}

export type MatrixPlacementClaimMode =
  | 'authoritative'
  | 'local-only'
  | 'pool-member'
  | 'mirror';

export interface RuntimeInventoryPlacementMetadata {
  readonly cardinality: MatrixMountCardinality;
  readonly claimMode: MatrixPlacementClaimMode;
  readonly logicalMount: string;
  readonly physicalMount: string;
  readonly callable: boolean;
  readonly standbyReason?: string;
  readonly placementEpoch?: number;
}

export type MatrixPlacementBindDenyReason =
  | 'SINGLETON_MOUNT_NOT_OWNED'
  | 'AUTHORITY_COORDINATOR_LEASE_NOT_OWNED'
  | 'UNKNOWN_PLACEMENT'
  | 'QUEUE_REQUIRED';

export type AuthorityCoordinatorLeaseStatus =
  | 'active'
  | 'released'
  | 'expired'
  | 'fenced';

export type AuthorityCoordinatorLeaseScope =
  | 'local-home'
  | 'hub-authority'
  | 'remote-projection';

export type AuthorityCoordinatorLeaseAuthority =
  | 'local'
  | 'hub';

export type AuthorityCoordinatorLeaseSource =
  | 'local-file'
  | 'hub-projection';

export interface AuthorityCoordinatorLease {
  readonly version: 1;
  readonly authorityRoot: string;
  readonly leaseId: string;
  readonly ownerNodeId: string;
  readonly ownerHostId?: string;
  readonly ownerInstallId?: string;
  readonly ownerHostLinkId?: string;
  readonly placementEpoch: number;
  readonly acquiredAt: string;
  readonly heartbeatAt: string;
  readonly expiresAt: string;
  readonly status: AuthorityCoordinatorLeaseStatus;
  readonly scope: AuthorityCoordinatorLeaseScope;
  readonly leaseAuthority: AuthorityCoordinatorLeaseAuthority;
  readonly source: AuthorityCoordinatorLeaseSource;
  readonly reason?: string;
}

export interface AuthorityCoordinatorLeaseAcquireInput {
  readonly authorityRoot: string;
  readonly ownerNodeId: string;
  readonly ownerHostId?: string;
  readonly ownerInstallId?: string;
  readonly ownerHostLinkId?: string;
  readonly placementEpoch: number;
  readonly scope?: AuthorityCoordinatorLeaseScope;
  readonly leaseAuthority?: AuthorityCoordinatorLeaseAuthority;
  readonly source?: AuthorityCoordinatorLeaseSource;
  readonly ttlMs: number;
  readonly now?: Date;
  readonly force?: boolean;
}

export interface AuthorityCoordinatorLeaseAcquireResult {
  readonly ok: boolean;
  readonly acquired: boolean;
  readonly lease?: AuthorityCoordinatorLease;
  readonly currentOwner?: AuthorityCoordinatorLease;
  readonly reason?:
    | 'LEASE_HELD_BY_OTHER_OWNER'
    | 'LEASE_EXPIRED'
    | 'LEASE_RELEASED'
    | 'LEASE_FORCED'
    | 'LEASE_RENEWED'
    | 'LEASE_ACQUIRED';
}

export interface MatrixPlacementBindRequest {
  readonly authorityRoot: string;
  readonly runtimeId?: string;
  readonly packageName?: string;
  readonly mount: string;
  readonly cardinality: MatrixMountCardinality;
  readonly topologyKind?: string;
  readonly authorityCoordinatorActive: boolean;
  readonly ownerNodeId?: string;
  readonly authorityLease?: AuthorityCoordinatorLease | null;
}

export interface MatrixPlacementBindDecision {
  readonly allowed: boolean;
  readonly callable: boolean;
  readonly reason?: MatrixPlacementBindDenyReason;
}

export interface MatrixPlacementPolicy {
  canBind(input: MatrixPlacementBindRequest): MatrixPlacementBindDecision;
}

export function decideMatrixPlacementBind(
  request: MatrixPlacementBindRequest,
): MatrixPlacementBindDecision {
  if (
    request.cardinality === 'authority-singleton'
    && request.authorityCoordinatorActive === false
  ) {
    return {
      allowed: false,
      callable: false,
      reason: 'SINGLETON_MOUNT_NOT_OWNED',
    };
  }

  if (request.cardinality === 'authority-singleton' && request.authorityLease) {
    if (
      request.authorityLease.status !== 'active'
      || (
        request.ownerNodeId
        && request.authorityLease.ownerNodeId !== request.ownerNodeId
      )
    ) {
      return {
        allowed: false,
        callable: false,
        reason: 'AUTHORITY_COORDINATOR_LEASE_NOT_OWNED',
      };
    }
  }

  return {
    allowed: true,
    callable: true,
  };
}
