package LIMS::Model::HMRN;

use Moose;
with (
	'LIMS::Model::HMRN::Data',
	'LIMS::Model::Roles::DBIxSimple',
	'LIMS::Model::Roles::QueryFormatter',
	'LIMS::Model::HMRN::PrognosticIndicator',
);
extends 'LIMS::Model::Base';
use MooseX::AttributeHelpers;
use namespace::clean -except => 'meta';

has _patient_id => (is => 'rw', isa => 'Int'); # set in get_all_hmrn_data()

has actions => (
    is         => 'ro',
    isa        => 'ArrayRef[Str]',
    default    => sub { [] },
    lazy    => 1,
	traits  => ['Array'],
	handles => {
		add_to_actions => 'push',
		all_actions    => 'elements',
	},
);

__PACKAGE__->meta->make_immutable;

use LIMS::Local::Utils;
use Data::Dumper;

# for Roles (HMRN::Data & HMRN::PrognosticIndicator):
sub patient_id { return shift->_patient_id }

# ------------------------------------------------------------------------------
sub has_data {
    my ($self, $patient_id) = @_; # warn $patient_id;
    
	my $dbix = $self->lims_dbix;
    
    # minimum data set for hmrn is mdt or chronology:
    for ( qw/chronologies mdt_dates/ ) {
        my $tbl = 'hmrn.patient_' . $_;        
        return 1 if $dbix->query( "SELECT 1 FROM $tbl WHERE patient_id = ?",
            $patient_id )->list;
    }
    return 0;
}

# ------------------------------------------------------------------------------
sub get_all_hmrn_data {
    my $self = shift;
	my $args = shift;
	
	# my $patient_id = $args->{patient_id}; # warn $patient_id;
	my $vars = $args->{vars}; # supplied if tx_details required
	$self->_patient_id($args->{patient_id});
    
    # switch db:
    # $dbix->dbh->do('use hmrn'); # needs reversing or get fatal error downstream
    
    my $data = { # initialise following keys:
		maps   => {},      # options for drop-down menus
		params => {        # data params from categories:
			myeloid => {}, lymphoid => {}, plasmacell => {}, precursor => {},
			staging => {}, # nodal, extranodal & extramedullary involved sites
			indices => {}, # calculated progrostic indicators (IPI, FLIPI, etc)
			comment => {}, # free-text comment			
		},
		chronology => {},  # dates (diagnosis, first appointment, etc)
		antecedent => {},  # antecedent events
		treatments => [],  # list of treatment episodes
		category_has_data => {}, # which dataset(s) we have eg myeloid => nn
		all_diagnoses => [], # list of all (ICDO3+) diagnoses for this patient
	};
	
	# get all this patients diagnoses (with an ICDO3 designation):
	$self->all_diagnoses($data);
	# chronological data (diagnosed, first appointment, etc):
	$self->chronologies($data);
	# dates of MDT meetings:
	$self->mdt_dates($data);
	# antecedent events & previous radio & chemotherapy:
	$self->antecedent_events($data);
	# treatment history:
	$self->treatment($data);
	# laboratory, physiological, etc parameters:
	$self->data_params($data);
	# which dataset(s) do we have:
	$self->dataset_type($data);
	# staging data (nodal & extranodal site involvements):
	$self->staging_data($data);
	# calculated prognostic indices (IPI, Ann Arbor, etc):
	$self->calculated_indices($data);
	# comments:
	$self->comments($data);
    { # get select option maps (pass vars in case tx_details req'd):
		my $select_option_data = $self->_get_select_option_data($vars); 
		$data->{maps} = $select_option_data;
	}
	
	return $data;
}

#-------------------------------------------------------------------------------
sub get_mdt_data {
	my ($self, $id) = @_;
	
	my $dbix = $self->lims_dbix;
	
	my $data
		= $dbix->query('select * from hmrn.patient_mdt_dates where id = ?', $id)->hash;

	# inflate 'date' val to DT object:
	$self->inflate_mysql_dates_to_datetime($data, ['date']);

	return $data;
}

#-------------------------------------------------------------------------------
sub get_treatment_data {
	my ($self, $id) = @_;
	
	my $dbix = $self->lims_dbix;
	
	my $data
		= $dbix->query('select * from hmrn.patient_treatment where id = ?', $id)->hash;

	# inflate dates:
	$self->inflate_mysql_dates_to_datetime($data, [ qw(start_date end_date) ]);
	
    { # get select option maps (don't need antecedent data, but all rest req'd):
		my $select_option_data # supply tx_type_id for select menu:
			= $self->_get_select_option_data({ tx_type_id => $data->{tx_type_id} });
		$data->{maps} = $select_option_data;
	}

	return $data;
}

#-------------------------------------------------------------------------------
sub get_defaults_and_ranges {
	my ($self, $param) = @_;
	
	my $dbix = $self->lims_dbix;

	my $sql = $self->sql_lib->retr('hmrn_defaults_and_ranges');

	my $data = $dbix->query($sql, $param)->hash;
	return $data;
}
	
#-------------------------------------------------------------------------------
sub get_parameter_constraints {
	my $self = shift;
	
	my $dbix = $self->lims_dbix;

	my $sql = $self->sql_lib->retr('hmrn_parameter_constraints');

	my $constraints = $dbix->query($sql)->map_hashes('param_name');
	
	return $constraints;
}

#-------------------------------------------------------------------------------
sub update_params {
    my $self = shift;
    my $args = shift; # warn Dumper $args; 
    
    my $patient_id = $args->{patient_id};
    my $form_data  = $args->{data}; # warn Dumper $form_data;
    my $data_type  = $args->{data_type};   
    
	my $dbix = $self->lims_dbix;

    my $params_map;
    { # get param_names for data type:
        my $sql = $self->sql_lib->retr('hmrn_category_params');
        $params_map = $dbix->query($sql, $data_type)->map; # warn Dumper $params_map; 
    }
    
    # get existing data:
    my $sql = $self->sql_lib->retr('hmrn_category_patient_params');
    my $data = $dbix->query($sql, $patient_id, $data_type)->map; # warn Dumper $data;
    
	my $table = 'hmrn.patient_params';
	
	my $db = $self->lims_db; # ie LIMS::DB->new_or_cached;

	my $tx = sub {
		if (%$data) { # update of existing:
			no warnings 'uninitialized';
			PARAM:
			while ( my ($param_name, $param_id) = each %$params_map ) {
				my $new = $form_data->{$param_name}; # warn Dumper $new;
				my $old = $data->{$param_name};      # warn Dumper $old;
				
				next PARAM if $new eq $old; # both same or both null
				
				my %params = (
					patient_id => $patient_id,
					param_id   => $param_id,
				); # need to test for defined values to handle zeros:
				if (defined $new && ! defined $old) { # add new param:
					$dbix->insert($table, { %params, result => $new });
					$self->add_to_actions("added new $param_name value");
				}
				elsif (defined $old && ! defined $new) { # delete record:
					$dbix->delete($table, \%params);                
					$self->add_to_actions("deleted $param_name value");
				}
				else { # $new ne $old, so update result:
					$dbix->update($table, { result => $new }, \%params );
					$self->add_to_actions(
						"updated $param_name value [$old -> $new]"
					);
				}
			}        
		}
		else { # new data:
			PARAM:
			while ( my ($param_name, $param_id) = each %$params_map ) {
				defined $form_data->{$param_name} || next PARAM; # maybe '0'
				
				my %data = (
					patient_id => $patient_id,
					param_id   => $param_id,
					result 	   => $form_data->{$param_name},
				); # warn Dumper \%data;
				$dbix->insert($table, \%data);
			}
			$self->add_to_actions("added new $data_type data set");
		}
		$self->do_history($patient_id);
	};
	
	my $ok = $db->do_transaction($tx); 
	
	# don't need return value unless error:
    return $ok ? 0 : 'update_params_data() error - ' . $db->error;		
}

#-------------------------------------------------------------------------------
sub new_treatment_data {
	my $self = shift;
	my $args = shift; # warn Dumper $args;
	
	my $tbl = 'hmrn.patient_treatment';
	
	my $meta = $self->get_meta($tbl); # warn Dumper $meta;
	
	my @cols = grep { $_ !~ /\A(id|timestamp)\Z/ } keys %$meta; # warn Dumper \@cols;
	my @date_cols = grep { $meta->{$_}->{type} eq 'date' } keys %$meta; # warn Dumper \@date_cols;

	my %data = map {
		$_ => $args->{$_};
	} grep $args->{$_}, @cols;
	
	map { # transform EU dates to MySQL format:
		$data{$_} = LIMS::Local::Utils::date_to_mysql($data{$_})
	} @date_cols; # warn Dumper \%data;
	
	my $dbix = $self->lims_dbix;
	
	my $db = $self->lims_db; # ie LIMS::DB->new_or_cached;

	my $new = sub {
		$dbix->insert($tbl, \%data);
		$self->add_to_actions('added new treatment dataset');
		$self->do_history($data{patient_id});
	};
	
	my $ok = $db->do_transaction($new); 
	
	# don't need return value unless error:
    return $ok ? 0 : 'new_treatment_data() error - ' . $db->error;	
}

#-------------------------------------------------------------------------------
sub get_tx_type_map {
	my $self = shift;

	my $dbix = $self->lims_dbix;

	my $map = $dbix->query('select description, id from hmrn.treatment_types')->map;
	
	return $map;
}

#-------------------------------------------------------------------------------
sub get_history {
    my ($self, $id) = @_;
    
    my $dbix = $self->lims_dbix;
    
    my $sql = q!select * from hmrn.history h join users u on h.user_id = u.id
        where patient_id = ? order by time!;
    my $data = $dbix->query($sql, $id)->hashes;
    
    $self->inflate_mysql_timestamp_to_datetime($_, ['time']) for @$data;
    
    return $data;
}

#-------------------------------------------------------------------------------
sub update_treatment_data {
	my $self = shift;
	my $args = shift; # warn Dumper $args;
	
	my $tbl = 'hmrn.patient_treatment';
	
	my $meta = $self->get_meta($tbl); # warn Dumper $meta;
	
	# get patient_treatment data cols (ie not PK, timestamp, patient_id):	
	my @cols = grep { $_ !~ /\A(id|patient_id|timestamp)\Z/ } keys %$meta; # warn Dumper \@cols;
	# date cols:
	my @date_cols = grep $meta->{$_}->{type} eq 'date', keys %$meta; # warn Dumper \@date_cols;
	
	$args->{$_} = LIMS::Local::Utils::to_datetime_using_datecalc($args->{$_}) 
		for grep $args->{$_}, @date_cols; # warn Dumper $args;

	my $dbix = $self->lims_dbix;

	my $data = $dbix->query("select * from $tbl where id = ?", $args->{id})->hash;
	$self->inflate_mysql_dates_to_datetime($data, \@date_cols); # inflate $data dates
	
	my $db = $self->lims_db; # ie LIMS::DB->new_or_cached;

	my $update = sub {
		my @updates = ();
		# non-date cols:
		COL: for my $col(@cols) {
			next COL if $meta->{$col}->{type} eq 'date'; # skip date cols - done later
			
			my $new = $args->{$col};
			my $old = $data->{$col};
			
			if ($new && $old && $new ne $old) { # changed
				push @updates, ( $col => $new );
				$self->add_to_actions("updated treatment $col [id = $args->{id}]");
			}
			elsif ($new && ! $old) {
				push @updates, ( $col => $new );
				$self->add_to_actions("added new treatment $col [id = $args->{id}]");
			}
			elsif ($old && ! $new) {
				push @updates, ( $col => undef );
				$self->add_to_actions("deleted treatment $col [id = $args->{id}]");
			}
			else { next COL } # both same or null so skip
		}
		# date cols:
		COL: for my $col(@date_cols) {
			my $new = $args->{$col}; # warn $new->datetime
			my $old = $data->{$col}; # warn $old->datetime
			
			if ($new && $old) {
				next COL unless DateTime->compare($old, $new); # true if different
				push @updates, ( $col => $new->ymd );
				$self->add_to_actions("updated treatment $col [id = $args->{id}]");
			}
			elsif ($new && ! $old) {
				push @updates, ( $col => $new->ymd );
				$self->add_to_actions("added new treatment $col [id = $args->{id}]");
			}
			elsif ($old && ! $new) {
				push @updates, ( $col => undef );
				$self->add_to_actions("deleted treatment $col [id = $args->{id}]");
			}
			else { next COL } # both null so skip
		}
		
		if (@updates) { warn Dumper \@updates; warn Dumper [$self->all_actions];
			#$dbix->update($tbl, { @updates }, { id => $args->{id} });
			#$self->do_history($data->{patient_id});
		}
	};
	
	my $ok = $db->do_transaction($update); 
	
	# don't need return value unless error:
    return $ok ? 0 : 'update_treatment_data() error - ' . $db->error;
}

#-------------------------------------------------------------------------------
sub update_mdt_date {
	my $self = shift;
	my $args = shift; # warn Dumper $args;
	
	my $date = LIMS::Local::Utils::to_datetime_using_datecalc($args->{date});
	
	my $tbl = 'hmrn.patient_mdt_dates';
	
	my $dbix = $self->lims_dbix;
	
	my $patient_id
		= $dbix->query("select patient_id from $tbl where id = ?", $args->{id})->list;
	
	my $db = $self->lims_db; # ie LIMS::DB->new_or_cached;

	my $update = sub {
		$dbix->update('hmrn.patient_mdt_dates',
			{ date => $date->ymd },
			{ id => $args->{id} }
		);
		$self->add_to_actions("updated mdt date [id = $args->{id}]");
		$self->do_history($patient_id);
	};
	
	my $ok = $db->do_transaction($update);
	
	# don't need return value unless error:
    return $ok ? 0 : 'update_mdt_date() error - ' . $db->error;
}

#-------------------------------------------------------------------------------
sub delete_mdt_date {
	my ($self, $id) = @_;
	
	my $dbix = $self->lims_dbix;

	my $tbl = 'hmrn.patient_mdt_dates';
	
	my $patient_id
		= $dbix->query("select patient_id from $tbl where id = ?", $id)->list;
	
	my $db = $self->lims_db; # ie LIMS::DB->new_or_cached;

	my $delete = sub {
		$dbix->delete($tbl, { id => $id });
		$self->add_to_actions('deleted mdt date');
		$self->do_history($patient_id);
	};
	
	my $ok = $db->do_transaction($delete);
	
    # don't need return value unless error:
    return $ok ? 0 : 'delete_mdt_date() error - ' . $db->error;
}

#-------------------------------------------------------------------------------
sub delete_treatment_data {
	my ($self, $id) = @_;
	
	my $dbix = $self->lims_dbix;

	my $tbl = 'hmrn.patient_treatment';
	
	my $patient_id
		= $dbix->query("select patient_id from $tbl where id = ?", $id)->list;
	
	my $db = $self->lims_db; # ie LIMS::DB->new_or_cached;

	my $delete = sub {
		$dbix->delete($tbl, { id => $id });
		$self->add_to_actions('deleted treatment data');
		$self->do_history($patient_id);
	};
	
	my $ok = $db->do_transaction($delete);
	
    # don't need return value unless error:
    return $ok ? 0 : 'delete_treatment_data() error - ' . $db->error;
}

#-------------------------------------------------------------------------------
sub get_new_diagnoses {
	my $self = shift;
	my $args = shift;
	
    # get HMRN parent organisation ids:
    my $o = LIMS::DB::LocalNetworkLocation::Manager->get_local_network_locations;
    my @parent_organisation_ids = map { $_->parent_id } @$o;	
	
	# get referral_type.id for practices (saves multi_many_ok flag in query):
	my $ref_type = LIMS::DB::ReferralType->new(description => 'practice')->load;

	my @query = (
		'request_history.action' => 'authorised',
		or => [ # any network hospital or GP practice:
			'parent_organisations.id' => \@parent_organisation_ids,
			'parent_organisations.referral_type_id' => $ref_type->id, # GP's
		],		
		or => [ # ICDO3 or MGUS diagnoses:
			'request_report.diagnosis.icdo3' => { like => '%3' },
			'request_report.diagnosis.icdo3' => '9765/1', # MGUS
		],
	);
	
	{ # calculate requested duration (previous_week, days, date_range, etc):
		my $constraints = $self->_get_hmrn_new_diagnoses_constraints($args);
		push @query, @$constraints;
	}
	
	my @tables = qw( patients diagnoses parent_organisations request_history
		referrers );	
	my $relationships = $self->get_relationships(\@tables);

	my @params = (
		query => \@query,
		require_objects => $relationships,
	);	
	
	my $cases = LIMS::DB::Request::Manager->get_requests(@params);
	
	return $cases;
}

# ------------------------------------------------------------------------------
sub get_patient_demographics {
	my ($self, $patient_id) = @_;
	
	my $data = LIMS::DB::PatientDemographic->new(patient_id => $patient_id)
		->load( with => [ 'referrer', 'practice' ], speculative => 1 );
	
	return $data;
}

# ------------------------------------------------------------------------------
sub update_patient_demographics {
	my ($self, $args) = @_; # warn Dumper $args; 
	
	my $patient_id = $args->{patient_id};
	
    # format post-code:
    $args->{post_code} = LIMS::Local::Utils::format_postcode($args->{post_code});
	# tidy up address:
	$args->{address} = $self->_reformat_address($args->{address});
	
	my $user_id = $self->user_profile->{id};

	my $o = LIMS::DB::PatientDemographic->new(patient_id => $patient_id);
	
	my $db = $self->lims_db; # ie LIMS::DB->new_or_cached;
	
	my $update = sub {
		if ($o->load_speculative) {
			my $old = $o->clone->as_tree;
			
			# update object with new data:
			map {
				$o->$_($args->{$_})
			} grep $_ ne 'patient_id', keys %$args;
			$o->save(changes_only => 1);
	
			my $new = $o->clone->as_tree;
			my @changed = do {
				no warnings 'uninitialized';
				grep { $new->{$_} ne $old->{$_} } keys %$old; 
			}; # warn Dumper \@new;
			
			for my $field (@changed) {
				my $action  = qq!updated '$field' from $old->{$field}!;
				my %data = (
					patient_id => $patient_id,
					user_id    => $user_id,
					action 	   => $action,
				);
				LIMS::DB::PatientDemographicHistory->new(%data)->save;
			}
		}
		else { # create new:
			map { $o->$_($args->{$_}) } keys %$args;
			$o->save;		
		}
	};
	
	my $ok = $db->do_transaction($update);
	
	# don't need return value unless error:
    return $ok ? 0 : 'update_patient_demographics() error - ' . $db->error;
}

# ------------------------------------------------------------------------------
sub update_comment {
	my ($self, $data) = @_; # warn Dumper $data;
	
	my $patient_id = $data->{patient_id}; # warn Dumper $patient_id;
	my $form_param = $data->{comment}; # warn Dumper $form_param;
	
	my $dbix = $self->lims_dbix;

	my $table = 'hmrn.patient_comments';
	
	my $db = $self->lims_db; # ie LIMS::DB->new_or_cached;

	my $update = sub {
		my $sql = "select comment from $table where patient_id = ?";
		
		if ( my $comment = $dbix->query($sql, $patient_id)->list ) { # warn Dumper $comment;
			return 0 if $form_param && $form_param eq $comment; # skip if no change
			
			if ($form_param) { # have both & they're different so do update:
				$dbix->update($table,
					{ comment => $form_param }, { patient_id => $patient_id }
				);
				$self->add_to_actions('updated comment');
			}
			else { # no form_param so delete:
				$dbix->delete($table, { patient_id => $patient_id });
				$self->add_to_actions('deleted comment');
			}
		}
		else { # have new data:
			$dbix->insert($table,
				{ patient_id => $patient_id, comment => $form_param }
			);
			$self->add_to_actions('added comment');
		}
		
		$self->do_history($patient_id); 
	};
	
	my $ok = $db->do_transaction($update); 
	
	# don't need return value unless error:
    return $ok ? 0 : 'update_comment() error - ' . $db->error;
}

# ------------------------------------------------------------------------------
sub update_staging_data {
	my ($self, $args) = @_;  warn Dumper $args;
	
	my $patient_id  = $args->{patient_id};
	my $form_params = $args->{form_params}; 
	
	my $dbix = $self->lims_dbix;

	my $staging_tbl = 'hmrn.patient_staging_site';
	my $other_tbl   = 'hmrn.patient_staging_other';
    
    my $staging_opts = $self->get_enum_opts($other_tbl, 'detail');
	
	my $sites_map
		= $dbix->query('select description, id from hmrn.staging_sites')->map;
    
	my $db = $self->lims_db; # ie LIMS::DB->new_or_cached;

	my $update = sub {
		{ # clear any existing data:
            my $i = grep {
                $dbix->query(
                    qq!select 1 from $_ where patient_id = ?!, $patient_id )->list;
            } ($staging_tbl, $other_tbl);
            
            if ($i) { # have existing data:
                for my $tbl($staging_tbl, $other_tbl) {
                    $dbix->delete($tbl, { patient_id => $patient_id });
                }
                $self->add_to_actions('updated staging data');
            }
            else {
                $self->add_to_actions('added new staging data');
            }
        }
        # staging sites:
		PARAM: for my $param(keys %$form_params) { # just need key presence/absence
			my $site_id = $sites_map->{$param} || next PARAM;
			$dbix->insert($staging_tbl,
                { patient_id => $patient_id, site_id => $site_id }
            );
		}
        # staging other details (eg bulky, extensive or check_ct):
		OPT: for my $opt( @$staging_opts ) { # warn Dumper $opt;
			next OPT unless $form_params->{$opt};
            $dbix->insert($other_tbl,
                { patient_id => $patient_id, detail => $opt }
            );
        }
        # free-text from 'details' textarea:
		if ( my $others = $form_params->{details} ) { 
			my $tbl = 'hmrn.patient_sites_other';
			my $sql = "select details from $tbl where patient_id = ?";
			my $old = $dbix->query($sql, $patient_id)->list;
			
			no warnings 'uninitialized';
			if ($others ne $old) {
				if ($others && ! $old) { # new data:
					$dbix->insert($tbl,
						{ patient_id => $patient_id, details => $others }
					);
				}
				elsif ($old && ! $others) { # delete:
					$dbix->delete($tbl, { patient_id => $patient_id });
				}
				else { # update
					$dbix->update($tbl,
						{ details => $others },
						{ patient_id => $patient_id }
					);
				}
			}
		}

		$self->do_history($patient_id);
	};
	
	my $ok = $db->do_transaction($update);
	
	# don't need return value unless error:
    return $ok ? 0 : 'update_lymphoid_involvement() error - ' . $db->error;
}

# ------------------------------------------------------------------------------
sub update_antecedent_events {
	my ($self, $args) = @_; # warn Dumper $args;
	
	my $patient_id = $args->{patient_id};
	
	my $dbix = $self->lims_dbix;
	
	my $antecedent_tbl = 'hmrn.patient_antecedent';
	my $prior_tx_tbl   = 'hmrn.patient_prior_therapies';
	
	my $therapy_opts = $self->get_enum_opts($prior_tx_tbl, 'therapy'); # ie Rx, Cx
	
	my $db = $self->lims_db; # ie LIMS::DB->new_or_cached;

	my $update = sub {
		{ # antecedent event:
			my $event_id = $args->{event_id}; # required field

			my $sql = qq!select event_id from $antecedent_tbl where patient_id = ?!;
			if ( $dbix->query($sql, $patient_id)->into(my $id) ) { # warn Dumper $data;
				if ( $event_id != $id ) {
					my %data = ( event_id => $event_id ); 
					$dbix->update($antecedent_tbl, \%data, { patient_id => $patient_id });
					$self->add_to_actions('updated antecedent event');
				}
			}
			else {
				my %data = (
					patient_id => $patient_id,
					event_id   => $event_id,
				);
				$dbix->insert($antecedent_tbl, \%data);
				$self->add_to_actions('added new antecedent event');
			}
		}
		{ # prior therapies:
			my $sql = qq!select therapy from $prior_tx_tbl where patient_id = ?!;
			my $list = $dbix->query($sql, $patient_id)->flat; # warn Dumper $data;
			if (@$list) { 
				my %old = map { $_ => 1 } @$list; # create hash for comp. with $args

				foreach my $tx(@$therapy_opts) { # warn Dumper $tx;
					my %data = (
						patient_id => $patient_id,
						therapy    => $tx,
					);

					my $have_new = $args->{$tx}; # will be 1 or 0
					my $have_old = $old{$tx};    # will be 1 or 0
					
					if ( $have_new && ! $have_old ) { # warn 'have new & not old';
						$dbix->insert($prior_tx_tbl, \%data);
						$self->add_to_actions("added new prior Tx $tx");
					}
					elsif ( $have_old && ! $have_new ) { # warn 'have old & not new';
						$dbix->delete($prior_tx_tbl, \%data );
						$self->add_to_actions("deleted prior Tx $tx entry");
					}
					else { } # old & new both same value or null, so skip
				}
			}
			else {
				foreach my $tx(@$therapy_opts) {
					next unless $args->{$tx};
					my %data = (
						patient_id => $patient_id,
						therapy    => $tx,						
					);
					$dbix->insert($prior_tx_tbl, \%data);
					$self->add_to_actions("added new prior Tx $tx");
				}
			}
		}
		$self->do_history($patient_id);
	};
	
	my $ok = $db->do_transaction($update); 
	
	# don't need return value unless error:
    return $ok ? 0 : 'update_antecedent_events() error - ' . $db->error;
}

# ------------------------------------------------------------------------------
sub update_patient_chronologies {
	my ($self, $args) = @_; # warn Dumper $args; # patient_id + dates

	my $patient_id = $args->{patient_id};
	
	my $dbix = $self->lims_dbix;
	
	my $table = 'hmrn.patient_chronologies';

	# get date cols in patient_chronologies table:
	my $meta = $self->get_meta($table);
	my @date_cols = grep { $meta->{$_}->{type} eq 'date' } keys %$meta; # warn Dumper \@date_cols;

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

	my $update = sub {
		# get existing chronologies:
		my $sql = qq!select * from $table where patient_id = ?!;
		if ( my $data = $dbix->query($sql, $patient_id)->hash ) { # warn Dumper $data;
			$self->inflate_mysql_dates_to_datetime($data, \@date_cols);
			
			my @updates = ();
			
			COL: for my $col(@date_cols) {
				my $new = $args->{$col}; # in EU format
				my $old = $data->{$col}; # in DT format
				
				if ( $new && $old ) {
					# convert new to DT object:
					my $new_dt = LIMS::Local::Utils::to_datetime_using_datecalc($new);
					
					next COL unless DateTime->compare($old, $new_dt); # true if different
					push @updates, ( $col => $new_dt->ymd );
					$self->add_to_actions("updated date $col");
				}
				elsif ( $old && ! $new ) {
					push @updates, ( $col => undef );
					$self->add_to_actions("deleted date $col");
				}
				elsif ( $new && ! $old ) {
					my $dt = LIMS::Local::Utils::to_datetime_using_datecalc($new);
					push @updates, ( $col => $dt->ymd );
					$self->add_to_actions("added new date $col");
				}
				else { next COL } # ignore if no new or old
			}
			
			if (@updates) { # warn Dumper \@updates;
				$dbix->update($table, { @updates }, { patient_id => $patient_id } );
			}
		}
		else { # insert new data:
			my @cols = keys %$meta; # warn Dumper \@cols;
			my %data = map { $_ => $args->{$_} } @cols; # warn Dumper \%data; 
			map { # transform EU dates to MySQL format:
				$data{$_} = LIMS::Local::Utils::date_to_mysql($data{$_})
			} @date_cols; # warn Dumper \%data;
			
			$dbix->insert($table, \%data);
			$self->add_to_actions("added new chronological data");
		}
		
		# mdt_date always new (not edited):
		if ( my $mdt_date = $args->{mdt_date} ) {
			my $date = LIMS::Local::Utils::date_to_mysql($mdt_date);
			my %data = (
				patient_id => $patient_id,
				date       => $date,
			); # warn Dumper \%data;
			$dbix->insert('hmrn.patient_mdt_dates', \%data);
			$self->add_to_actions("added new mdt meeting date");
		}
		
		$self->do_history($patient_id);
	};
	
	my $ok = $db->do_transaction($update);
	
	# don't need return value unless error:
    return $ok ? 0 : 'update_patient_chronologies() error - ' . $db->error;
}

sub do_history {
	my ($self, $patient_id) = @_;
	
	my @actions = $self->all_actions;
	
	my $dbix = $self->lims_dbix;

	my $user_id = $self->user_profile->{id};
	
	for my $action(@actions) {
		my %data = (
			patient_id => $patient_id,
			user_id    => $user_id,
			action 	   => $action,
		); # warn Dumper \%data;
		$dbix->insert('hmrn.history', \%data);
	}	
}

# ------------------------------------------------------------------------------
sub is_in_outreach {
	my ($self, $patient_id) = @_;
	
	my $dbix = $self->lims_dbix;
	
	my $sql = 'select 1 from outreach.dispatch_details where patient_id = ?';
	return $dbix->query( $sql, $patient_id )->list;	
}

# ------------------------------------------------------------------------------
sub get_tx_details_for_tx_type {
	my ($self, $type_id) = @_;
	
	my $dbix = $self->lims_dbix;

	my $sql = q!select id, description from hmrn.treatment_details where
		type_id = ? order by description!;
	return $dbix->query( $sql, $type_id )->hashes; # array(ref) of hashes
}

# ----------------------- private methods --------------------------------------
sub _get_select_option_data {
	my $self = shift;
	my $args = shift || {}; # optional - supplied only if tx_details required
	
	my $dbix = $self->lims_dbix;

	my %maps = ();
	
	{ # locations:
		my $locations = $dbix->query(q!select location,id from hmrn.locations!)->map;
		$maps{location} = $locations;
	}
	{ # antecedent events:
		my $antecedent
			= $dbix->query(q!select event,id from hmrn.antecedent_events!)->map;
		$maps{antecedent} = $antecedent;
	}
	{ # treatment types:
		my $tx_types
			= $dbix->query(q!select description,id from hmrn.treatment_types!)->map;
		$maps{tx_type} = $tx_types;
	}
	{ # unique tx_type_ids from treatment_details table:
		my $sql = q!select distinct(type_id) from hmrn.treatment_details!;
		my $unique_tx_type_ids = $dbix->query($sql)->flat; # warn Dumper $unique_tx_type_ids;
		$maps{tx_type_ids} = $unique_tx_type_ids;
	}
	{ # treatment reponses:
		my $opts = $self->get_enum_opts('hmrn.patient_treatment', 'response');		
		$maps{response} = $opts; # warn Dumper $opts;
	}
	{ # parameter validation data:
		my $sql = q!select * from hmrn.parameters!;
		my $data = $dbix->query($sql)->map_hashes('param_name'); # HoH with keys = param_name
		$maps{parameters} = $data; # warn Dumper $data;		
	}
	{ # menu options:
		my $sql = $self->sql_lib->retr('hmrn_menu_options');
		my $data = $dbix->query($sql)->map; # HoH with keys = param_name
		$maps{menu_options} = $data; # warn Dumper $data;		
	}
	# treatment details for treatment_type (if submitted):
	if ( my $tx_type_id = $args->{tx_type_id} ) { 
		my $sql = q!select id,description from hmrn.treatment_details where type_id = ?!;
		my $tx_details = $dbix->query($sql, $tx_type_id)->map;
		# tx_details is ajax function in original form, so not loaded in _get_select_option_data()
		$maps{tx_details} = $tx_details;
	}
	
    return \%maps;
}

sub _reformat_address {
	my $self = shift;
	my $addr = shift;
	
	my @lines = split /[\,\n]+/, $addr; # split on new-line, comma or both 
  
	my @new = map { # doesn't deal with inconsistent use of comma between house number & street name
		# capitalise 1st letter of each word in @lines:
		join ' ', grep { s/(.*)/ucfirst( lc($1) )/e } split ' ', $_; 
	} @lines;

	return join ', ', grep $_, @new; # reform with comma-space field delimiters 
}

1;