use db::sqlx;
use model::workspace::WkGroupModel;
use serde::Deserialize;
use session::Session;

use super::types::{
    WorkspaceGroupMemberRow, WorkspaceGroupResponse, WorkspaceMemberResponse,
    group_response,
};
use crate::{AppService, error::AppError, session_user};

#[derive(Debug, Clone, Deserialize, utoipa::ToSchema)]
pub struct CreateWorkspaceGroup {
    pub name: String,
    pub avatar_url: Option<String>,
}

#[derive(Debug, Clone, Deserialize, utoipa::ToSchema)]
pub struct UpdateWorkspaceGroup {
    pub name: Option<String>,
    pub avatar_url: Option<Option<String>>,
}

impl AppService {
    pub async fn workspace_groups(
        &self,
        ctx: &Session,
        name: &str,
    ) -> Result<Vec<WorkspaceGroupResponse>, AppError> {
        let user_uid = session_user(ctx)?;
        let wk = self.workspace_resolve(name).await?;
        self.workspace_require_member(wk.id, user_uid).await?;

        let rows = sqlx::query_as::<_, WkGroupModel>(
            "SELECT id, name, wk, created_at, avatar_url, is_deleted FROM wk_group \
             WHERE wk = $1 ORDER BY created_at DESC",
        )
        .bind(wk.id)
        .fetch_all(self.db.reader())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        Ok(rows.into_iter().map(group_response).collect())
    }

    pub async fn workspace_create_group(
        &self,
        ctx: &Session,
        name: &str,
        params: CreateWorkspaceGroup,
    ) -> Result<WorkspaceGroupResponse, AppError> {
        let user_uid = session_user(ctx)?;
        let wk = self.workspace_resolve(name).await?;
        self.workspace_require_admin(wk.id, user_uid).await?;

        use super::types::normalize_name;
        let group_name = normalize_name(&params.name)?;

        let row = sqlx::query_as::<_, WkGroupModel>(
            "INSERT INTO wk_group (id, name, wk, created_at, avatar_url, is_deleted) \
             VALUES ($1, $2, $3, $4, $5, false) \
             RETURNING id, name, wk, created_at, avatar_url, is_deleted",
        )
        .bind(uuid::Uuid::now_v7())
        .bind(group_name)
        .bind(wk.id)
        .bind(chrono::Utc::now())
        .bind(params.avatar_url)
        .fetch_one(self.db.writer())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        Ok(group_response(row))
    }

    pub async fn workspace_update_group(
        &self,
        ctx: &Session,
        name: &str,
        group_name: &str,
        params: UpdateWorkspaceGroup,
    ) -> Result<WorkspaceGroupResponse, AppError> {
        let user_uid = session_user(ctx)?;
        let wk = self.workspace_resolve(name).await?;
        self.workspace_require_admin(wk.id, user_uid).await?;
        let group = self.workspace_group_by_name(wk.id, group_name).await?;

        use super::types::normalize_name;
        let group_name = match params.name {
            Some(name) => normalize_name(&name)?,
            None => group.name,
        };
        let avatar_url = params.avatar_url.unwrap_or(group.avatar_url);

        let row = sqlx::query_as::<_, WkGroupModel>(
            "UPDATE wk_group SET name = $1, avatar_url = $2 WHERE id = $3 AND wk = $4 \
             RETURNING id, name, wk, created_at, avatar_url, is_deleted",
        )
        .bind(group_name)
        .bind(avatar_url)
        .bind(group.id)
        .bind(wk.id)
        .fetch_one(self.db.writer())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        Ok(group_response(row))
    }

    pub async fn workspace_delete_group(
        &self,
        ctx: &Session,
        name: &str,
        group_name: &str,
    ) -> Result<(), AppError> {
        let user_uid = session_user(ctx)?;
        let wk = self.workspace_resolve(name).await?;
        self.workspace_require_admin(wk.id, user_uid).await?;
        let group = self.workspace_group_by_name(wk.id, group_name).await?;
        sqlx::query(
            "UPDATE wk_group SET is_deleted = true WHERE id = $1 AND wk = $2",
        )
        .bind(group.id)
        .bind(wk.id)
        .execute(self.db.writer())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;
        Ok(())
    }

    pub async fn workspace_add_group_member(
        &self,
        ctx: &Session,
        name: &str,
        group_name: &str,
        username: &str,
    ) -> Result<(), AppError> {
        let user_uid = session_user(ctx)?;
        let wk = self.workspace_resolve(name).await?;
        self.workspace_require_admin(wk.id, user_uid).await?;
        let group = self.workspace_group_by_name(wk.id, group_name).await?;
        let target = self.users_find_active_user_by_username(username).await?;
        self.workspace_require_member(wk.id, target.id).await?;

        sqlx::query(
            "INSERT INTO wk_gp_member (\"user\", gp, join_at, leave_at) VALUES ($1, $2, $3, NULL) \
             ON CONFLICT (\"user\", gp) DO UPDATE SET leave_at = NULL",
        )
        .bind(target.id)
        .bind(group.id)
        .bind(chrono::Utc::now())
        .execute(self.db.writer())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;
        Ok(())
    }

    pub async fn workspace_remove_group_member(
        &self,
        ctx: &Session,
        name: &str,
        group_name: &str,
        username: &str,
    ) -> Result<(), AppError> {
        let user_uid = session_user(ctx)?;
        let wk = self.workspace_resolve(name).await?;
        self.workspace_require_admin(wk.id, user_uid).await?;
        let group = self.workspace_group_by_name(wk.id, group_name).await?;
        let target = self.users_find_active_user_by_username(username).await?;

        sqlx::query("UPDATE wk_gp_member SET leave_at = $1 WHERE \"user\" = $2 AND gp = $3")
            .bind(chrono::Utc::now())
            .bind(target.id)
            .bind(group.id)
            .execute(self.db.writer())
            .await
            .map_err(|e| AppError::DatabaseError(e.to_string()))?;
        Ok(())
    }

    pub async fn workspace_group_members(
        &self,
        ctx: &Session,
        name: &str,
        group_name: &str,
    ) -> Result<Vec<WorkspaceMemberResponse>, AppError> {
        let user_uid = session_user(ctx)?;
        let wk = self.workspace_resolve(name).await?;
        self.workspace_require_member(wk.id, user_uid).await?;
        let group = self.workspace_group_by_name(wk.id, group_name).await?;

        let rows = sqlx::query_as::<_, WorkspaceGroupMemberRow>(
            "SELECT u.id, u.username, u.display_name, u.avatar_url, u.website_url, u.allow_use, u.can_search, \
                    u.last_sign_in_at, u.created_at, u.updated_at, gm.join_at \
             FROM wk_gp_member gm \
             INNER JOIN \"user\" u ON u.id = gm.\"user\" \
             WHERE gm.gp = $1 AND gm.leave_at IS NULL \
             ORDER BY gm.join_at ASC",
        )
        .bind(group.id)
        .fetch_all(self.db.reader())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        Ok(rows
            .into_iter()
            .map(WorkspaceMemberResponse::from)
            .collect())
    }

    pub async fn workspace_group_by_name(
        &self,
        wk_id: uuid::Uuid,
        group_name: &str,
    ) -> Result<WkGroupModel, AppError> {
        sqlx::query_as::<_, WkGroupModel>(
            "SELECT id, name, wk, created_at, avatar_url, is_deleted \
             FROM wk_group WHERE wk = $1 AND name = $2 AND is_deleted = false",
        )
        .bind(wk_id)
        .bind(group_name)
        .fetch_optional(self.db.reader())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?
        .ok_or(AppError::NotFound("workspace group not found".to_string()))
    }
}
