#[derive(Deserialize, Debug)]
pub struct GetMessagesValues {
    pub conv_id: u32,
    pub timestamp: Option<String>,
}


pub async fn get_message(
    State(state): State<AppState>,
    AuthClaims { user_id, hotel_id, username }: AuthClaims,
    Json(payload): Json<GetMessagesValues>, // ✅ no custom payload wrapper
) -> Result<impl IntoResponse, (StatusCode, String)>  {

      let pool = state.hotel_pools.get_pool(hotel_id);

    let conn = pool.get()
        .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Pool error".to_string()))?;

    let from_time = match payload.timestamp.as_deref() {
        Some("0") | None => "1970-01-01 00:00:00",
        Some(ts) => ts,
    };

    let mut stmt = conn.prepare(
        "SELECT id, sender_id, content, sent_at
         FROM message
         WHERE conversation_id = ?1
           AND sent_at > ?2
         ORDER BY sent_at DESC
         LIMIT 50"
    ).map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Prepare failed".to_string()))?;

    let messages = stmt.query_map(
        params![payload.conv_id, from_time],
        |row| {
            Ok(Message {
                id: row.get(0)?,
                sender_id: row.get(1)?,
                content: row.get(2)?,
                sent_at: row.get(3)?,
            })
        }
    )
    .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Query failed".to_string()))?
    .collect::<Result<Vec<_>, _>>()
    .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Collect failed".to_string()))?;
  

}


