"""
DOLPHYN: Decision Optimization for Low-carbon Power and Hydrogen Networks
Copyright (C) 2022,  Massachusetts Institute of Technology
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
A complete copy of the GNU General Public License v2 (GPLv2) is available
in LICENSE.txt.  Users uncompressing this from an archive may not have
received this license file.  If not, see <http://www.gnu.org/licenses/>.
"""

@doc raw"""
	write_synfuel_gen(path::AbstractString, sep::AbstractString, inputs::Dict, setup::Dict, EP::Model)

Function for reporting the different values of synthetic gasoline, jetfuel, and diesel generated by the different technologies in operation.
"""
function write_synfuel_gen(path::AbstractString, sep::AbstractString, inputs::Dict, setup::Dict, EP::Model)

	dfSynFuels = inputs["dfSynFuels"]
	H = inputs["SYN_FUELS_RES_ALL"]     # Number of resources (generators, storage, DR, and DERs)
	T = inputs["T"]     # Number of time steps (hours)

	# Power injected by each resource in each time step
	# dfSynFuelsDieselGenOut_annual = DataFrame(Resource = inputs["H2_RESOURCES_NAME"], Zone = dfSynFuels [!,:Zone], AnnualSum = Array{Union{Missing,Float32}}(undef, H))
	dfSynFuelsDieselGenOut = DataFrame(Resource = inputs["SYN_FUELS_RESOURCES_NAME"], Zone = dfSynFuels[!,:Zone], AnnualSum = Array{Union{Missing,Float32}}(undef, H))

	for i in 1:H
		dfSynFuelsDieselGenOut[!,:AnnualSum][i] = sum(inputs["omega"].* (value.(EP[:vSFProd_Diesel])[i,:]))
	end
	# Load hourly values
	dfSynFuelsDieselGenOut = hcat(dfSynFuelsDieselGenOut, DataFrame((value.(EP[:vSFProd_Diesel])), :auto))

	# Add labels
	auxNew_Names_Diesel=[Symbol("Resource");Symbol("Zone");Symbol("AnnualSum");[Symbol("t$t") for t in 1:T]]
	rename!(dfSynFuelsDieselGenOut,auxNew_Names_Diesel)

	Diesel_total = DataFrame(["Total" 0 sum(dfSynFuelsDieselGenOut[!,:AnnualSum]) fill(0.0, (1,T))], :auto)

	for t in  1:T
		Diesel_total[:,t+3] .= sum(dfSynFuelsDieselGenOut[:,Symbol("t$t")][1:H])
	end

	rename!(Diesel_total,auxNew_Names_Diesel)
	dfSynFuelsDieselGenOut = vcat(dfSynFuelsDieselGenOut, Diesel_total)

 	CSV.write(string(path,sep,"Synfuel_diesel_production.csv"), dftranspose(dfSynFuelsDieselGenOut, false), writeheader=false)

	# Power injected by each resource in each time step
	# dfSynFuelsJetfuelGenOut_annual = DataFrame(Resource = inputs["H2_RESOURCES_NAME"], Zone = dfSynFuels [!,:Zone], AnnualSum = Array{Union{Missing,Float32}}(undef, H))
	dfSynFuelsJetfuelGenOut = DataFrame(Resource = inputs["SYN_FUELS_RESOURCES_NAME"], Zone = dfSynFuels[!,:Zone], AnnualSum = Array{Union{Missing,Float32}}(undef, H))

	for i in 1:H
		dfSynFuelsJetfuelGenOut[!,:AnnualSum][i] = sum(inputs["omega"].* (value.(EP[:vSFProd_Jetfuel])[i,:]))
	end
	# Load hourly values
	dfSynFuelsJetfuelGenOut = hcat(dfSynFuelsJetfuelGenOut, DataFrame((value.(EP[:vSFProd_Jetfuel])), :auto))

	# Add labels
	auxNew_Names_Jetfuel=[Symbol("Resource");Symbol("Zone");Symbol("AnnualSum");[Symbol("t$t") for t in 1:T]]
	rename!(dfSynFuelsJetfuelGenOut,auxNew_Names_Jetfuel)

	Jetfuel_total = DataFrame(["Total" 0 sum(dfSynFuelsJetfuelGenOut[!,:AnnualSum]) fill(0.0, (1,T))], :auto)

	for t in  1:T
		Jetfuel_total[:,t+3] .= sum(dfSynFuelsJetfuelGenOut[:,Symbol("t$t")][1:H])
	end

	rename!(Jetfuel_total,auxNew_Names_Jetfuel)
	dfSynFuelsJetfuelGenOut = vcat(dfSynFuelsJetfuelGenOut, Jetfuel_total)

 	CSV.write(string(path,sep,"Synfuel_jetfuel_production.csv"), dftranspose(dfSynFuelsJetfuelGenOut, false), writeheader=false)


	# Power injected by each resource in each time step
	# dfSynFuelsGasolineGenOut_annual = DataFrame(Resource = inputs["H2_RESOURCES_NAME"], Zone = dfSynFuels [!,:Zone], AnnualSum = Array{Union{Missing,Float32}}(undef, H))
	dfSynFuelsGasolineGenOut = DataFrame(Resource = inputs["SYN_FUELS_RESOURCES_NAME"], Zone = dfSynFuels[!,:Zone], AnnualSum = Array{Union{Missing,Float32}}(undef, H))

	for i in 1:H
		dfSynFuelsGasolineGenOut[!,:AnnualSum][i] = sum(inputs["omega"].* (value.(EP[:vSFProd_Gasoline])[i,:]))
	end
	# Load hourly values
	dfSynFuelsGasolineGenOut = hcat(dfSynFuelsGasolineGenOut, DataFrame((value.(EP[:vSFProd_Gasoline])), :auto))

	# Add labels
	auxNew_Names_Gasoline=[Symbol("Resource");Symbol("Zone");Symbol("AnnualSum");[Symbol("t$t") for t in 1:T]]
	rename!(dfSynFuelsGasolineGenOut,auxNew_Names_Gasoline)

	Gasoline_total = DataFrame(["Total" 0 sum(dfSynFuelsGasolineGenOut[!,:AnnualSum]) fill(0.0, (1,T))], :auto)

	for t in  1:T
		Gasoline_total[:,t+3] .= sum(dfSynFuelsGasolineGenOut[:,Symbol("t$t")][1:H])
	end

	rename!(Gasoline_total,auxNew_Names_Gasoline)
	dfSynFuelsGasolineGenOut = vcat(dfSynFuelsGasolineGenOut, Gasoline_total)

 	CSV.write(string(path,sep,"Synfuel_gasoline_production.csv"), dftranspose(dfSynFuelsGasolineGenOut, false), writeheader=false)

end
