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

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

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

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

impl AppService {
    pub async fn issue_comment_create(
        &self,
        ctx: &Session,
        wk_name: &str,
        number: i64,
        params: CreateComment,
    ) -> Result<IssueCommentResponse, 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 body = params.body.trim();
        if body.is_empty() {
            return Err(AppError::BadRequest(
                "comment body is required".to_string(),
            ));
        }

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

        let comment = sqlx::query_as::<_, IssueCommentModel>(
            "INSERT INTO issue_comment (id, issue, author, body, created_at, updated_at) \
             VALUES ($1, $2, $3, $4, $5, $5) \
             RETURNING id, issue, author, body, created_at, updated_at, deleted_at",
        )
        .bind(id)
        .bind(issue.id)
        .bind(user_uid)
        .bind(body)
        .bind(now)
        .fetch_one(self.db.writer())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        sqlx::query(
            "INSERT INTO issue_event (id, issue, actor, event, from_value, to_value, created_at) \
             VALUES ($1, $2, $3, 'commented', NULL, $4, $5)",
        )
        .bind(uuid::Uuid::now_v7())
        .bind(issue.id)
        .bind(user_uid)
        .bind(comment.id)
        .bind(now)
        .execute(self.db.writer())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        let author = self.users_find_by_id(user_uid).await?;
        Ok(IssueCommentResponse {
            id: comment.id,
            author: issue_author(author),
            body: comment.body,
            created_at: comment.created_at,
            updated_at: comment.updated_at,
        })
    }

    pub async fn issue_comment_list(
        &self,
        ctx: &Session,
        wk_name: &str,
        number: i64,
    ) -> Result<Vec<IssueCommentResponse>, 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 comments = sqlx::query_as::<_, IssueCommentModel>(
            "SELECT id, issue, author, body, created_at, updated_at, deleted_at \
             FROM issue_comment WHERE issue = $1 AND deleted_at 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 comment in comments {
            let author = self.users_find_by_id(comment.author).await?;
            results.push(IssueCommentResponse {
                id: comment.id,
                author: issue_author(author),
                body: comment.body,
                created_at: comment.created_at,
                updated_at: comment.updated_at,
            });
        }
        Ok(results)
    }

    pub async fn issue_comment_update(
        &self,
        ctx: &Session,
        wk_name: &str,
        number: i64,
        comment_id: uuid::Uuid,
        params: UpdateComment,
    ) -> Result<IssueCommentResponse, 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 comment = sqlx::query_as::<_, IssueCommentModel>(
            "SELECT id, issue, author, body, created_at, updated_at, deleted_at \
             FROM issue_comment WHERE id = $1 AND issue = $2 AND deleted_at IS NULL",
        )
        .bind(comment_id)
        .bind(issue.id)
        .fetch_optional(self.db.reader())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?
        .ok_or(AppError::CommentNotFound)?;

        if comment.author != user_uid {
            return Err(AppError::Forbidden(
                "only the comment author can edit".to_string(),
            ));
        }

        let body = params.body.trim();
        if body.is_empty() {
            return Err(AppError::BadRequest(
                "comment body is required".to_string(),
            ));
        }

        let now = chrono::Utc::now();
        let updated = sqlx::query_as::<_, IssueCommentModel>(
            "UPDATE issue_comment SET body = $1, updated_at = $2 WHERE id = $3 \
             RETURNING id, issue, author, body, created_at, updated_at, deleted_at",
        )
        .bind(body)
        .bind(now)
        .bind(comment_id)
        .fetch_one(self.db.writer())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        let author = self.users_find_by_id(user_uid).await?;
        Ok(IssueCommentResponse {
            id: updated.id,
            author: issue_author(author),
            body: updated.body,
            created_at: updated.created_at,
            updated_at: updated.updated_at,
        })
    }

    pub async fn issue_comment_delete(
        &self,
        ctx: &Session,
        wk_name: &str,
        number: i64,
        comment_id: uuid::Uuid,
    ) -> Result<(), 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 comment = sqlx::query_as::<_, IssueCommentModel>(
            "SELECT id, issue, author, body, created_at, updated_at, deleted_at \
             FROM issue_comment WHERE id = $1 AND issue = $2 AND deleted_at IS NULL",
        )
        .bind(comment_id)
        .bind(issue.id)
        .fetch_optional(self.db.reader())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?
        .ok_or(AppError::CommentNotFound)?;

        if comment.author != user_uid {
            self.workspace_require_admin(wk.id, user_uid).await?;
        }

        sqlx::query("UPDATE issue_comment SET deleted_at = $1 WHERE id = $2")
            .bind(chrono::Utc::now())
            .bind(comment_id)
            .execute(self.db.writer())
            .await
            .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        Ok(())
    }
}
