package App::bif::show::identity;
use strict;
use warnings;
use Bif::Mo;
use DBIx::ThinSQL qw/sum case coalesce concat qv/;

our $VERSION = '0.1.5_5';
extends 'App::bif::show';

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

    $opts->{id} = $self->uuid2id( $opts->{id} );

    my @data;
    my $now = $self->now;

    my $ref = $db->xhashref(
        select => [
            'i.id',
            concat( qv('u'), 'n.uuid' )->as('uuid'),
            'e.name',
            'i.shortname',
            'cn.contact_id != e.id AS other_contact',
            'cn.name AS contact',
            'e.local',
        ],
        from       => 'identities i',
        inner_join => 'entities e',
        on         => 'e.id = i.id',
        inner_join => 'nodes n',
        on         => 'n.id = i.id',
        inner_join => 'changes c',
        on         => 'c.id = n.first_change_id',
        inner_join => 'entities cn',
        on         => 'cn.id = e.contact_id',
        where      => { 'i.id' => $opts->{id} },
    );

    return $self->err( 'IdentityNotFound', "identity not found: $opts->{id}" )
      unless $ref;

    my $yellow = $self->colours('yellow');

    push(
        @data,
        $self->header(
            $yellow . 'Identity',
            $yellow . $ref->{id},
            $ref->{uuid}
        ),
        $self->header( 'Name',      $ref->{name} ),
        $self->header( 'ShortName', $ref->{shortname} ),
    );

    push( @data, $self->header( 'Contact', $ref->{contact} ), )
      if $ref->{other_contact};

    my @methods = $db->xhashrefs(
        select => [
            'ecm.method', 'ecm.mvalue',
            'ecm.id = e.default_contact_method_id AS preferred',
        ],
        from       => 'entities e',
        inner_join => 'entity_contact_methods ecm',
        on         => 'ecm.entity_id = e.id',
        where      => { 'e.id' => $opts->{id} },
        order_by   => [qw/ ecm.method ecm.mvalue /],
    );

    push(
        @data,
        $self->header(
            '  '
              . ( $ref->{other_contact} ? '  ' : '' )
              . ucfirst( $_->{method} ),
            $_->{mvalue},
            $_->{preferred} ? 'preferred' : ()
        ),
    ) for @methods;

    $self->start_pager;
    print $self->render_table( 'l  l', undef, \@data, 1 );

    print "\n";
    $self->dispatch( 'App::bif::log::identity',
        { opts => { id => $opts->{id} } } );

    return $self->ok( 'ShowIdentity', \@data );
}

1;
__END__

=head1 NAME

=for bif-doc #show

bif-show-identity - display a identity's current status

=head1 VERSION

0.1.5_5 (2015-08-13)

=head1 SYNOPSIS

    bif show identity ID [OPTIONS...]

=head1 DESCRIPTION

The B<bif-show-identity> command displays the characteristics of an
identity.

=head1 ARGUMENTS & OPTIONS

=over

=item ID

An identity ID. Required.

=item --full, -f

Display a more verbose version of the current status.

=item --uuid, -U

Lookup the node using ID as a UUID string instead of a node integer.

=back

=head1 SEE ALSO

L<bif>(1)

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENSE

Copyright 2014-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.

