#' Build JAGS Model String (Continuous M & Binary Y)
#'
#' Constructs the JAGS model specification for a Bayesian mediation model
#' where mediators are continuous and the outcome is binary (0/1).
#'
#' @description
#' This internal function generates a JAGS model string that uses Gaussian
#' likelihoods for the mediators and a logistic regression (logit link)
#' for the binary 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 uses a mixed-likelihood approach:
#' \itemize{
#'   \item \strong{Mediator Models:} \eqn{M_k \sim Normal(\mu_{M_k}, \tau_{M_k})}.
#'   Variable selection is applied to the \eqn{a}-paths from \eqn{X}.
#'   \item \strong{Outcome Model:} \eqn{Y \sim Bernoulli(p_y)}, where
#'   \eqn{logit(p_y)} is a linear combination of predictors and mediators.
#'   \item \strong{Precision:} Note that \code{y.prec} is not used in this
#'   formulation as the outcome is binary.
#' }
#'
#' @seealso \code{\link{buzzEBMcontMcatY}}, \code{\link{define_init_values}}
#'
#' @keywords internal


build_ebmed_model_mcont_ycat <- 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

  # Generate dynamic mediator loops and outcome string
  a_effect_string <- ""
  b_effect_string <- ""

  for (k in 1:K) {
    # For mediators: logit(p[i]) = X * a
    a_effect_string <- paste0(
      a_effect_string,
      "m", k, "[i] ~ dnorm(mu.m", k, "[i], m.prec[", k, "])\n",
      "mu.m", k, "[i] <- inprod(X[i, ], a[", k, ",])\n\n"
    )
    b_effect_string <- paste0(
      b_effect_string,
      " + (m", k, "[i] * b[", k, "])"
    )
  }

  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]
    ", prior_strings["a.coef"],"
    }
    ", prior_strings["m.prec"],"
  }

  ## b effects (Mediators -> Binary Outcome y)
  for (i in 1:N) {
    y[i] ~ dbern(prob.y[i])
    logit(prob.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]
  ", prior_strings["b.coef"],"
  }

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

  ## Hyperparameters
  ", prior_strings["a.pip.hyperprior"], "
  ", prior_strings["b.pip.hyperprior"], "

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

  return(modelstring)
}
