#!/usr/bin/perl
use strict;
use warnings;
use English;
use Getopt::Long;
use Pod::Usage;
use NGCP::API::Client;
use Readonly;

Readonly my @required => qw(customer_id username domain password cc ac sn);

my $opts = {
    admin   => 0,
    verbose => 0,
};

GetOptions( $opts,
    "help|h" => \&usage,
    "customer_id=i",
    "username|u=s",
    "password|p=s",
    "domain|d=s",
    "admin|s=i",
    "cc|c=i",
    "ac|a=i",
    "sn|n=i",
    "webpassword|w=s",
    "verbose",
) or usage();

sub check_params {
    my @missing;
    foreach my $param (@required) {
        push @missing, $param unless $opts->{$param};
    }
    usage(join(' ', @missing)) if scalar @missing;
    return;
}

sub usage {
    my $msg = shift;
    pod2usage(-exitval => $msg ? 1 : 0,
              -verbose => 99,
              -sections => [ qw(NAME OPTIONS USAGE) ],
              -message => $msg
                            ? $msg =~ /not found/i
                                ? $msg
                                : "Missing parameters: $msg"
                            : '',
             );
    return;
}

sub main {
    check_params();
    my $uri = '/api/subscribers/';
    my %data = map { $_ => $opts->{$_} }
                    qw(customer_id username password webpassword);
    $data{primary_number} = { map { $_ => $opts->{$_} } @{$opts}{qw(cc ac sn)} };
    $data{administrative} = $opts->{admin};
    my $client = new NGCP::API::Client;
    $client->set_verbose($opts->{verbose});
    # domain_id
    my $dom = $client->request("GET", "/api/domains/?domain=".$opts->{domain});
    if ($dom->as_hash->{total_count} == 1) {
        $data{domain_id} = $dom->as_hash->{_embedded}->{'ngcp:domains'}->{id};
    } else {
        usage("Domain not found");
    }
    my $res = $client->request("POST", $uri, \%data);
    print $res->result."\n";

    exit !$res->is_success;

    return;
}

main();

exit 0;

__END__

=head1 NAME

ngcp-create_subscriber - create a subscriber

=head1 OPTIONS

=over 8

=item B<-help>

Print a brief help message.

=item B<-customer_id>

An existing customer id to assign this subscriber to.

=item B<-username|u>

A SIP username.

=item B<-domain|d>

An existing domain for the new subscriber.

=item B<-password|p>

An unencrypted SIP password for the new subscriber.

=item B<-webpassword|w>

An unencrypted web password for the new subscriber.

=item B<-cc|c>

A country code part of the subscriber's number.

=item B<-ac|a>

An area code part of the subscriber's number.

=item B<-number|n>

A local number part of the subscriber's number.

=item B<-admin|s>

Set the administrative flag for the new subscriber.
Defaults to 0 (no).

=item B<-verbose>

Show additional debug information. Default false.

=back

=head1 USAGE

ngcp-create_subscriber [options]

ngcp-create_subscriber --customer_id 1 --username 4311101 --domain sipwise.com --password SecurePassword1 --cc 43 --ac 111 --sn 01

=head1 DESCRIPTION

B<This program> creates a subscriber on the NGCP platform.

=head1 REQUIRED ARGUMENTS

=over 8

=item B<-customer_id>

=item B<-username>

=item B<-domain>

=item B<-password>

=item B<-cc>

=item B<-ac>

=item B<-sn>

=back

=head1 EXIT STATUS

Exit code 0 means everything is ok otherwise 1.

=head1 CONFIGURATION

=head1 DIAGNOSTICS

=head1 SEE ALSO

NGCP::API::Client

=head1 DEPENDENCIES

ngcp-create_customer relies on a bunch of Perl modules, all of them specified as
dependencies through the Debian package.

=head1 INCOMPATIBILITIES

No known at this time.

=head1 BUGS AND LIMITATIONS

Please report problems you notice to the Sipwise
Development Team <support@sipwise.com>.

=head1 AUTHOR

Victor Seva <vseva@sipwise.com>

=head1 LICENSE AND COPYRIGHT

Copyright (C) 2016 Sipwise GmbH, Austria

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

=cut
