library(tidyverse)
#R script to quickly find IMPLAN inputs or adjustment factors
GET_INPUTS_GAS <- 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)])
  #Dollar value added to the RNG in addition to the main production
  RNG_ADD_YEARS <- c(0,380102,3097001,5943721,11224037,18050255,18050255)
 RNG_ADD <-  RNG_ADD_YEARS[YEAR-2023]
  DF <- read_csv(FILE) %>% filter(ImpactType=="Direct",EventName=="Gas Production (Campbell)")
  DF <- DF %>% mutate(IntermediateInputs=Output-EmployeeCompensation-ProprietorIncome-OtherPropertyIncome-TaxesOnProductionAndImports)%>% select(WageAndSalaryEmployment,EmployeeCompensation,Output,ProprietorEmployment,OtherPropertyIncome,Employment,IntermediateInputs)%>% mutate(Output=Output+RNG_ADD)
  return(DF)
}
#Find the input values for the beet factories
GET_INPUTS_BEET <- function(YEAR,OP_COST=75){
  MAX_WASH_EMP <- 8
  MAX_GOSHEN_EMP <-15
  GOSHEN_WAGE <- 53007.10
  WASH_WAGE <- 71017.25
  
  WASH_PROD <- c(0,0,0,35000,37500,50000,50000)
  GOSHEN_PROD <- c(0,0,0,52500,112500,150000,150000)
  
  GOSHEN_OP_COST <- OP_COST*GOSHEN_PROD
  WASH_OP_COST <- OP_COST*WASH_PROD
  
  GOSHEN_TAX_CRED <- GOSHEN_PROD*0.62*90
  WASH_TAX_CRED <- WASH_PROD*0.62*90
 
  WASH_EMP <- MAX_WASH_EMP*(WASH_PROD)/max(WASH_PROD)
  GOSHEN_EMP <- MAX_GOSHEN_EMP*(GOSHEN_PROD)/max(GOSHEN_PROD)
  TOPI <- GOSHEN_EMP*0
  OTH_PROP <- GOSHEN_EMP*0

  GOSHEN_COMP <- GOSHEN_WAGE*GOSHEN_EMP
  WASH_COMP <- WASH_WAGE*WASH_EMP
 
  WASH_RETURN <- WASH_TAX_CRED-WASH_COMP-WASH_OP_COST
  GOSHEN_RETURN <- GOSHEN_TAX_CRED-GOSHEN_COMP-GOSHEN_OP_COST
  RES <- rbind(as.numeric(cbind(WASH_EMP,WASH_COMP,WASH_RETURN,TOPI,OTH_PROP,WASH_OP_COST)[YEAR-2023,]),as.numeric(cbind(GOSHEN_EMP,GOSHEN_COMP,GOSHEN_RETURN,TOPI,OTH_PROP,GOSHEN_OP_COST)[YEAR-2023,])) %>% as_tibble
  colnames(RES) <- c("Wage_Emp","Compensation","Proprietor_Income","TOPI","OTHER_PROP","Inter_Inputs")
  RES$County <- c("Washakie","Goshen")
  RES <- RES %>% select(County,everything())

  return(RES)
}

4415*c(0.19,0.06)

#Pull the truck and rail transportation induced, to remove it from IMPLAN results. Those costs are explicitly modeled.
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 <- DF %>% select(County, Industry=IndustryDescription,Output) %>% mutate(Output=-Output)
  return(DF)
}

GET_INPUTS_GAS(2028)
GET_INPUTS_BEET(2029,10)
GET_ADJ(2027)