use crate::{
    error::Result, 
    models::{ApiResponse, BibleVerse}, 
    services::BibleVerseService,
    utils::response::success_response,
    AppState
};
use axum::{extract::{Query, State}, Json};
use serde::Deserialize;

#[derive(Deserialize)]
pub struct SearchQuery {
    pub q: String,
}

pub async fn random(
    State(state): State<AppState>,
) -> Result<Json<ApiResponse<BibleVerse>>> {
    let verse = BibleVerseService::get_random_v1(&state.pool).await?;
    
    Ok(Json(ApiResponse {
        success: true,
        data: verse,
        message: None,
    }))
}

pub async fn list(
    State(state): State<AppState>,
) -> Result<Json<ApiResponse<Vec<BibleVerse>>>> {
    let verses = BibleVerseService::list_v1(&state.pool).await?;
    
    Ok(success_response(verses))
}

pub async fn search(
    State(state): State<AppState>,
    Query(query): Query<SearchQuery>,
) -> Result<Json<ApiResponse<Vec<BibleVerse>>>> {
    let verses = BibleVerseService::search_v1(&state.pool, &query.q).await?;
    
    Ok(success_response(verses))
}
