package LATMOS::Accounts::Web::Controller::Remote::Listing;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

LATMOS::Accounts::Web::Controller::Remote::Listing - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;

    my $base = $c->model('Accounts')->db;

    my $otype = $c->req->param('otype');
    my @search = $c->req->param('q');
    my @attrs = $c->req->param('attribute');

    if (!$base->is_supported_object($otype)) {
        $c->res->status('404');
        return;
    }

    my %objects = map { $_ => {} } $c->model('Accounts')->db->search_objects($otype, @search);

    foreach my $attr (@attrs) {
        foreach (keys %objects) {
            $objects{$_}{$attr} = [];
        }
        my %res = $base->attributes_summary_by_object($otype, $attr);
        foreach my $obj (keys %res) {
            exists($objects{$obj}) or next;
            $objects{$obj}{$attr} = $res{$obj};
        }
    }

    $c->stash->{data} = \%objects;
}



=encoding utf8

=head1 AUTHOR

Olivier Thauvin,Guyancourt - B1428,+33 1 80285052,

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

1;
