#!/usr/bin/perl

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

sub help {
	return qq{
	Prints list of facility Owners. Facility is required field.
	-----------------------------------------------------------
	Available options:
	--facilityId   | -f  facility idetifier
	--facilityName | -F  facility name
	--orderById    | -i  order by numeric ID
	--orderByName  | -n  order by name
	--batch        | -b  batch
	--help         | -h  prints this help
	};
}

our $batch;
my ($facilityId, $facilityName, $facilityType, $sortingFunction);
GetOptions("help|h" => sub { print help; exit 0;} ,
					"facilityId|f=i" => \$facilityId,
					"facilityName|F=s" => \$facilityName,
					"orderById|i" => sub { $sortingFunction = getSortingFunction("getId") } ,
					"orderByName|n" =>  sub {$sortingFunction = getSortingFunction("getName", 1) },
					"batch|b" => \$batch) || die help;

#options check
			 unless(defined $facilityId or (defined $facilityName)) { die "ERROR: facilityId or facilityName is required\n";}
			 unless(defined $sortingFunction) { $sortingFunction = getSortingFunction("getName", 1); }

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

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

			 my @owners = $facilitiesAgent->getOwners(facility => $facilityId);
			 unless(@owners) { printMessage "No owner found", $batch; exit 0;}


			 printTable $sortingFunction, @owners;
