use crate::error::Result;
use crate::models::{ScheduleV2, ApiResponse, ConferenceData};
use crate::utils::{
    response::success_response,
    datetime::parse_date,
};
use crate::services::ScheduleService;
use axum::{
    extract::{Query, State},
    Json,
};
use serde::Deserialize;
use crate::AppState;

#[derive(Deserialize)]
pub struct ScheduleQuery {
    date: Option<String>,
    // Note: timezone support removed - V2 uses UTC timestamps, client handles timezone conversion
}

pub async fn get_schedule(
    State(state): State<AppState>,
    Query(query): Query<ScheduleQuery>,
) -> Result<Json<ApiResponse<ScheduleV2>>> {
    let date = if let Some(date_str) = &query.date {
        parse_date(date_str)?
    } else {
        chrono::Utc::now().date_naive()
    };
    
    let schedule = ScheduleService::get_schedule_v2(&state.pool, &date).await?
        .ok_or_else(|| crate::error::ApiError::NotFound("Schedule not found".to_string()))?;
    
    Ok(success_response(schedule))
}

pub async fn get_conference_data(
    State(state): State<AppState>,
    Query(query): Query<ScheduleQuery>,
) -> Result<Json<ApiResponse<ConferenceData>>> {
    let date = if let Some(date_str) = &query.date {
        parse_date(date_str)?
    } else {
        chrono::Utc::now().date_naive()
    };
    
    let conference_data = ScheduleService::get_conference_data_v2(&state.pool, &date).await?;
    
    Ok(success_response(conference_data))
}