library(metapsyData)
library(metapsyTools)
library(dplyr)
library(stringr)

d <- getData("bodily-distress-psyctr", version = "25.1.0")
dat <- d$data

instrument.recode <- c(
  "vas" = "VAS", "hdrs" = "HDRS", "ham-a" = "HAM-A", "fiq" = "FIQ",
  "euroqol-vas" = "EQ-VAS", "sss" = "SSS", "sf-36 physical functioning" = "SF-36 physical functioning",
  "physician global ratings" = "Physician global ratings", "ibs-sss" = "IBS-SSS",
  "hads-d" = "HADS-D", "hads-a" = "HADS-A", "sf-36" = "SF-36", "nrs" = "NRS",
  "phq-15" = "PHQ-15", "phq-9" = "PHQ-9", "gad-7" = "GAD-7", "wsas" = "WSAS",
  "mpq" = "MPQ", "bpi" = "BPI", "bdi" = "BDI", "stai" = "STAI", "fss" = "FSS",
  "bai" = "BAI", "sf-12" = "SF-12", "bsi-18 somatization" = "BSI-18 somatization",
  "bsi-18 depression" = "BSI-18 depression", "bsi-18 anxiety" = "BSI-18 anxiety",
  "ibs-qol" = "IBS-QoL", "cfq" = "CFQ", "gi symptoms" = "GI symptoms",
  "stai state" = "STAI state", "ibs-qol health concern" = "IBS-QoL health concern",
  "gsrs-ibs" = "GSRS-IBS", "dass-21 depression subscale" = "DASS-21 depression subscale",
  "dass-21 anxiety subscale" = "DASS-21 anxiety subscale", "cis" = "CIS", "ibssi" = "IBSSI",
  "bdi-II" = "BDI-II", "sf-36 pcs" = "SF-36 PCS", "bsss-ibs" = "BSSS-IBS",
  "ibs-sss daily life disability subscale" = "IBS-SSS daily life disability subscale",
  "mpi-1 pain intensity" = "MPI-1 pain intensity", "madrs" = "MADRS",
  "mpi-1 interference" = "MPI-1 interference", "sf-8 physical" = "SF-8 physical",
  "mpq-sf vas" = "MPQ-SF VAS", "scl-90-r depression" = "SCL-90-R depression",
  "scl-90-r anxiety" = "SCL-90-R anxiety", "vas pain" = "VAS pain", "eq-5d" = "EQ-5D",
  "bsi-6" = "BSI-6", "wi" = "WI", "staiy1" = "STAI-Y1", "pdi" = "PDI", "fsi" = "FSI",
  "ces-d" = "CES-D", "gsrs" = "GSRS", "dass-42 depr" = "DASS-42 depression",
  "dass-42 anx" = "DASS-42 anxiety", "soms" = "SOMS", "bpi-severity" = "BPI severity",
  "bpi-inteference" = "BPI interference", "promis-si" = "PROMIS-SI", "promis-pf" = "PROMIS-PF",
  "promis-pi" = "PROMIS-PI", "bds checklist" = "BDS checklist", "scl-92 som" = "SCL-92 somatization",
  "scl-92 depr" = "SCL-92 depression", "scl-92 anx" = "SCL-92 anxiety", "wi-7" = "WI-7",
  "who-das II" = "WHO-DAS II", "fsdc" = "FSDC", "fiq-r" = "FIQ-R", "sip" = "SIP",
  "pf-10" = "PF-10", "pain diary" = "Pain diary", "gcq" = "GCQ", "plc" = "PLC",
  "soms-7" = "SOMS-7", "sf-mpq" = "SF-MPQ", "mpi pain intensity" = "MPI pain intensity",
  "gi scale pain" = "GI scale pain", "stai trait" = "STAI trait", "pcs" = "PCS",
  "gi symptom diary" = "GI symptom diary", "stpi-state" = "STPI state",
  "scl-90-som" = "SCL-90 somatization", "scl-90-depression" = "SCL-90 depression",
  "scl-90-anx" = "SCL-90 anxiety", "sf-36 physical compnent" = "SF-36 physical component"
)

dat.filtered <- dat %>%
  rename(outcome_hierarchy = outcome_hierachy, n_sessions_arm1 = no_sessions_arm1,
         rob = rob_overall, d1 = rob_domain1, d2 = rob_domain2, d3 = rob_domain3,
         d4 = rob_domain4, d5 = rob_domain5) %>%
  mutate(
    study = str_to_title(study),
    time = str_to_title(time),
    year = sub(".*, (\\d{4}).*", "\\1", study) %>% as.numeric(),
    n = n_arm1 + n_arm2 %>% as.numeric(),
    instrument = if_else(
      instrument %in% names(instrument.recode),
      recode(instrument, !!!instrument.recode),
      str_to_title(instrument)
    )
  ) %>%
  filterPoolingData(
    outcome_hierarchy == "primary",
    !is.na(diagnosis_which),
    outcome_domain %in% c("som", "anx", "depr", "ha", "phs"))

out <- dat.filtered
out <- out %>% mutate(.g = if_else(outcome_domain == "phs", .g, .g * -1))
BodilyDistressDB <- list(dat = out, metadata = d$returnMetadata())
save(BodilyDistressDB,
     file = "www/data/BodilyDistressDB.rda")