#!/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{
	Creates an attribute. Attribute name, namespace and type are required fields.
	--------------------------------------
	Available options:
	--attributeName       | -N attribute friendly name
	--attributeNameSpace  | -n namespace (e.g.: urn:perun:user:attribute-def:def)
	--attributeDisplayName| -e attribute display name
	--attributeType       | -t attribute type (integer/string/array/hash/boolean)
	--attributeDsc        | -d description
	--isUnique            | -u attribute have to be unique
	--batch               | -b batch
	--help                | -h prints this help

	};
}

my ($attributeName, $attributeDisplayName, $attributeNameSpace, $attributeType, $attributeDsc, $unique, $batch);
GetOptions ("help|h"         => sub {
		print help();
		exit 0;
	}, "batch|b"             => \$batch,
	"attributeName|N=s"      => \$attributeName, "attributeDisplayName|e=s" => \$attributeDisplayName,
	"attributeNameSpace|n=s" => \$attributeNameSpace, "attributeType|t=s" => \$attributeType,
	"attributeDsc|d=s"       => \$attributeDsc,
	"isUnique|u" => \$unique) || die help();

# Check options
unless (defined($attributeName)) { die "ERROR: attributeName is required \n";}
unless (defined($attributeNameSpace)) { die "ERROR: attributeNameSpace is required \n";}
unless (defined($attributeType)) { die "ERROR: attributeType is required \n";}

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

my $attribute = Perun::beans::AttributeDefinition->new;
$attribute->setFriendlyName( $attributeName );
$attribute->setNamespace( $attributeNameSpace );
$attribute->setDisplayName( $attributeDisplayName );
$attribute->setType( $attributeType );
$attribute->setDescription( $attributeDsc ) if (defined($attributeDsc));
$attribute->setUnique( $unique ) if (defined($unique));

$attribute = $attributesAgent->createAttribute( attribute => $attribute );

printMessage("Attribute Id:".$attribute->getId." successfully created", $batch);
