//! Channel member queries for user presence in channels.

use sqlx::PgExecutor;
use uuid::Uuid;

use crate::models::{ChannelMember, ChannelMemberInfo};
use chattyness_error::AppError;

/// Join a channel as an authenticated user.
///
/// Creates a channel_members entry with default position (400, 300).
pub async fn join_channel<'e>(
    executor: impl PgExecutor<'e>,
    channel_id: Uuid,
    user_id: Uuid,
) -> Result<ChannelMember, AppError> {
    let member = sqlx::query_as::<_, ChannelMember>(
        r#"
        INSERT INTO realm.channel_members (channel_id, user_id, position)
        VALUES ($1, $2, ST_SetSRID(ST_MakePoint(400, 300), 0))
        ON CONFLICT (channel_id, user_id) DO UPDATE
        SET joined_at = now()
        RETURNING
            id,
            channel_id,
            user_id,
            guest_session_id,
            ST_X(position) as position_x,
            ST_Y(position) as position_y,
            facing_direction,
            is_moving,
            is_afk,
            joined_at,
            last_moved_at
        "#,
    )
    .bind(channel_id)
    .bind(user_id)
    .fetch_one(executor)
    .await?;

    Ok(member)
}

/// Ensure an active avatar exists for a user in a realm.
/// Uses the user's default avatar (slot 0) if none exists.
pub async fn ensure_active_avatar<'e>(
    executor: impl PgExecutor<'e>,
    user_id: Uuid,
    realm_id: Uuid,
) -> Result<(), AppError> {
    sqlx::query(
        r#"
        INSERT INTO props.active_avatars (user_id, realm_id, avatar_id, current_emotion)
        SELECT $1, $2, id, 0
        FROM props.avatars
        WHERE user_id = $1 AND slot_number = 0
        ON CONFLICT (user_id, realm_id) DO NOTHING
        "#,
    )
    .bind(user_id)
    .bind(realm_id)
    .execute(executor)
    .await?;

    Ok(())
}

/// Leave a channel.
pub async fn leave_channel<'e>(
    executor: impl PgExecutor<'e>,
    channel_id: Uuid,
    user_id: Uuid,
) -> Result<(), AppError> {
    sqlx::query(
        r#"DELETE FROM realm.channel_members WHERE channel_id = $1 AND user_id = $2"#,
    )
    .bind(channel_id)
    .bind(user_id)
    .execute(executor)
    .await?;

    Ok(())
}

/// Update a user's position in a channel.
pub async fn update_position<'e>(
    executor: impl PgExecutor<'e>,
    channel_id: Uuid,
    user_id: Uuid,
    x: f64,
    y: f64,
) -> Result<(), AppError> {
    let result = sqlx::query(
        r#"
        UPDATE realm.channel_members
        SET position = ST_SetSRID(ST_MakePoint($3, $4), 0),
            last_moved_at = now(),
            is_moving = true
        WHERE channel_id = $1 AND user_id = $2
        "#,
    )
    .bind(channel_id)
    .bind(user_id)
    .bind(x)
    .bind(y)
    .execute(executor)
    .await?;

    if result.rows_affected() == 0 {
        return Err(AppError::NotFound("Channel member not found".to_string()));
    }

    Ok(())
}

/// Get all members in a channel with their display info and current emotion.
pub async fn get_channel_members<'e>(
    executor: impl PgExecutor<'e>,
    channel_id: Uuid,
    realm_id: Uuid,
) -> Result<Vec<ChannelMemberInfo>, AppError> {
    let members = sqlx::query_as::<_, ChannelMemberInfo>(
        r#"
        SELECT
            cm.id,
            cm.channel_id,
            cm.user_id,
            cm.guest_session_id,
            COALESCE(u.display_name, gs.guest_name, 'Anonymous') as display_name,
            ST_X(cm.position) as position_x,
            ST_Y(cm.position) as position_y,
            cm.facing_direction,
            cm.is_moving,
            cm.is_afk,
            COALESCE(aa.current_emotion, 0::smallint) as current_emotion,
            cm.joined_at
        FROM realm.channel_members cm
        LEFT JOIN auth.users u ON cm.user_id = u.id
        LEFT JOIN auth.guest_sessions gs ON cm.guest_session_id = gs.id
        LEFT JOIN props.active_avatars aa ON cm.user_id = aa.user_id AND aa.realm_id = $2
        WHERE cm.channel_id = $1
        ORDER BY cm.joined_at ASC
        "#,
    )
    .bind(channel_id)
    .bind(realm_id)
    .fetch_all(executor)
    .await?;

    Ok(members)
}

/// Get a specific channel member by user ID.
pub async fn get_channel_member<'e>(
    executor: impl PgExecutor<'e>,
    channel_id: Uuid,
    user_id: Uuid,
    realm_id: Uuid,
) -> Result<Option<ChannelMemberInfo>, AppError> {
    let member = sqlx::query_as::<_, ChannelMemberInfo>(
        r#"
        SELECT
            cm.id,
            cm.channel_id,
            cm.user_id,
            cm.guest_session_id,
            COALESCE(u.display_name, 'Anonymous') as display_name,
            ST_X(cm.position) as position_x,
            ST_Y(cm.position) as position_y,
            cm.facing_direction,
            cm.is_moving,
            cm.is_afk,
            COALESCE(aa.current_emotion, 0::smallint) as current_emotion,
            cm.joined_at
        FROM realm.channel_members cm
        LEFT JOIN auth.users u ON cm.user_id = u.id
        LEFT JOIN props.active_avatars aa ON cm.user_id = aa.user_id AND aa.realm_id = $3
        WHERE cm.channel_id = $1 AND cm.user_id = $2
        "#,
    )
    .bind(channel_id)
    .bind(user_id)
    .bind(realm_id)
    .fetch_optional(executor)
    .await?;

    Ok(member)
}

/// Set a user's moving state to false (called after movement animation completes).
pub async fn set_stopped<'e>(
    executor: impl PgExecutor<'e>,
    channel_id: Uuid,
    user_id: Uuid,
) -> Result<(), AppError> {
    sqlx::query(
        r#"
        UPDATE realm.channel_members
        SET is_moving = false
        WHERE channel_id = $1 AND user_id = $2
        "#,
    )
    .bind(channel_id)
    .bind(user_id)
    .execute(executor)
    .await?;

    Ok(())
}

/// Set a user's AFK state.
pub async fn set_afk<'e>(
    executor: impl PgExecutor<'e>,
    channel_id: Uuid,
    user_id: Uuid,
    is_afk: bool,
) -> Result<(), AppError> {
    sqlx::query(
        r#"
        UPDATE realm.channel_members
        SET is_afk = $3
        WHERE channel_id = $1 AND user_id = $2
        "#,
    )
    .bind(channel_id)
    .bind(user_id)
    .bind(is_afk)
    .execute(executor)
    .await?;

    Ok(())
}
