package LATMOS::Accounts::Web::Controller::Json::AccAge;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

LATMOS::Accounts::Web::Controller::Json::Expire - 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 $graph = {
        data => [],
        series => [],
    };

    my %exists = map { $_ => 1 } $base->search_objects('user', 'active=1');
    my %create = $base->attributes_summary_by_object('user', 'create');
    
    my %dates;
    foreach my $user (keys %create) {
        foreach (@{ $create{$user} }) {
            $_ or next;
            $_ =~ s/-.*//;
            my $status = $exists{$user} ? 'active' : 'inactive';
            $dates{active}{$_} ||= 0;
            $dates{inactive}{$_} ||= 0;
            $dates{$status}{$_} ++;
        }
    }

    foreach my $status ('active', 'inactive') {
        push(@{ $graph->{data} }, [ map { [ $_, $dates{$status}{$_} +0 ] } sort { $a cmp $b } keys %{ $dates{$status} } ]);
        push(@{ $graph->{series} }, { label => $status });
    } 
    
    $c->stash->{data} = $graph;
}



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