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

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

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

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut

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

    $c->stash->{data} = [
        $c->model('Accounts')->db->list_supported_objects
    ];
}

sub List :Path :Args(1) {
    my ($self, $c, $otype) = @_;

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

    $c->stash->{data} = [
        $c->model('Accounts')->db->list_objects($otype)
    ]
}

sub Show :Path :Args(2) {
    my ($self, $c, $otype, $id) = @_;

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

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

    my $obj = $base->get_object($otype, $id);

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

    foreach my $attr (sort { $a cmp $b } $base->list_canonical_fields($otype, 'r')) {
        my $oattr = $obj->attribute($attr) or next;
        if ($oattr->hidden) { next; }
        my $val = $obj->get_c_field($attr);
        $c->stash->{data}{$attr} = $val if ($val);
    } 
}

=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;
