package App::Counter;
use Moose;
use MooseX::AttributeHelpers;
use MooseX::Attribute::ENV;    

use Counter;

with 'MooseX::Getopt';

has 'document_uri' => (
    traits   => [ 'ENV', 'Getopt' ],
    is       => 'ro',
    isa      => 'Str',   
    cmd_flag => 'uri',
    env_key  => 'DOCUMENT_URI',
    default  => 'root',
);

has 'counter_file' => (
    is      => 'ro',
    isa     => 'Str',   
    lazy    => 1,
    default => sub {
        my $self = shift;
        my $counter_file = $self->document_uri;
        $counter_file =~ s/\/+$//g;
        $counter_file =~ s/^\/+//g;        
        $counter_file =~ s/[^\w]/_/g;
        "${counter_file}.json";         
    },
);    

has 'counter' => (
    is      => 'ro',
    isa     => 'Counter',
    lazy    => 1,   
    default => sub {
        my $self = shift;
        -e $self->counter_file
            ? Counter->load($self->counter_file)
            : Counter->new
    },
    handles => [qw[
        inc_counter
        store
        to_string
        reset_date
    ]]
);

sub run {
    my $self = shift;
    print $self->to_string, "\n";    
    $self->inc_counter;    
    $self->store($self->counter_file);
}


1;