## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  warning  = FALSE,
  message  = FALSE
)
library(swcEcon)

## ----bcr----------------------------------------------------------------------
r <- calc_bcr(
  investment    = 20,     # INR 20 lakh capital cost
  annual_benefit = 6,    # INR 6 lakh per year
  annual_omc    = 0.8,   # INR 0.8 lakh O&M per year
  life          = 20,    # 20-year design life
  discount_rate = 0.12   # 12% discount rate (GoI 2008)
)
print(r)

## ----npv----------------------------------------------------------------------
n <- calc_npv(
  investment    = 20,
  annual_benefit = 6,
  annual_omc    = 0.8,
  life          = 20,
  discount_rate = 0.12
)
print(n)
head(n$cashflows)

## ----irr----------------------------------------------------------------------
i <- calc_irr(
  investment    = 20,
  annual_benefit = 6,
  annual_omc    = 0.8,
  life          = 20
)
print(i)

## ----pbp----------------------------------------------------------------------
p <- calc_pbp(
  investment    = 20,
  annual_benefit = 6,
  annual_omc    = 0.8
)
print(p)

## ----mrr----------------------------------------------------------------------
m <- calc_mrr(
  nb_with      = 18000,   # net benefit per ha with contour bund
  nb_without   = 11000,   # net benefit per ha current practice
  cost_with    = 16000,   # variable cost per ha with SWC
  cost_without = 11500    # variable cost per ha current practice
)
print(m)

## ----mbcr---------------------------------------------------------------------
calc_mbcr(total_benefit = 80, operating_cost = 12, capital_cost = 20)

## ----soil---------------------------------------------------------------------
data(usle_india_soils)
K_vert <- usle_india_soils[
  usle_india_soils$soil_series == "Vertisols", "k_mean"]

s <- calc_soil_loss_cost(
  R      = 720,      # R-factor from rainfall_erosivity_india (Pune)
  K      = K_vert,   # K-factor from usle_india_soils
  LS     = 4.2,      # slope length-gradient factor
  C_pre  = 0.35,     # cover factor before contour bund
  C_post = 0.18,     # cover factor after contour bund
  P_pre  = 1.0,      # no support practice before
  P_post = 0.5,      # support practice P after bunding
  area_ha = 500      # watershed area
)
print(s)

## ----nutrient-----------------------------------------------------------------
data(usle_india_soils)
soil <- usle_india_soils[usle_india_soils$soil_series == "Vertisols", ]

calc_nutrient_cost(
  soil_loss_t_ha = s$soil_loss_pre,
  area_ha        = 500,
  n_kg_per_t     = soil$n_kg_per_t,
  p_kg_per_t     = soil$p_kg_per_t,
  k_kg_per_t     = soil$k_kg_per_t
)

## ----water--------------------------------------------------------------------
data(rainfall_erosivity_india)
rf_pune <- rainfall_erosivity_india[
  rainfall_erosivity_india$district == "Pune", "annual_rf_mm"]

w <- calc_water_value(
  area_ha        = 500,
  rainfall_mm    = rf_pune,
  rc_pre         = 0.35,   # runoff coefficient before SWC
  rc_post        = 0.20,   # runoff coefficient after SWC
  harvest_pct    = 45,     # percentage of reduced runoff harvested
  gw_recharge_pct = 20,   # percentage percolating to groundwater
  water_value_m3 = 3.5    # INR per cubic metre (Joshi et al. 2005)
)
print(w)

## ----irrigation---------------------------------------------------------------
calc_irrigation_benefit(
  irrig_area_ha       = 80,
  yield_increase_t_ha = 1.6,
  crop_price_inr_t    = 18000,
  input_cost_inr_ha   = 8000
)

## ----employment---------------------------------------------------------------
calc_employment(
  employment_days = 45000,   # total person-days
  investment_lakh = 50,      # INR 50 lakh project cost
  wages_per_day   = 250      # daily wage rate (INR)
)

## ----sensitivity--------------------------------------------------------------
sa <- sensitivity_analysis(
  investment    = 20,
  annual_benefit = 6,
  annual_omc    = 0.8,
  life          = 20,
  discount_rate = 0.12,
  cost_range_pct    = 20,
  benefit_range_pct = 20,
  rate_range_pct    = 3
)
print(sa)

## ----switching----------------------------------------------------------------
sv <- calc_switching_value(
  investment    = 20,
  annual_benefit = 6,
  annual_omc    = 0.8,
  life          = 20,
  discount_rate = 0.12
)
print(sv)

## ----mc, eval = FALSE---------------------------------------------------------
# mc <- monte_carlo_swc(
#   inv_mean = 20, inv_cv  = 0.10,
#   ben_mean = 6,  ben_cv  = 0.15,
#   omc_mean = 0.8, omc_cv = 0.20,
#   life_min = 15, life_max = 25,
#   r_min    = 0.10, r_max = 0.14,
#   n_sim    = 5000, seed  = 42
# )
# print(mc)

## ----benchmarks---------------------------------------------------------------
data(swc_benchmarks)
swc_benchmarks[, c("state", "agro_zone", "bcr_typical",
                    "irr_pct", "pbp_years")]

## ----soils_table--------------------------------------------------------------
data(usle_india_soils)
usle_india_soils[, c("soil_series", "soil_order",
                      "k_mean", "t_value", "n_kg_per_t")]

## ----erosivity----------------------------------------------------------------
data(rainfall_erosivity_india)
rainfall_erosivity_india[, c("district", "state",
                              "annual_rf_mm", "r_factor")]

## ----costs--------------------------------------------------------------------
data(swc_cost_norms)
swc_cost_norms[, c("measure", "norm_2024_inr",
                    "design_life_yr", "labour_pct")]

## ----pipeline, eval = FALSE---------------------------------------------------
# pl <- run_swc_pipeline(
#   investment     = 20,
#   annual_benefit = 6,
#   annual_omc     = 0.8,
#   life           = 20,
#   discount_rate  = 0.12,
#   project_name   = "Hypothetical Check Dam, Semi-arid India",
#   include_sensitivity = TRUE,
#   include_monte_carlo = FALSE
# )
# print(pl)
# 
# # AFTER
# generate_swc_report(
#   pl,
#   output_file  = "swcEcon_appraisal.html",
#   title        = "Economic Appraisal: Hypothetical Watershed",
#   author       = "Your Name",
#   organisation = "Your Organisation"
# )
# 

