#To Do
  #Adjust to remove transportation cost
  #Adjust to remove taxes on intermediate SBS production


##Inputs
COST_TO_BUILD_SKID <- 300000
SKID_OP_COST <- 30000
SKID_CONSTRUCTION_COST_RATIO <- 0.33
SKID_EQUIP_COST_RATIO <- 1-0.33
FACILITY_OP_EX <- 75 #$ per SBS produced
SBS_PURCHASE_PRICE <- 225 #$ per SBS produced (or molasses)
BEET_PURCHASE_PRICE <- 100 #$ per for the input price of beets that produce 1 ton of SBS
FACILITY_OP_COST <- 75 #$ per SBS produced
MTU_CARBON_PER_TON_BEET <- 0.62 #MTU carbon per ton of SBS injected
CARBON_TAX_CREDIT_PRICE <- 90 # $ per MTU of carbon
VCM_PRICE <- 70 # $ per MTU of carbon sold on the market
RNG_BONUS <- 15 #$/MMBTU
NG_PRICE <- 3 # $/MMBTU
MAX_WASH_EMP <- 8 #Total number of employees at the Washakie facility
MAX_GOSHEN_EMP <-15 #Total number of employees at the Goshen facility
RAIL_COST <- 0.054162 #Cost in ton miles of train freight. (inflation adjusted from https://www.bts.gov/content/average-freight-revenue-ton-mile)
TRUCK_COST <- 0.29028 #Cost in ton miles of truck freight. (inflation adjusted from https://www.bts.gov/content/average-freight-revenue-ton-mile)
 WASHAKIE_TO_CAMPBELL <- 160 #Miles from Washakie SBS facility (and Wyoming Sugar) to Campbell skids
 GOSHEN_TO_CAMPBELL <- 205 #Miles from Goshen SBS facility to Campbell skids
 WESTON_TO_CAMPBELL <- 60 #Distance from the train depot for out of state beets in Upton, to the Campbell skids
 OUT_STATE_TO_WESTON <- 400  #Avg distance from out of state beet suppliers and the Upton train depot.
#IMPLAN Inputs
GOSHEN_WAGE <- 53007.10 #Average wage at the Goshen facility (taken from regional IMPLAN data)
WASH_WAGE <- 71017.25 #Average wage at the Washakie facility (taken from regional IMPLAN data)
PER_BEET_SPENDING <- 0.6832346 #The percentage of expenses associated with buying beets when operating a beet processing facility.

#Time Series Inputs
  Year <- 2024:2030
  #Gas production non-renewable and renewable natural gas
  GAS_PROD <- 1015*c(0,143500,124650,108277,94325,87253,87253)
  RNG_PROD <- 1015*c(0,24436,199100,382110,721571,1160415,1160415)
  #Capital investment in beet processing facilities
  BEET_CAP_WASHAKIE <- c(0,0,2500000,2500000,0,0,0)
  BEET_CAP_GOSHEN <- c(0,0,0,7500000,7500000,0,0)
  #Beet purchases
  TOTAL_SBS <- c(3000,10000,100000,175000,200000,240000,240000) #Total tons of SBS purchased or produced
  WYOMING_SUGAR_PURCHASES <-  c(3000,3000,43000,10000,10000,10000,10000) #Amount of SBS (or molasses) purchased from Wyoming Sugar LLC
  PRODUCED_SGS <- c(0,0,0,87500,150000,200000,200000) #Amount of SBS produced by all CCF owned processing facilities
  #Skids
  CONSTRUCTED_SKIDS <- c(1,4,10,10,5,0,0) #Number of skids built in a given year
  
######################Natural Gas
GAS_VALUE <- (RNG_PROD+GAS_PROD)*NG_PRICE
RNG_ADD_VALUE <- RNG_PROD*RNG_BONUS
GAS_TOTAL_VALUE <- GAS_VALUE+RNG_ADD_VALUE
#########################Beet Facility
#Find the percentage of total capital expenses for use in production allocation.
BEET_TOTAL_CAP_WASHAKIE <- c()
BEET_TOTAL_CAP_GOSHEN  <- c()
for(i in 1:length(BEET_CAP_GOSHEN)){
  BEET_TOTAL_CAP_WASHAKIE[i] <-  sum(BEET_CAP_WASHAKIE[1:i])
  BEET_TOTAL_CAP_GOSHEN[i] <-  sum(BEET_CAP_GOSHEN[1:i])
}
BEET_TOTAL_CAP <- BEET_TOTAL_CAP_WASHAKIE+BEET_TOTAL_CAP_GOSHEN
PROD_PER_WASHAKIE <- BEET_TOTAL_CAP_WASHAKIE/ifelse(BEET_TOTAL_CAP==0,1,BEET_TOTAL_CAP)
PROD_PER_GOSHEN <- BEET_TOTAL_CAP_GOSHEN/ifelse(BEET_TOTAL_CAP==0,1,BEET_TOTAL_CAP)

#########################Beet Production
PROD_GOSHEN <- PRODUCED_SGS*PROD_PER_GOSHEN
PROD_WASHAKIE <- PRODUCED_SGS*PROD_PER_WASHAKIE

VALUE_SGS_PRODUCED_GOSHEN <- PROD_GOSHEN*SBS_PURCHASE_PRICE
VALUE_SGS_PRODUCED_WASHAKIE <- PROD_GOSHEN*PROD_WASHAKIE

OP_COST_GOSHEN <- PROD_GOSHEN*(FACILITY_OP_EX+BEET_PURCHASE_PRICE)
OP_COST_WASHAKIE <- PROD_WASHAKIE*(FACILITY_OP_EX+BEET_PURCHASE_PRICE)



#Assume employment scales with production
WASH_EMP <- MAX_WASH_EMP*(PROD_WASHAKIE)/max(PROD_WASHAKIE)
GOSHEN_EMP <- MAX_GOSHEN_EMP*(PROD_GOSHEN)/max(PROD_GOSHEN)
#Total Compensation to employees
GOSHEN_COMP <- GOSHEN_WAGE*GOSHEN_EMP
WASH_COMP <- WASH_WAGE*WASH_EMP
#########################SBS Purchases
SBS_LOCAL_COST <- SBS_PURCHASE_PRICE*WYOMING_SUGAR_PURCHASES
SBS_NATIONAL_COST <- SBS_PURCHASE_PRICE*(TOTAL_SBS-WYOMING_SUGAR_PURCHASES-PRODUCED_SGS)
#########################Beet Purchases
GOSHEN_BEET_PURCHASE <- PROD_GOSHEN*BEET_PURCHASE_PRICE
WASHAKIE_BEET_PURCHASE <- PROD_WASHAKIE*BEET_PURCHASE_PRICE
#########################Skids
SKID_TOTAL_CONST_COST <- COST_TO_BUILD_SKID*CONSTRUCTED_SKIDS
SKID_EQUIP_COST <- SKID_TOTAL_CONST_COST*SKID_EQUIP_COST_RATIO #Cost of building the skid associated with equipment.
SKID_BUILD_COST <- SKID_TOTAL_CONST_COST*SKID_CONSTRUCTION_COST_RATIO #Cost of building the skid associated with general building costs.
  
NUM_SKIDS <- 0
ACTIVE_SKIDS <- c()
for(i in 1:length(CONSTRUCTED_SKIDS)){
  ACTIVE_SKIDS[i] <- NUM_SKIDS
  NUM_SKIDS <- NUM_SKIDS+CONSTRUCTED_SKIDS[i]
}
SKID_MAINT_COST <- ACTIVE_SKIDS*SKID_OP_COST #Cost to upkeep the active skids
#########################Transportation Costs
FROM_GOSHEN_COST <- TRUCK_COST*PROD_GOSHEN*GOSHEN_TO_CAMPBELL #Cost to ship SBS from Goshen processing facility to the final site.
FROM_WASHAKIE_COST <- TRUCK_COST*(PROD_WASHAKIE+WYOMING_SUGAR_PURCHASES)*WASHAKIE_TO_CAMPBELL #Cost to ship SBS from Wyoming sugar and the Washakie processing facility to the final site.
FROM_WESTON_COST <- TRUCK_COST*WESTON_TO_CAMPBELL*(TOTAL_SBS-WYOMING_SUGAR_PURCHASES-PRODUCED_SGS) #Cost to ship out of state SGS from the Weston rail depot to the skids.
OUT_STATE_SHIPPING_COST <- RAIL_COST*OUT_STATE_TO_WESTON*(TOTAL_SBS-WYOMING_SUGAR_PURCHASES-PRODUCED_SGS) #Cost to ship the out of state SGS to the rail depot in Weston (Upton,WY)
###############Tax Credit and voluntary Carbon Market
CARBON_ABATEMENT <- TOTAL_SBS*MTU_CARBON_PER_TON_BEET
#Shift by one year, so credits are acquired after one year of injection
CARBON_ABATEMENT_SHIFT <- c(0,CARBON_ABATEMENT[-length(CARBON_ABATEMENT)])
CARBON_TAX <- CARBON_ABATEMENT_SHIFT*CARBON_TAX_CREDIT_PRICE
VCM_CREDIT <- VCM_PRICE*CARBON_ABATEMENT
##################Adjustments
  #Negative commodity output shock to remove the beet spending from the facility (since it is modeled separately)
  BEET_REMOVAL_COEF_GOSHEN <- -PER_BEET_SPENDING*OP_COST_GOSHEN
  BEET_REMOVAL_COEF_WASHAKIE <- -PER_BEET_SPENDING*OP_COST_WASHAKIE
  #Negative transportation adjustments since direct transportation costs are modeled separately.
  GET_ADJ <- function(YEAR){
    FILES <- list.files("./Raw_Output/Detailed_Economic_Indicators/Prelim-Run/")
    FILE <- paste0("./Raw_Output/Detailed_Economic_Indicators/Prelim-Run/",FILES[grep(paste0("-",YEAR),FILES)])
    DF <- read_csv(FILE) %>% filter(ImpactType=="Direct",IndustryCode %in% c(397,399),TagName=="beet purchase")
    
    DF$County <-  gsub(" County, WY \\(2023\\)","",DF$DestinationRegion )
    DF$Year <- YEAR
    DF <- DF %>% select(Year, County, Industry=IndustryDescription,Output) %>% mutate(Output=-Output)
    return(DF)
  }
  YEARS_TO_PROC <- Year[c(-1,-7)]
  for(i in YEARS_TO_PROC){
    if(!exists("ADJUST_COSTS")){ADJUST_COSTS <- GET_ADJ(i)} else{ADJUST_COSTS <- rbind(ADJUST_COSTS,GET_ADJ(i))}
  }
WASHAKIE_TRUCK_ADJ <-  rbind(ADJUST_COSTS %>% filter(County=="Washakie"),c(2024,"Washakie","Truck transportation",0),c(2030,"Weston","Rail transportation",0)) %>% unique %>% arrange(Year) %>% pull(Output)
WESTON_RAIL_ADJ <-  rbind(ADJUST_COSTS %>% filter(County=="Weston",Industry=="Rail transportation"),c(2024,"Weston","Rail transportation",0),c(2030,"Weston","Rail transportation",0))%>% unique %>% arrange(Year) %>% pull(Output)
WESTON_TRUCK_ADJ <-    rbind(ADJUST_COSTS %>% filter(County=="Weston",Industry=="Truck transportation"),c(2024,"Weston","Truck transportation",0),c(2030,"Weston","Rail transportation",0))%>% unique %>% arrange(Year) %>% pull(Output)

DATA <- cbind(Year,CARBON_TAX,VCM_CREDIT,GAS_VALUE,RNG_ADD_VALUE,
      SBS_LOCAL_COST,SBS_NATIONAL_COST,GOSHEN_BEET_PURCHASE,WASHAKIE_BEET_PURCHASE,
      SKID_EQUIP_COST,SKID_BUILD_COST,SKID_MAINT_COST,
      BEET_CAP_WASHAKIE,BEET_TOTAL_CAP_GOSHEN,OP_COST_WASHAKIE,OP_COST_GOSHEN,WASH_EMP,GOSHEN_EMP,WASH_COMP,GOSHEN_COMP,PROD_WASHAKIE,PROD_GOSHEN,
      FROM_GOSHEN_COST,FROM_WASHAKIE_COST,FROM_WESTON_COST,OUT_STATE_SHIPPING_COST,
      WASHAKIE_TRUCK_ADJ,WESTON_RAIL_ADJ,WESTON_TRUCK_ADJ,BEET_REMOVAL_COEF_GOSHEN,BEET_REMOVAL_COEF_WASHAKIE     ) %>% as_tibble



