package Local::Class::DBI::Relationship::HasA;
use Scalar::Util qw/ blessed /;

use strict;
use warnings;

use base 'Local::Class::DBI::Relationship';

sub remap_arguments {
	my ($proto, $class, $want_col, $a_class, %meths) = @_;
	$class->_invalid_object_method("has_a") if ref $class;
	my $column = $class->find_column($want_col)
		or return $class->_croak("Column $want_col does not exist in $class");
	$class->_croak("$class $column needs an associated class") unless $a_class;
	return ($class, $column, $a_class, \%meths);
}

sub triggers {
	my $self = shift;
	$self->class->_require_class($self->foreign_class);

	my @triggers;
	unless (defined $self->args->{'no_inflate'}) {
		my $column = $self->accessor;
		push @triggers, (
			select => $self->_inflator,

			# after_create	=> $self->_inflator, # see t/6
			"after_set_$column" => $self->_inflator,
			deflate_for_create  => $self->_deflator(1),
			deflate_for_update  => $self->_deflator,
		);
	}

	return @triggers;
}

sub _inflator {
	my $rel = shift;
	my $col = $rel->accessor;
	# Only gets used in a string context, might as well just do it once
	$col = "${col}";
	my $join_key = "__joined__${col}";
	my $meta = $rel->class->meta_info($rel->name => $col);
	my $a_class = $meta->foreign_class;
	my $inflate = $meta->args->{'inflate'};
	return sub {
		my $self = shift;
		defined(my $value = $self->_attrs($col)) or return;

		return if ref $value and $value->isa($a_class);
		my $inflator;

		my $get_new_value = sub {
			my ($inflator, $value, $want_class, $obj) = @_;
			my $new_value =
				(ref $inflator eq 'CODE')
				? $inflator->($value, $obj)
				: $want_class->$inflator($value);
			return $new_value;
		};

		# If we have a custom inflate ...
		if (defined $inflate) {
			$value = $get_new_value->($inflate, $value, $a_class, $self);
			return $self->_attribute_store($col, $value)
				if ref $value
				and $value->isa($a_class);
			$self->_croak("Inflate method didn't inflate right") if ref $value;
		}

		# If a sub-class redefines a has_a, might have to extract the primary key and load again
		if (blessed $value && $value->isa('Local::Class::DBI')) {
		  $value = $value->id;
		}

		return $self->_croak("Can't inflate $col to $a_class using '$value': "
				. ref($value)
				. " is not a $a_class")
			if ref $value;

		if ($a_class->isa('Local::Class::DBI') && (my $data = delete $self->{$join_key})) {
			$inflator = "construct";
			$value = $data;
		} else {
			$inflator = $a_class->isa('Local::Class::DBI') ? "_simple_bless" : "new";
		}

		$value = $get_new_value->($inflator, $value, $a_class);

		return $self->_attribute_store($col, $value)
			if ref $value
			and $value->isa($a_class);

		# use ref as $obj may be overloaded and appear 'false'
		return $self->_croak(
			"Can't inflate $col to $a_class " . "via $inflator using '$value'")
			unless ref $value;
	};
}

sub _deflator {
	my ($self, $always) = @_;
	my $col = $self->accessor;
	# Only gets used in a string context, might as well just do it once
	$col = "${col}";
	return sub {
		my $self = shift;
		return unless $self->_attribute_exists($col);
		$self->_attribute_store($col => $self->_deflated_column($col))
			if ($always or $self->{__Changed}->{$col});
	};
}

sub _set_up_class_data {
	my $self = shift;
	$self->class->_extend_class_data(__hasa_rels => $self->accessor =>
			[ $self->foreign_class, %{ $self->args } ]);
	$self->SUPER::_set_up_class_data;
}

1;
