<cfcomponent displayname="Fax Functions" hint="I Return Fax Information" output="false">
	<cfparam name="client.Loc_ID" default = "0">
   <cffunction name="init" displayname="Initialize Component" access="public" returntype="Fax" hint="I initialize and return the Fax object.">
		<cfreturn this>
	</cffunction>
    
   	<cffunction name="send_fax"  returntype="any" access="public" output="false" >
	    <cfargument name="fax_number" required="yes" hint="fax_number">	    
	    <cfargument name="filename" required="yes" hint="filename">	 
	    <cfargument name="coverpage" required="yes" hint="coverpage">	
	    <cfset return_message = "failure">	
	    <cfscript>
		    // Configuration variables (replace with actual values or use environment variables)
		    rcServerUrl = "https://platform.ringcentral.com"; // or sandbox URL: https://platform.devtest.ringcentral.com
		    clientId = "9xFJiZjs9OadyVZU6hJuUT";
		    clientSecret = "9mtfvdcTU0HcWzcPDv9lNCA89E19RDznlcHPy3wEys7L";
		    jwtToken = "eyJraWQiOiI4NzYyZjU5OGQwNTk0NGRiODZiZjVjYTk3ODA0NzYwOCIsInR5cCI6IkpXVCIsImFsZyI6IlJTMjU2In0.eyJhdWQiOiJodHRwczovL3BsYXRmb3JtLnJpbmdjZW50cmFsLmNvbS9yZXN0YXBpL29hdXRoL3Rva2VuIiwic3ViIjoiMzIzNzgwMTAxNSIsImlzcyI6Imh0dHBzOi8vcGxhdGZvcm0ucmluZ2NlbnRyYWwuY29tIiwiZXhwIjozODkzMDY0OTE4LCJpYXQiOjE3NDU1ODEyNzEsImp0aSI6Imh4bEltSERnVE1xWTJoNmFqOVNzNHcifQ.dPhhHIboyGGvxXNjTOpbRrMtWS50Rs3o8HBS28oSMdjGlJZN59Iw5uZcfF4IMhGvPGszDFtI7RXdIfDnkMgDxFKPXOuBvo_x9Pi5oLM1FFKW9v3vHT9fgUuE3DOp9NQB1mKM8woTuTorTf6Ey8ersZtFpGQ1hb9DdFiWhyzDdr3p86hPrR0bd9A76ckRhsJJNoFzIiW6ButFKw-8kPNUYQWZPudta1lAodI6XcRSzvgr2cav-KinvEvxoNOBoUCfiq7yLr7q1Yd_FhCCHSrnwaRfCpmmZe8DyUzLyXGXtQY_qVne9uIm-s4qJ9Ls1fU01CKlRq73QNfnYWpfln8FQg"; // JWT used for login
		    accountId = "3237801015";
		    extensionId = "3237801015";
		</cfscript>
		<cfquery  name='getagency' datasource='#Application.DataSrc#'>
			  	SELECT   * FROM  #Request.prefix_db_lookup#.Agency
 			  	WHERE  Agency_ID = '#session.AgencyId#'
 			  	AND  RingCentral_extensionId IS NOT NULL
 		 </cfquery>		

		<cfif  getagency.recordcount GT 0>
			<cfset accountId = getagency.RingCentral_accountId />
			<cfset extensionId = getagency.RingCentral_extensionId />
			<!--- <cfset clientId = getagency.RingCentral_ClientId />
			<cfset clientSecret = getagency.RingCentral_ClientSecret /> --->
			<cfset jwtToken = getagency.RingCentral_JWT /> 
		</cfif> 
		<!--- Step 1: Authenticate using JWT --->
		<cfset loginUrl = rcServerUrl & "/restapi/oauth/token">
		<cfset loginHeaders = {
		    "Authorization" = "Basic " & toBase64(clientId & ":" & clientSecret),
		    "Content-Type" = "application/x-www-form-urlencoded"
		}>
		<cfset loginBody = "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=" & jwtToken>

		<cfhttp url="#loginUrl#" method="post" result="loginResponse">
		    <cfhttpparam type="header" name="Authorization" value="#loginHeaders['Authorization']#">
		    <cfhttpparam type="header" name="Content-Type" value="#loginHeaders['Content-Type']#">
		    <cfhttpparam type="body" value="#loginBody#">
		</cfhttp>

		<cfset accessToken = "">
		<!--- <cfif CGI.REMOTE_ADDR EQ '103.143.7.161' OR CGI.REMOTE_ADDR EQ '157.51.4.55'>
		 <cfdump var="#loginResponse#">
		</cfif>--->
		<cfif loginResponse.statusCode EQ "200 OK">
		    <cfset authData = deserializeJson(loginResponse.fileContent)>
		    <cfset accessToken = authData.access_token>
		<cfelse>
		    <cfoutput>Login failed: #loginResponse.statusCode#</cfoutput>
		    <cflog  file="faxLogerror"  text="Fax Status: #loginResponse.statusCode# - Error when we get accessToken"   type="information">
		    <cfabort>
		</cfif>

		<!--- Step 2: Send Fax --->
		<cfset faxUrl = rcServerUrl & "/restapi/v1.0/account/#accountId#/extension/#extensionId#/fax">
		<cfset faxHeaders = {
		    "Authorization" = "Bearer " & accessToken,
		    "Content-Type" = "multipart/form-data"
		}>

		<cfhttp url="#faxUrl#" method="post" result="faxResponse">
		    <cfhttpparam type="header" name="Authorization" value="#faxHeaders['Authorization']#">
		    <cfhttpparam type="header" name="Content-Type" value="#faxHeaders['Content-Type']#">
		    <cfhttpparam type="header" name="accountId" value="#accountId#">
		    <cfhttpparam type="header" name="extensionId" value="#extensionId#">
		    <!--- Attach file --->
		    <cfhttpparam type="file" name="attachment" file="#arguments.filename#" mimetype="application/pdf">
		    <!--- Fax recipient --->
		    <cfhttpparam type="formfield" name="to" value="#arguments.fax_number#">
			<cfhttpparam type="formfield" name="coverIndex" value="0">
		    <!---<cfhttpparam type="formfield" name="coverPageText" value="#arguments.coverpage#"> --->
		</cfhttp>

		<cfif faxResponse.statusCode EQ "200 OK" OR faxResponse.statusCode EQ "201">
		   	<cfset return_message = "success">
		    <cflog  file="faxLog"  text="Fax Status: #faxResponse.statusCode# - Fax send successfully to #arguments.fax_number# with cover page of - #arguments.coverpage#"   type="information">
		<cfelse>
		    <cflog  file="faxLogerror"  text="Fax Status: #loginResponse.statusCode# - Error when we send fax - #faxResponse.fileContent#"   type="information">
		    <cfdump var="#faxResponse#">
		    <cfabort>
		</cfif>
		<cfreturn return_message>		
	</cffunction>
</cfcomponent>