package EPrints::KlarasConnect::Identity;

# What this installation tells the panel about itself on activation and
# on every heartbeat. No member data, no secrets.

use strict;
use warnings;

use EPrints::KlarasConnect;
use EPrints::KlarasConnect::Settings;
use EPrints::KlarasConnect::Compat;

# The public https address the repository is reached at, as EPrints knows
# it. The librarian confirms (or corrects) it on the settings screen,
# because many sites sit behind a TLS proxy EPrints does not know about.
sub suggested_base_url
{
	my( $repo ) = @_;

	my $securehost = $repo->config( 'securehost' );
	if( defined $securehost && length $securehost )
	{
		my $port = $repo->config( 'secureport' );
		my $root = $repo->config( 'https_root' );
		$root = '' if !defined $root;
		my $url = 'https://' . $securehost;
		$url .= ":$port" if defined $port && length $port && $port != 443;
		return _clean( $url . $root );
	}

	my $host = $repo->config( 'host' );
	if( defined $host && length $host )
	{
		my $root = $repo->config( 'http_root' );
		$root = '' if !defined $root;
		# Suggest https even when EPrints is configured for http: the member
		# app only connects over https.
		return _clean( 'https://' . $host . $root );
	}

	return _clean( $repo->config( 'base_url' ) );
}

sub base_url
{
	my( $repo ) = @_;
	my $saved = EPrints::KlarasConnect::Settings::get( $repo, 'base_url' );
	return defined $saved && length $saved ? $saved : suggested_base_url( $repo );
}

sub source_api_base
{
	my( $repo, $base ) = @_;
	return ( defined $base ? $base : base_url( $repo ) ) . EPrints::KlarasConnect::SOURCE_PREFIX;
}

sub display_name
{
	my( $repo ) = @_;
	my $name = eval { $repo->phrase( 'archive_name' ) };
	$name = $repo->get_id if !defined $name || !length $name;
	$name =~ s/\s+/ /g;
	$name =~ s/\A\s+|\s+\z//g;
	return substr( $name, 0, 190 );
}

sub capabilities
{
	return [qw( search detail download events discover )];
}

# Payload for POST /api/v1/instances/activate and /heartbeat. base_url
# overrides the saved address: activation reports the one being tried
# before it is saved.
sub report
{
	my( $repo, %opts ) = @_;
	my $base = defined $opts{base_url} ? $opts{base_url} : base_url( $repo );

	return {
		platform         => EPrints::KlarasConnect::PLATFORM,
		instance_id      => EPrints::KlarasConnect::Settings::get( $repo, 'instance_id' ),
		base_url         => $base,
		source_api_base  => source_api_base( $repo, $base ),
		plugin_version   => EPrints::KlarasConnect::version(),
		platform_version => EPrints::KlarasConnect::Compat::eprints_version(),
		runtime_version  => sprintf( '%vd', $^V ),
		display_name     => display_name( $repo ),
		capabilities     => capabilities(),
		modules          => {
			xapian => EPrints::KlarasConnect::Compat::has_xapian( $repo ) ? \1 : \0,
		},
		has_cron         => EPrints::KlarasConnect::Compat::indexer_running( $repo ) ? \1 : \0,
		# Whether members of other libraries may find this repository in the
		# Klaras directory (open-access files only). Set on the settings screen.
		directory_listed => EPrints::KlarasConnect::Settings::get( $repo, 'directory_listed' ) ? \1 : \0,
	};
}

sub _clean
{
	my( $url ) = @_;
	return '' if !defined $url;
	$url =~ s{/+\z}{};
	return $url;
}

1;
