/*
   Module `ports` specifies the API by which external modules interact with the user domain.

   All traits are bounded by `Send + Sync + 'static`, since their implementations must be shareable
   between request-handling threads.

   Trait methods are explicitly asynchronous, including `Send` bounds on response types,
   since the application is expected to always run in a multithreaded environment.
*/

use std::future::Future;

use super::models::user::*;

/// `ApiService` is the public API for the user domain.
///
/// External modules must conform to this contract – the domain is not concerned with the
/// implementation details or underlying technology of any external code.
pub trait ApiService: Clone + Send + Sync + 'static {
    /// Asynchronously create a new [User].
    ///
    /// # Errors
    ///
    /// - [CreateUserError::Duplicate] if an [User] with the same [EmailAddress] already exists.
    fn create_user(
        &self,
        req: CreateUserRequest,
    ) -> impl Future<Output = Result<User, CreateUserError>> + Send;

    // fn activate_user

    fn get_user_session(
        &self,
        session: &axum_session::SessionAnySession, // TODO: Get rid of this and make cleaner
    ) -> impl Future<Output = Option<User>> + Send;

    fn activate_user_account(
        &self,
        token: ActivationToken,
    ) -> impl Future<Output = Result<User, ActivateUserError>> + Send;

    fn user_login(
        &self,
        req: UserLoginRequest,
    ) -> impl Future<Output = Result<User, UserLoginError>> + Send;

    fn forgot_password(&self, email: &EmailAddress) -> impl Future<Output = ()> + Send;

    fn reset_password(
        &self,
        token: &PasswordResetToken,
        password: &Password,
    ) -> impl Future<Output = Result<User, ResetPasswordError>> + Send;

    fn find_user_by_password_reset_token(
        &self,
        token: &PasswordResetToken,
    ) -> impl Future<Output = Option<User>> + Send;

    // These shouldnt be here, _why_ are they here, and implement that here instead
    // fn find_user_by_email(self, email: EmailAddress) -> impl Future<Output = Option<User>> + Send;
    // fn find_user_by_id(&self, user_id: uuid::Uuid) -> impl Future<Output = Option<User>> + Send;
}

pub trait UserRepository: Clone + Send + Sync + 'static {
    // Create
    fn create_user(
        &self,
        req: CreateUserRequest,
    ) -> impl Future<Output = Result<User, CreateUserError>> + Send;

    fn create_activation_token(
        &self,
        ent: &User,
    ) -> impl Future<Output = Result<ActivationToken, anyhow::Error>> + Send;

    fn create_password_reset_token(
        &self,
        ent: &User,
    ) -> impl Future<Output = Result<PasswordResetToken, anyhow::Error>> + Send;

    // Read
    fn all_users(&self) -> impl Future<Output = Vec<User>> + Send;

    fn find_user_by_id(
        &self,
        id: uuid::Uuid,
    ) -> impl Future<Output = Result<Option<User>, anyhow::Error>> + Send;

    fn find_user_by_email(
        &self,
        email: &EmailAddress,
    ) -> impl Future<Output = Result<Option<User>, anyhow::Error>> + Send;

    fn find_user_by_activation_token(
        &self,
        token: &ActivationToken,
    ) -> impl Future<Output = Result<Option<User>, anyhow::Error>> + Send;

    fn find_user_by_password_reset_token(
        &self,
        token: &PasswordResetToken,
    ) -> impl Future<Output = Result<Option<User>, anyhow::Error>> + Send;

    // // Update
    fn update_user(
        &self,
        ent: &User,
        req: UpdateUserRequest,
    ) -> impl Future<Output = Result<(User, User), UpdateUserError>> + Send;

    // Delete
    // fn delete_user(&self, ent: User) -> impl Future<Output = Result<User, DeleteUserError>> + Send;
    fn delete_activation_token_for_user(
        &self,
        ent: &User,
    ) -> impl Future<Output = Result<(), anyhow::Error>> + Send;

    fn delete_password_reset_tokens_for_user(
        &self,
        ent: &User,
    ) -> impl Future<Output = Result<(), anyhow::Error>> + Send;
}

pub trait UserNotifier: Clone + Send + Sync + 'static {
    fn user_created(&self, user: &User, token: &ActivationToken)
        -> impl Future<Output = ()> + Send;
    fn forgot_password(
        &self,
        user: &User,
        token: &PasswordResetToken,
    ) -> impl Future<Output = ()> + Send;
}
