package App::IdiotBox::Bucket;

use Moo;

sub fields { return qw(slug name) }

with 'App::IdiotBox::Clonable';

sub name { shift->{name} }
sub slug { shift->{slug} }
sub videos { shift->{videos} }
#has 'name'   => (is => 'rw');
#has 'slug'   => (is => 'rw');
#has 'videos' => (is => 'rw');
has '_video_count' => (is => 'rw');

sub video_count {
    my $self = shift;

    return $self->_video_count if defined $self->_video_count;

    return $self->{video_count} if defined $self->{video_count};

    return $self->videos->count;
}

sub create {
	my ($class, %args) = @_;

	unless ($args{name} && $args{slug}) {
		return (undef, "Please enter a name and a bucket");
	}
	if ($args{name} =~ /^\s+$/ || $args{slug} =~ /^\s+$/) {
		return (undef, "Names/buckets must not be all whitespace");
	}

        $args{slug} =~ s/\s+/-/g;

	return App::IdiotBox::Bucket->new(
		name => $args{name},
		slug => $args{slug},
	);
}

sub update {
	my ($self, %args) = @_;

	unless ($args{name}) {
		return (undef, "Please enter a new name");
	}

	if ($args{name} =~ /^\s+$/) {
		return (undef, "Names must not be all whitespace");
	}

	my $new = $self->clone;

	$new->{name} = $args{name};

	return $new;
}

1;
