#!/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 destination. Facility, service and destination and type are required fields.
	--------------------------------------
	Available options:
	--facilityId   | -f facility id
	--facilityName | -F facility name
	--serviceId    | -s service id
	--destination  | -D destination string
	--type         | -t destination type (host/email/url)
	--batch        | -b batch
	--help         | -h prints this help

	};
}

my ($facilityId, $facilityName, $serviceId, $destination, $typ, $batch);
GetOptions ("help|h" => sub { print help(); exit 0;} ,"batch|b" => \$batch,
"facilityId|f=i" => \$facilityId,
"facilityName|F=s" => \$facilityName,
"serviceId|s=i" => \$serviceId,
"destination|D=s" => \$destination,
"type|t=s" => \$typ) || die help();

# Check options
unless (defined($facilityId) or (defined($facilityName))) { die "ERROR: facilityId or facilityName is required \n";}
unless (defined($serviceId)) { die "ERROR: serviceId is required \n";}
unless (defined($destination)) { die "ERROR: destination is required \n";}
unless (defined($typ)) { die "ERROR: destination type is required \n";}
unless ($destination !~ /^\s*$/) { die "ERROR: destination cannot be empty string\n";}

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

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

my $servicesAgent = $agent->getServicesAgent;

$servicesAgent->addDestination(facility => $facilityId, service => $serviceId, destination => $destination, type => $typ);

printMessage("Destination '$destination' successfully added for the service Id:$serviceId on the facility Id:$facilityId", $batch);
