package App::bif::new::hub;
use strict;
use warnings;
use Bif::Mo;
use IO::Prompt::Tiny qw/prompt/;

our $VERSION = '0.1.5_3';
extends 'App::bif';

sub run {
    my $self = shift;
    my $opts = $self->opts;
    my $dbw  = $self->dbw;

    $opts->{name} ||= prompt( 'Name:', '' )
      || return $self->err( 'NameRequired', 'name is required' );

    $opts->{title} ||= prompt( 'Title:', '' )
      || return $self->err( 'TitleRequired', 'title is required' );

    $opts->{message} ||= "New hub $opts->{name}";

    $dbw->txn(
        sub {
            $opts->{id}        = $dbw->nextval('topics');
            $opts->{change_id} = $self->new_change;

            $self->new_hub;

            my $path = $dbw->xval(
                select => 'path',
                from   => 'topic_path tp',
                where  => { 'tp.id' => $opts->{id} },
            );

            $self->end_change(
                id                => $opts->{change_id},
                message           => $opts->{message},
                action_format     => "new hub $path (%s)",
                action_topic_id_1 => $opts->{id},
            );

            printf( "Hub created: %s\n", $opts->{name} );

        }
    );

    return $self->ok('NewHub');
}

sub new_hub {
    my $self = shift;
    my $opts = $self->opts;
    my $dbw  = $self->dbw;

    $self->new_cmd(
        'App::bif::new::project',
        opts => {
            change_id => $opts->{change_id},
            id        => $opts->{id},
            name      => $opts->{name},
            title     => $opts->{title},
        }
    )->new_project;

    $dbw->xdo(
        insert_into => 'func_new_hub',
        values      => {
            change_id   => $opts->{change_id},
            id          => $opts->{id},
            set_default => $opts->{default},
        },
    );

    my $i = 0;
    foreach my $location ( @{ $opts->{locations} } ) {
        my $rid = $dbw->nextval('topics');

        $dbw->xdo(
            insert_into => 'func_new_hub_repo',
            values      => {
                id        => $rid,
                change_id => $opts->{change_id},
                hub_id    => $opts->{id},
                location  => $location,
            },
        );

        $dbw->xdo(
            update => 'hubs',
            set    => {
                default_repo_id => $rid,
            },
            where => {
                id => $opts->{id},
            },
        ) unless $i++;
    }

    return 1;
}

1;
__END__

=head1 NAME

=for bif-doc #hubadmin

bif-new-hub - create a new hub in the repository

=head1 VERSION

0.1.5_3 (2015-06-30)

=head1 SYNOPSIS

    bif new hub [NAME] [LOCATIONS...] [OPTIONS...]

=head1 DESCRIPTION

The B<bif-new-hub> command creates a new hub representing an a project
organisation.

=head1 ARGUMENTS & OPTIONS

=over

=item NAME

The name of the hub.

=item LOCATIONS

The locations of the hub repositories.

=item --default

Mark this hub as local/default.

=item --message, -m MESSAGE

The creation message, set to "" by default.

=back

=head1 SEE ALSO

L<bif>(1)

=head1 AUTHOR

Mark Lawrence E<lt>nomad@null.netE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2013-2015 Mark Lawrence <nomad@null.net>

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.

