model{
  for (i in 1:p){
    for (j in 1:A){
      for (k in 1:(T+h)){
        dxt[i,j,k]~dpois(q[i,j,k]*ext[i,j,k])
        q[i,j,k]<-exp(alpha[j]+(beta[j]) * (kappa[k]+c_p[i]))
      }
    }
  }

  for (j in 2:T){
  kappa[j]<-kappa_rest[j-1]
  }

  kappa[1]<--sum(kappa_rest)

  for (k in 1:h){
    kappa[k+T]<-kappa_forecast_rest[k]
  }

    for (j in 2:(T-1)){
    prior_mean_kappa[j]<-eta_mean[j+1]+rho*(kappa_rest[j-1]-eta_mean[j])	
    kappa_rest[j]~dnorm(prior_mean_kappa[j],i_sigma2_kappa)
    }
    prior_mean_kappa[1]<-eta_mean[2]+0 # the 0 should have been rho*(kappa[1]-eta_mean[1]) but it does not work because the model could not be uniquely defined	
    kappa_rest[1]~dnorm(prior_mean_kappa[1],i_sigma2_kappa)

    for (j in 2:h){
    prior_mean_kappa_forecast[j]<-eta_mean[T+j]+rho*(kappa_forecast_rest[j-1]-eta_mean[T+j-1])	
    kappa_forecast_rest[j]~dnorm(prior_mean_kappa_forecast[j],i_sigma2_kappa)
    }
    prior_mean_kappa_forecast[1]<-eta_mean[T+1]+rho*(kappa_rest[T-1]-eta_mean[T])	
    kappa_forecast_rest[1]~dnorm(prior_mean_kappa_forecast[1],i_sigma2_kappa)

  eta_mean<-c(matrix_kappa_X%*%eta)

  for (m in 1:A){
      alpha[m]~dnorm(0,0.001)
    }

  beta<-c(beta1,beta_rest)
  beta1<-1-sum(beta_rest)
  beta_rest~dmnorm(prior_mean_beta,prior_prec_beta)

  c_p<-c(-sum(c_p_rest),c_p_rest)
  for (m in 1:(p-1)){  
  c_p_rest[m]~dnorm(0,0.001)
  }

  eta~dmnorm(prior_mean_eta,prior_prec_eta) #supply prior_prec_eta<-solve(matrix(c(400,0,0,2),nrow=2));prior_mean_eta<-c(0,0)
  rho~dnorm(0,1) T(-1,1)
  sigma2_kappa<-1/i_sigma2_kappa
  i_sigma2_kappa~dgamma(1,0.0001)

}
