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

#[derive(Deserialize)]
pub struct BibleVerseQuery {
    // Note: timezone support removed - V2 uses UTC timestamps, client handles timezone conversion
}

#[derive(Deserialize)]
pub struct SearchQuery {
    pub q: String,
    // Note: timezone support removed - V2 uses UTC timestamps, client handles timezone conversion
}

pub async fn get_random(
    State(state): State<AppState>,
    Query(_query): Query<BibleVerseQuery>,
) -> Result<Json<ApiResponse<BibleVerseV2>>> {
    let verse = BibleVerseService::get_random_v2(&state.pool).await?
        .ok_or_else(|| crate::error::ApiError::NotFound("No bible verses available".to_string()))?;
    
    Ok(success_response(verse))
}

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

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