#' Build JAGS Model String (Binary M & Continuous Y)
#'
#' Constructs the JAGS model specification for a Bayesian mediation model
#' where mediators are binary (0/1) and the outcome is continuous.
#'
#' @description
#' This internal function generates a JAGS model string using logistic
#' regression (logit link) for the binary mediators and a Gaussian
#' likelihood for the continuous outcome. It incorporates spike-and-slab
#' variable selection on all mediation pathways.
#'
#' @param P Integer. The number of predictors/covariates.
#' @param K Integer. The number of mediators.
#' @param parms A \code{data.frame} generated by \code{\link{make_parms_main}}
#' containing columns: \code{priors}, \code{distribution}, \code{arguments},
#' and \code{template}.
#'
#' @return A character string containing the full JAGS model specification.
#'
#' @details
#' The model structure for binary mediators:
#' \itemize{
#'   \item \strong{Mediator Models:} \eqn{M_k \sim Bernoulli(p_{m_k})}, where
#'   \eqn{logit(p_{m_k})} is a linear combination of predictors \eqn{X}.
#'   \item \strong{Outcome Model:} \eqn{Y \sim Normal(\mu_y, \tau_y)}, where
#'   \eqn{\mu_y} includes both direct effects from \eqn{X} and indirect
#'   effects from binary \eqn{M}.
#'   \item \strong{Precision:} Note that \code{m.prec} is not used in this
#'   formulation as the mediators are binary.
#' }
#'
#' @seealso \code{\link{buzzEBMcatMcontY}}, \code{\link{define_init_values}}
#'
#' @keywords internal

build_ebmed_model_mcat_ycont <- function(P, K, parms) {

  #Set up prior strings
  prior_strings <- mapply(function(p, d, a, t) {
    sprintf(t, p, d, a)
  }, parms$priors, parms$distribution, parms$arguments, parms$template)
  names(prior_strings) <- parms$priors

  # Create a and b effect blocks
  a_effect_string <- ""
  b_effect_string <- ""

  for (k in 1:K) {
    a_effect_string <- paste0(
      a_effect_string,
      "    logit(p.m", k, "[i]) <- inprod(X[i, ], a[", k, ", ])\n",
      "    m", k, "[i] ~ dbern(p.m", k, "[i])\n\n"
    )
    b_effect_string <- paste0(
      b_effect_string,
      "+ inprod(m",k,"[i],b[",k,"])"
    )
  }


  # Create actual model string
  modelstring <- paste0("
model {

  ## a effects (X -> mediators)
  for (i in 1:N) {
", a_effect_string, "
  }

  for (j in 1:", K, ") {
    a.pip[j] ~ dbern(a.pip.hyperprior)
    for (p in 1:", P, ") {
      a[j, p] <- a.pip[j] * a.coef[j, p]
      a.coef[j, p] ~ dnorm(0, a.coef.hyperprec)
    }
  }

  ## b effects (mediators -> y)
  for (i in 1:N) {
    y[i] ~ dnorm(mu.y[i], y.prec)
    mu.y[i] <- inprod(X[i, ], direct.coef[])",
                        b_effect_string, "
  }

  for (j in 1:", K, ") {
    b.pip[j] ~ dbern(b.pip.hyperprior)
    b[j] <- b.pip[j] * b.coef[j]
    b.coef[j] ~ dnorm(0, b.coef.hyperprec)
  }

  ## Direct effects for predictors
  for (p in 1:", P, ") {
  ",prior_strings["direct.coef"],"
  }

  ## Hyperparameters
  ", prior_strings["y.prec"], "
  ", prior_strings["a.pip.hyperprior"], "
  ", prior_strings["b.pip.hyperprior"], "
  a.coef.hyperprec ~ dgamma(1, 0.001)
  b.coef.hyperprec ~ dgamma(1, 0.001)

  ## Joint inclusion indicators
  ind.joint <- a.pip * b.pip
}
")

  return(modelstring)
}
