#!/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|c ac|a sn|n);

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

GetOptions($opts,
    'help|h' => sub { usage() },
    'customer_id=i',
    'username|u=s',
    'password|p=s',
    'domain|d=s',
    'admin|s=i',
    'cc|c=s',
    'ac|a=s',
    'sn|n=s',
    'aliases=s@',
    'display_name=s',
    'webpassword|w=s',
    'pbx_pilot',
    'pbx_extension=s',
    'verbose',
) or usage();

sub check_params {
    my @missing;

    foreach my $param (@required) {
        push @missing, $param unless
            grep { defined $opts->{$_} } split /\|/, $param;
    }
    usage(join(' ', @missing)) if scalar @missing;

    return;
}

sub usage {
    my $missing = shift;

    pod2usage(
        -exitval => $missing ? 1 : 0,
        -verbose => 99,
        -sections => 'SYNOPSIS|REQUIRED ARGUMENTS|OPTIONS',
        -message => $missing ? "Missing parameters: $missing" : '',
    );

    return;
}

sub main {

    check_params();

    my $client = NGCP::API::Client->new(verbose => $opts->{verbose});

    # domain_id
    my $dom = $client->request('GET', '/api/domains/?domain=' . $opts->{domain});
    if ($dom->as_hash->{total_count} != 1) {
        die "Domain $opts->{domain} not found\n";
    }

    my $uri = '/api/subscribers/';

    my %data = map {
        $_ => $opts->{$_}
    } qw(customer_id username password webpassword);
    $data{primary_number} = { map { $_ => $opts->{$_} } qw(cc ac sn) };
    $data{administrative} = $opts->{admin};
    $data{is_pbx_pilot} = $opts->{pbx_pilot};
    $data{display_name} = $opts->{display_name} // undef;
    $data{pbx_extension} = $opts->{pbx_extension} // undef;
    my $tmp = $dom->as_hash->{_embedded}->{'ngcp:domains'};
    if (ref $tmp eq 'ARRAY') {
        $data{domain_id} = @{$tmp}[0]->{id};
    } else {
        $data{domain_id} = $tmp->{id};
    }

    if ($opts->{aliases}) {
        $data{alias_numbers} = [
            map {
                    {
                        map {
                            split /\=/, $_
                        } split /\s+/, $_
                    }
            } @{$opts->{aliases}}
        ];
    }

    my $res = $client->request('POST', $uri, \%data);
    print $res->result . "\n";

    return !$res->is_success;
}

exit main();

__END__

=head1 NAME

ngcp-create-subscriber - create a subscriber

=head1 SYNOPSIS

B<ngcp-create-subscriber> [I<options>...] I<required-arguments>...

=head1 DESCRIPTION

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

=head1 REQUIRED ARGUMENTS

=over 8

=item B<--customer_id> I<cid>

An existing customer id to assign this subscriber to.

=item B<-u>, B<--username> I<username>

A SIP username.

=item B<-d>, B<--domain> I<domain>

An existing domain for the new subscriber.

=item B<-p>, B<--password> I<password>

An unencrypted SIP password for the new subscriber.

B<Note:> using this option is a security hole, as it will be visible to
other users at least from the /proc filesystem.

=item B<-w>, B<--webpassword> I<password>

An unencrypted web password for the new subscriber.

B<Note:> using this option is a security hole, as it will be visible to
other users at least from the /proc filesystem.

=item B<-c>, B<--cc> I<country-code>

A country code part of the subscriber's number.

=item B<-a>, B<--ac> I<area-code>

An area code part of the subscriber's number.

=item B<-n>, B<--sn> I<sub-number>

A local number part of the subscriber's number.

=back

=head1 OPTIONS

=over 8

=item B<-s>, B<--admin>

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

=item B<--display> I<display-name>

A display name part of the subscriber's number.

=item B<--aliases>

A list of alias numbers

    Format: --aliases "cc=43 ac=1 sn=123" --aliases "cc=43 ac=1 sn=456"
    Optional:
        is_devid=1
        devid_alias=Alice


=item B<--pbx_pilot>

Set the "pbx pilot" flag for the new subscriber.
Defaults to 0 (no).

=item B<--pbx_extension>

PBX extension number (only usable for PBX subscribers)

=item B<--verbose>

Show additional debug information. Default false.

=item B<--help>

Print a brief help message.

=back

=head1 EXIT STATUS

Exit code 0 means everything is ok otherwise 1.

=head1 SEE ALSO

NGCP::API::Client

=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

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