package App::IdiotBox::Bucket;

use Moo;

sub fields { return qw(slug name) }

with 'App::IdiotBox::Clonable';

sub slug { shift->{slug} }
sub name { shift->{name} }
sub video_count {
  exists $_[0]->{video_count}
    ? $_[0]->{video_count}
    : $_[0]->{videos}->count
}
sub videos { shift->{videos} }

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 bless {
		name => $args{name},
		slug => $args{slug},
	}, $class;
}

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");
	}

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

	$self->clone;
}

1;
