package App::IdiotBox::Video;

use Moo;

sub fields { return qw(slug bucket_slug name author details announcement_id) }

with 'App::IdiotBox::Clonable';

sub announcement_id { shift->{announcement_id} }
sub bucket_slug { shift->{bucket_slug} }
sub slug { shift->{slug} }
sub name { shift->{name} }
sub author { shift->{author} }
sub details { shift->{details} }
sub bucket { shift->{bucket} }
sub file_name {
  (my $s = join(' ', @{+shift}{qw(author name)})) =~ s/ /-/g;
  $s;
}
sub url_path {
  join('/', $_[0]->bucket->slug, $_[0]->slug);
}

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

	my %update = map { $_ => delete $args{$_} } grep { exists $args{$_} } $self->fields;

	if (%args) {
		return (undef, "Unknown fields in update: " . join(',', keys %args));
	}

	unless (%update) {
		return (undef, "No changes requested");
	}

	for my $k (qw(slug bucket_slug name author details)) {
		my @bad;

		if (exists $update{$k} && $update{$k} =~ /^\s+$/) {
			push @bad, $k;
		}

		return (undef, "Empty fields in update: ", join(',', @bad)) if @bad;
	}

	for my $k (qw(slug bucket_slug)) {
		$update{$k} =~ s/\s+/-/g if exists $update{$k};
	}

	my $new = $self->clone;

	$new->{$_} = $update{$_} for keys %update;

	return $new;
}

1;
