#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case);
use Perun::Agent;
use Perun::Common qw(printMessage);

sub help {
	return qq{
	Adds facility owner. Facility and owner are required fields.
	--------------------------------------
	Available options:
	--facilityId   | -f facility id
	--facilityName | -F facility name
	--ownerId      | -o owner id
	--batch        | -b batch
	--help         | -h prints this help

	};
}

my ($facilityId, $facilityName, $ownerId, $batch);
GetOptions ("help|h"   => sub {
		print help();
		exit 0;
	}, "batch|b"       => \$batch,
	"facilityId|f=i"   => \$facilityId,
	"facilityName|F=s" => \$facilityName,
	"ownerId|o=i"      => \$ownerId) || die help();

# Check options
unless (defined($facilityId) or (defined($facilityName))) { die "ERROR: facilityId or facilityName is required \n";}
unless (defined($ownerId)) { die "ERROR: ownerId is required \n";}

my $agent = Perun::Agent->new();
my $facilitiesAgent = $agent->getFacilitiesAgent;

unless ($facilityId) {
	my $facility = $facilitiesAgent->getFacilityByName( name => $facilityName );
	$facilityId = $facility->getId;
}

$facilitiesAgent->addOwner( facility => $facilityId, owner => $ownerId );

printMessage("Owner Id:$ownerId successfully added to the facility Id:$facilityId", $batch);
