#!/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{
	Removes selected destination from the facility. Facility id or name, attribute id and destination and type are required.
	--------------------------------------
	Available options:
	--facilityId   | -f facility id
	--facilityName | -F facility name
	--serviceId    | -s service id
	--destination  | -D destination
	--type         | -t destination type
	--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, "serviceId|s=i" => \$serviceId,
 "facilityName|F=s" => \$facilityName,
 "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->removeDestination(facility => $facilityId, service => $serviceId, destination => $destination, type => $typ);

printMessage("Destination:$destination removed from the facility Id:$facilityId and service Id:$serviceId", $batch);
