use strict;
use warnings;

package API::Product::PricingImport::PreProcess;

use MIME::Base64;
use MIME::Entity;
use MIME::Lite;

use Acme::Damn ();

use SOAP::Lite ();
use base 'SOAP::Server::Parameters';

our $Config;
BEGIN {
	use API::Config;
	$Config = $API::Config::Config;
}
use lib ($Config->{'Website_libs'});

use ECS::Config;
use ECS::SQL;

use ECS::Product::PricingImport::PreProcess;
use ECS::Product::PricingImport::PreProcessor;

my $website_config = \%ECS::Config::c;

return 1;

sub getAll {
	my $class = shift;
	
	return {
		'success' => 1,
		'error' => undef,
		'data' => \%ECS::Product::PricingImport::PreProcess::pre_processors,
		'preprocessorOrder' => \@ECS::Product::PricingImport::PreProcess::preprocessorOrder,
		'update_DB'	=> $Config->{'update_DB'}
	};
}

sub newPreProcess {
	my $self = shift;
	my $input = shift;
	my $envelope = pop;
	
	my $result = {
		'success' => 1,
		'error' => undef,
		'data' => {},
		'update_DB'	=> $Config->{'update_DB'}
	};
	
	foreach my $k ('employeeID', 'ipaddr', 'preProcessor') {
		if (not $input->{$k}) {
			$result->{'success'} = 0;
			$result->{'error'} = $k.' is required to create new Pricing Import';
			return $result;
		}
	}
	
	my @parts = $envelope->parts();
	my $file;
	
	if (
		defined($parts[0])
		and defined($parts[0]->[0])
	) {
		$file = $parts[0]->[0];
	} else {
		$result->{'success'} = 0;
		$result->{'error'} = 'No pricing file found';
		return $result;
	}
	
	my $DBH = ECS::SQL::connect();
	
	$input->{'status'} = 'init';
	
	my $pricingImportPreProcessID = ECS::SQL::insert({
		'DBH' => $DBH,
		'table' => 'pricingImportPreProcess',
		'columns' => ['status', 'employeeID', 'ipaddr', 'preProcessor'],
		'hash' => $input
	});
	
	if (not $pricingImportPreProcessID) {
		$result->{'success'} = 0;
		$result->{'error'} = 'Unable to create new Pricing Import PreProcess';
		return $result;
	}
	
	if (open(my $of, '>'.$website_config->{'pricing_import'}->{'file_dir'}.'/preprocess/init/preprocess_'.$pricingImportPreProcessID.'.zip')) {
		print $of $file->bodyhandle->as_string;
	} else {
		$result->{'success'} = 0;
		$result->{'error'} = 'Unable to save pricing file';
		return $result;
	}
	
	$result->{'data'}->{'pricingImportPreProcessID'} = $pricingImportPreProcessID;
	
	return $result;
}

sub getDetails {
	my $self = shift;
	my $input = shift;

	my $result = {
		'success' => 1,
		'error' => undef,
		'data' => {},
		'update_DB'	=> $Config->{'update_DB'}
	};

	if (not $input->{'pricingImportPreProcessID'}) {
		$result->{'success'} = 0;
		$result->{'error'} = 'Pricing Import PreProcess ID is required to fetch details';
		return $result;
	}
	
	$input->{'pricingImportPreProcessID'} =~ s/\D//g;
	
	my $DBH = ECS::SQL::connect();
	
	my $import = ECS::Product::PricingImport::PreProcess->load({
		'DBH' => $DBH,
		'pricingImportPreProcessID' => $input->{'pricingImportPreProcessID'}
	});
	
	delete($import->{'DBH'});
	delete($import->{'DBHr'});
	
	$result->{'data'} = $import;
	
	return $result;
}

sub delete {
	my $self = shift;
	my $input = shift;

	my $result = {
		'success' => 1,
		'error' => undef,
		'data' => {},
		'update_DB'	=> $Config->{'update_DB'}
	};
	
	$input->{'pricingImportPreProcessID'} =~ s/\D//g;

	if (not $input->{'pricingImportPreProcessID'}) {
		$result->{'success'} = 0;
		$result->{'error'} = 'Pricing Import PreProcess ID is required to fetch details';
		return $result;
	}
	
	my $DBH = ECS::SQL::connect();
	
	my $import = ECS::Product::PricingImport::PreProcess->load({
		'DBH' => $DBH,
		'pricingImportPreProcessID' => $input->{'pricingImportPreProcessID'}
	});

	if ($import) {
		$result->{'success'} = ECS::Product::PricingImport::PreProcess->delete({
			'DBH' => $DBH,
			'pricingImportPreProcessID' => $import->{'pricingImportPreProcessID'},
			'pricingFilePath' => $import->{'pricingFilePath'}
		});
	}
	
	if ($result->{'success'} == 0) {
		$result->{'error'} = 'The pre-processed import could not be deleted successfully.';
	}
	
	return $result;
}

