use axum::{extract::State, Json};

use crate::{
    error::Result,
    models::{LoginRequest, LoginResponse, User, ApiResponse},
    services::AuthService,
    utils::response::success_response,
    AppState,
};

pub async fn login(
    State(state): State<AppState>,
    Json(req): Json<LoginRequest>,
) -> Result<Json<ApiResponse<LoginResponse>>> {
    let login_response = AuthService::login(&state.pool, req, &state.jwt_secret).await?;
    
    Ok(Json(ApiResponse {
        success: true,
        data: Some(login_response),
        message: Some("Login successful".to_string()),
    }))
}

pub async fn list_users(
    State(state): State<AppState>,
) -> Result<Json<ApiResponse<Vec<User>>>> {
    let users = AuthService::list_users(&state.pool).await?;
    
    Ok(success_response(users))
}
