package LIMS::Model::ErrorCode;

use base 'LIMS::Model::Base';
use strict;

use Data::Dumper;

#-------------------------------------------------------------------------------
sub get_error_code {
    my ($self, $code_id) = @_; # $self->debug($code_id);

    my $data = LIMS::DB::ErrorCode->new(id => $code_id)->load;

    return $data;
}

#-------------------------------------------------------------------------------
sub get_report_error_code {
    my ($self, $code_id) = @_; # $self->debug($code_id);

    my $data = LIMS::DB::ReportErrorCode->new(id => $code_id)->load;

    return $data;
}

#-------------------------------------------------------------------------------
sub check_error_code {
    my ($self, $code) = @_; # $self->debug($code_id);

    my $data = LIMS::DB::ErrorCode->new(code => $code)->load(speculative => 1);

    return $data;
}

#-------------------------------------------------------------------------------
sub get_error_codes {
    my $self = shift;
    my $args = shift || {};

    my %args = (
        query   => [ %$args ], # ok for $args to be empty
        sort_by => 'code',
    );
    # get all error_code rows as arrayref:
    my $data = LIMS::DB::ErrorCode::Manager->get_error_codes(%args); # DEBUG $data;

    return $data;
}

#-------------------------------------------------------------------------------
sub get_error_code_assignment {
    my $self = shift;
    my $type = shift;

    my %args = (
        query => [
            type => $type,
        ],
        require_objects => 'error_code',
    );
    
    my $error_codes
        = LIMS::DB::ErrorCodeAssignment::Manager->get_error_code_assignment(%args);
    
    return $error_codes;
}
    
#-------------------------------------------------------------------------------
sub get_report_error_codes {
    my $self = shift;
    my $args = shift || {};

    my %args = (
        query   => [ %$args ], # ok for $args to be empty
        sort_by => 'code',
    );
    # get all report_error_code rows as arrayref:
    my $data = LIMS::DB::ReportErrorCode::Manager->get_report_error_codes(%args); # DEBUG $data;

    return $data;
}

#-------------------------------------------------------------------------------
sub update_error_codes {
    my $self = shift;
    my $data = shift; # $self->debug( $data );
   
    my %args = ( class => 'ErrorCode', data  => $data );
    
    return $self->update_object(\%args);
}

#-------------------------------------------------------------------------------
sub update_error_code_assignments {
    my $self = shift;
    my $data = shift; # $self->debug( $data );

    my $db = $self->lims_db; # ie LIMS::DB->new_or_cached;

    my $type = $data->{type};

    my @error_code_ids = ref $data->{error_code_id} eq 'ARRAY'
        ? @{ $data->{error_code_id} } # if > 1 entry submitted
        : ( $data->{error_code_id} ); # if only 1 entry submitted
        # warn Dumper \@error_code_ids;
    my $update = sub {
        # first clear existing entries for type:
        LIMS::DB::ErrorCodeAssignment::Manager->delete_error_code_assignment(
            where => [ type => $type ],
        );
        # new entry for each error_code_id:
        for my $id (@error_code_ids) { # warn $id;
            next unless defined $id; # will be single undef value if no codes submitted
            LIMS::DB::ErrorCodeAssignment->new(
                type => $type,
                error_code_id => $id,
            )->save;
        }
    };
    
    my $ok = $db->do_transaction($update);

    # don't need return value unless error:
    return $ok ? 0 : 'update_error_codes_config() error - ' . $db->error;    
}

#-------------------------------------------------------------------------------
sub update_report_error_codes {
    my $self = shift;
    my $data = shift; # $self->debug( $data );
   
    my %args = ( class => 'ReportErrorCode', data  => $data );
    
    return $self->update_object(\%args);
}

1;
