package LasTres::EnemyTeam;

use v5.36.0;
use strict;
use warnings;

use feature 'signatures';

use Moo;
use LasTres::Enemy;

use UUID::URandom qw/create_uuid_string/;

has uuid => (
    is      => 'rw',
    default => sub ($self) {
        return create_uuid_string();
    },
);

has members => ( is => 'rw', );

has current_battle => (
    is       => 'rw',
    required => 1,
);

sub is_defeated ($self) {
    my @members       = @{ $self->combat_members };
    my @alive_members = grep { $_->health > 0 } @members;
    if ( !scalar @alive_members ) {
        return 1;
    }
    return 0;
}

sub update ($self) {
    return $self;
}

sub on_lose_combat ($self) {
}

sub on_win_combat ($self) {
}

sub combat_members ($self) {
    return $self->members;
}

sub combat_members_serializable($self) {
    my @members_hash = map { $_->to_hash_display } $self->members->@*;
    return \@members_hash;
}

sub to_serializable ($self) {
    return {
        members        => [ map { $_->to_hash } @{ $self->members } ],
        current_battle => $self->current_battle,
        uuid           => $self->uuid,
    };
}

sub on_end_combat($self) {
}

sub from_serializable ( $class, $hash ) {
    if ( $hash->{is_db} ) {
        die "This is not an Enemy Team but a DB Team.";
    }
    my $hash_members   = $hash->{members};
    my $current_battle = $hash->{current_battle};
    my $uuid           = $hash->{uuid};
    my $members = [ map { LasTres::Enemy->from_hash($_) } @$hash_members ];
    my $self    = $class->new(
        members        => $members,
        current_battle => $current_battle,
        uuid           => $uuid
    );
    for my $member ( $self->members->@* ) {
        $member->team($self);
    }
    return $self;
}

with 'LasTres::RedisTeam';
1;
