use db::sqlx;
use model::issues::IssueReactionModel;
use serde::Deserialize;
use session::Session;

use super::types::{IssueReactionResponse, issue_author};
use crate::{AppService, error::AppError, session_user};

#[derive(Debug, Clone, Deserialize, utoipa::ToSchema)]
pub struct AddReaction {
    pub reaction: String,
}

impl AppService {
    pub async fn issue_add_reaction(
        &self,
        ctx: &Session,
        wk_name: &str,
        number: i64,
        params: AddReaction,
    ) -> Result<Vec<IssueReactionResponse>, AppError> {
        let user_uid = session_user(ctx)?;
        let wk = self.workspace_resolve(wk_name).await?;
        self.workspace_require_member(wk.id, user_uid).await?;
        let issue = self.issue_resolve(wk.id, number).await?;

        let id = uuid::Uuid::now_v7();
        let now = chrono::Utc::now();

        sqlx::query(
            "INSERT INTO issue_reaction (id, issue, comment, \"user\", reaction, created_at) \
             VALUES ($1, $2, NULL, $3, $4, $5) \
             ON CONFLICT (issue, comment, \"user\", reaction) DO NOTHING",
        )
        .bind(id)
        .bind(issue.id)
        .bind(user_uid)
        .bind(&params.reaction)
        .bind(now)
        .execute(self.db.writer())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        self.issue_reactions_for(issue.id, None).await
    }

    pub async fn issue_remove_reaction(
        &self,
        ctx: &Session,
        wk_name: &str,
        number: i64,
        reaction: &str,
    ) -> Result<Vec<IssueReactionResponse>, AppError> {
        let user_uid = session_user(ctx)?;
        let wk = self.workspace_resolve(wk_name).await?;
        self.workspace_require_member(wk.id, user_uid).await?;
        let issue = self.issue_resolve(wk.id, number).await?;

        sqlx::query(
            "DELETE FROM issue_reaction WHERE issue = $1 AND comment IS NULL AND \"user\" = $2 AND reaction = $3",
        )
        .bind(issue.id)
        .bind(user_uid)
        .bind(reaction)
        .execute(self.db.writer())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        self.issue_reactions_for(issue.id, None).await
    }

    pub async fn issue_comment_add_reaction(
        &self,
        ctx: &Session,
        wk_name: &str,
        number: i64,
        comment_id: uuid::Uuid,
        params: AddReaction,
    ) -> Result<Vec<IssueReactionResponse>, AppError> {
        let user_uid = session_user(ctx)?;
        let wk = self.workspace_resolve(wk_name).await?;
        self.workspace_require_member(wk.id, user_uid).await?;
        let issue = self.issue_resolve(wk.id, number).await?;

        let exists = sqlx::query_scalar::<_, bool>(
            "SELECT EXISTS(SELECT 1 FROM issue_comment WHERE id = $1 AND issue = $2 AND deleted_at IS NULL)",
        )
        .bind(comment_id)
        .bind(issue.id)
        .fetch_one(self.db.reader())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        if !exists {
            return Err(AppError::CommentNotFound);
        }

        let id = uuid::Uuid::now_v7();
        let now = chrono::Utc::now();

        sqlx::query(
            "INSERT INTO issue_reaction (id, issue, comment, \"user\", reaction, created_at) \
             VALUES ($1, $2, $3, $4, $5, $6) \
             ON CONFLICT (issue, comment, \"user\", reaction) DO NOTHING",
        )
        .bind(id)
        .bind(issue.id)
        .bind(comment_id)
        .bind(user_uid)
        .bind(&params.reaction)
        .bind(now)
        .execute(self.db.writer())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        self.issue_reactions_for(issue.id, Some(comment_id)).await
    }

    pub async fn issue_comment_remove_reaction(
        &self,
        ctx: &Session,
        wk_name: &str,
        number: i64,
        comment_id: uuid::Uuid,
        reaction: &str,
    ) -> Result<Vec<IssueReactionResponse>, AppError> {
        let user_uid = session_user(ctx)?;
        let wk = self.workspace_resolve(wk_name).await?;
        self.workspace_require_member(wk.id, user_uid).await?;
        let issue = self.issue_resolve(wk.id, number).await?;

        sqlx::query(
            "DELETE FROM issue_reaction WHERE issue = $1 AND comment = $2 AND \"user\" = $3 AND reaction = $4",
        )
        .bind(issue.id)
        .bind(comment_id)
        .bind(user_uid)
        .bind(reaction)
        .execute(self.db.writer())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        self.issue_reactions_for(issue.id, Some(comment_id)).await
    }

    pub async fn issue_reactions_for(
        &self,
        issue_id: uuid::Uuid,
        comment_id: Option<uuid::Uuid>,
    ) -> Result<Vec<IssueReactionResponse>, AppError> {
        let reactions = if let Some(cid) = comment_id {
            sqlx::query_as::<_, IssueReactionModel>(
                "SELECT id, issue, comment, \"user\", reaction, created_at \
                 FROM issue_reaction WHERE issue = $1 AND comment = $2 \
                 ORDER BY created_at ASC",
            )
            .bind(issue_id)
            .bind(cid)
            .fetch_all(self.db.reader())
            .await
            .map_err(|e| AppError::DatabaseError(e.to_string()))?
        } else {
            sqlx::query_as::<_, IssueReactionModel>(
                "SELECT id, issue, comment, \"user\", reaction, created_at \
                 FROM issue_reaction WHERE issue = $1 AND comment IS NULL \
                 ORDER BY created_at ASC",
            )
            .bind(issue_id)
            .fetch_all(self.db.reader())
            .await
            .map_err(|e| AppError::DatabaseError(e.to_string()))?
        };

        let mut results = Vec::new();
        for reaction in reactions {
            let user = self.users_find_by_id(reaction.user).await?;
            results.push(IssueReactionResponse {
                id: reaction.id,
                user: issue_author(user),
                reaction: reaction.reaction,
                created_at: reaction.created_at,
            });
        }
        Ok(results)
    }
}
