package manufacturing;

import java.util.ArrayList;

import sygr.pots.extensions.Attachment;
import sygr.pots.extensions.Attr;
import sygr.pots.extensions.ExtAlert;
import sygr.pots.extensions.ExtConstants;
import sygr.pots.extensions.HttpHeaderValue;
import sygr.pots.extensions.PluginData;
import sygr.pots.extensions.PluginInterface;
import sygr.pots.extensions.PluginUtilInterface;

public class ErpInitPo implements PluginInterface{

	@Override
	public void execute(PluginData data, PluginUtilInterface util) {
			
		if(data.pot == null) {
			util.log("ErpInitPo ERROR: no entity");
			return;
		}
		
		Attr attr = data.pot.getFlexi();
		String attaguid = util.getNodeValue(attr, Constants.ATTAGUID);
		
		Attachment atta = util.readAttachmentByGuid(attaguid);
		if(atta == null || atta.getContent() == null) {
			util.log("ErpInitPo ERROR: attachment not found");
			return;
		}
		util.deleteAttachment(attaguid);
		
		byte[] pdf = atta.getContent();
		
		String uri = Util.getPdfTxtUrl(util);
		if(uri.equals("")) return;
				
		// Create the headers
		ArrayList<HttpHeaderValue> headers = new ArrayList<>();
		HttpHeaderValue header1 = new HttpHeaderValue();
		header1.headerName = "Content-type";
		header1.headerValue = "application/pdf";
		headers.add(header1);
			
		// We send the request body as binary, but expect the answer as plain text
		ArrayList<Object> result = util.execCommand(
				"HTTP", "BINARY", "TEXT", uri, headers, pdf);
		util.log("ErpInitPo: execCommand returned");
		
		if(result == null || result.size() == 0) {
			util.log("ErpInitPo: execCommand ERROR, no usable result returned.");
		}
		Object retobj = result.get(0);
		if(!(retobj instanceof String)) {
			util.log("ErpInitPo: execCommand ERROR, returned object is not String");
		}
		String pdfcontent = ( String ) retobj;
		pdfcontent = pdfcontent.replaceAll(System.lineSeparator(), "\\\\n");
		util.log("ErpInitPo: execCommand returned string size " + pdfcontent.length());
		
		String prompt = Util.getPrompt(pdfcontent);
		
		String answer = "";
		ArrayList<Object> alo = util.execCommand("AI", "CHATGPT", "SIMPLE", "aigpt", prompt);
		if(!(alo == null) && alo.size() > 1 && alo.get(1) instanceof String) {
			answer = ( String ) alo.get(1);			
		}
		else {
			util.log("ErpInitPo: GPT execution error");
		}
		
		// Send here mail to ERP with the answer
		ExtAlert mail = new ExtAlert();
		mail.setAlertseverity(ExtConstants.alertseverityINFO);
		mail.setAlerttarget("ERP");
		mail.setAlertsubject("New Purchase Order Arrived");
		String text = "Dear Sales Department," + System.lineSeparator() + System.lineSeparator();
		text = text + "a new Purchase Order was posted in the system." + System.lineSeparator() + System.lineSeparator();
		text = text + "Order number: " + data.pot.getMatchval0() + System.lineSeparator() + System.lineSeparator();
		text = text + answer;
		mail.setAlerttext(text);
		util.sendAlert(mail);
		
		
		// Simply overwrite the flexi attribute of the Entity
		Attr attr1 = Util.getPoAttr(answer, util);
		data.pot.setFlexi(attr1);
		data.pot.setType(Constants.ENTERPPO);
		
		util.log("ErpInitPo: finished");
		
	}

	@Override
	public void reloadConfig(ArrayList<String> changes, PluginUtilInterface util) {}

}
