use axum::Json;
use axum::extract::State;
use serde_json::Value;
use sqlx::SqlitePool;

pub async fn get_articles(State(pool): State<SqlitePool>) -> Result<Json<Value>, AppError> {
    // TODO: Article logic
    Ok(Json(serde_json::json!({"articles": []})))
}

pub async fn get_summaries(State(pool): State<SqlitePool>) -> Result<Json<Value>, AppError> {
    // TODO: Summaries logic
    Ok(Json(serde_json::json!({"summaries": []})))
}

use axum::{
    http::StatusCode,
    response::{IntoResponse, Response},
};

pub struct AppError(anyhow::Error);

impl IntoResponse for AppError {
    fn into_response(self) -> Response {
        let (status, message) = match self.0.downcast_ref::<sqlx::Error>() {
            Some(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Database error occurred"),
            None => (StatusCode::INTERNAL_SERVER_ERROR, "An error occurred"),
        };

        tracing::error!("API Error: {:?}", self.0);
        (status, message).into_response()
    }
}

impl<E> From<E> for AppError
where
    E: Into<anyhow::Error>, {
    fn from(err: E) -> Self {
        Self(err.into())
    }
}
