package Platform::Account;
use strict;
use warnings;
use base 'Platform::DBI';

__PACKAGE__->table('account');
__PACKAGE__->sequence('account_account_seq');
__PACKAGE__->columns( Primary => 'account' );
__PACKAGE__->columns( Essential => qw / 
  first_name
  last_name
  email
  created_at
  updated_at
/ );

__PACKAGE__->has_many( attendees => 'Platform::Attendee' );
__PACKAGE__->has_many( events => [ 'Platform::Attendee', 'event' ] );
__PACKAGE__->has_a( created_at => 'Class::Date' );
__PACKAGE__->has_a( updated_at => 'Class::Date' );

__PACKAGE__->mk_classdata(
	Field_form => {
		  first_name => [ Textfield => { size => 35 } ]
		, last_name => [ Textfield => { size => 35 } ]
		, email => [ Textfield => { size => 35 } ]
	}
);

our @required_fields = qw/ first_name last_name email /;

sub full_name {
  my ($account) = @_;
  return $account->first_name . ' ' . $account->last_name;
}

sub field_form {
  my ($account) = @_;
  my $form = $account->SUPER::field_form;

  return $form;
}

sub save_from_form {
  my ($account, $field_form) = @_;

  $account->SUPER::save_from_form($field_form);
}

sub create_from_form {
  my ($class, $form) = @_;
  my $account = $class->SUPER::create_from_form($form);

  return $account;
}

sub send_edit_profile_request {
  my ($account) = @_;
  my $buffer;
  my $TT = Template->new({
    INCLUDE_PATH => Platform::path_from_config('common.email_path'),
    ENCODING => 'utf8',
  });
  my $edit_profile_request = Platform::Edit_Profile_Request->insert({
    account => $account
  });

  if ($TT->process('edit_profile_request.tt', { edit_profile_request => $edit_profile_request }, \$buffer, {binmode => ':utf8'})) {
    my %email = (
      From => $Platform::CONFIG->{common}{email_from},
      'Reply-To' => $Platform::CONFIG->{common}{reply_to},
      To => $account->email,
      Subject => 'Edit My Profile Request for: Ortho Registration Site (1235573)',
      Data => $buffer
    );
    my $message = Platform::Email->new(%email);

    $message->send();
  }
}
