use axum::{extract::rejection::JsonRejection, http::StatusCode};
use sea_orm::DbErr;

use app::error::UserError;

use super::traits::HTTPError;

impl HTTPError for JsonRejection {
    fn to_status_code(&self) -> StatusCode {
        match self {
            JsonRejection::JsonSyntaxError(_) => StatusCode::BAD_REQUEST,
            _ => StatusCode::BAD_REQUEST,
        }
    }
}

impl HTTPError for DbErr {
    fn to_status_code(&self) -> StatusCode {
        match self {
            DbErr::ConnectionAcquire(_) => StatusCode::INTERNAL_SERVER_ERROR,
            DbErr::UnpackInsertId => StatusCode::CONFLICT,
            DbErr::RecordNotFound(_) => StatusCode::NOT_FOUND,
            DbErr::Custom(s) if s == "Users cannot follow themselves" => StatusCode::BAD_REQUEST,
            _ => StatusCode::INTERNAL_SERVER_ERROR, // TODO:: more granularity
        }
    }
}

impl HTTPError for UserError {
    fn to_status_code(&self) -> StatusCode {
        match self {
            UserError::NotFound => StatusCode::NOT_FOUND,
            UserError::NotFollowing => StatusCode::NOT_FOUND,
            UserError::Forbidden => StatusCode::FORBIDDEN,
            UserError::UsernameTaken => StatusCode::BAD_REQUEST,
            UserError::AlreadyFollowing => StatusCode::BAD_REQUEST,
            UserError::Validation(_) => StatusCode::UNPROCESSABLE_ENTITY,
            UserError::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
        }
    }
}
