use db::sqlx;
use model::pull_request::PullRequestCommentModel;
use serde::Deserialize;
use session::Session;

use crate::{
    AppService, error::AppError,
    pull_request::types::PullRequestCommentResponse, session_user,
};

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

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

impl AppService {
    pub async fn pr_comment_create(
        &self,
        ctx: &Session,
        wk_name: &str,
        repo_name: &str,
        number: i64,
        params: CreatePrComment,
    ) -> Result<PullRequestCommentResponse, AppError> {
        let user_uid = session_user(ctx)?;
        let (repo_id, _) =
            self.pr_resolve_repo(ctx, wk_name, repo_name).await?;
        let pr = self.pr_resolve(repo_id, number).await?;

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

        let now = chrono::Utc::now();
        let comment = sqlx::query_as::<_, PullRequestCommentModel>(
            "INSERT INTO pull_request_comment (id, pull_request, author, body, created_at, updated_at) \
             VALUES ($1, $2, $3, $4, $5, $5) \
             RETURNING id, pull_request, author, body, created_at, updated_at, deleted_at",
        )
        .bind(uuid::Uuid::now_v7())
        .bind(pr.id)
        .bind(user_uid)
        .bind(&params.body)
        .bind(now)
        .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(PullRequestCommentResponse {
            id: comment.id,
            author: crate::issues::types::issue_author(author),
            body: comment.body,
            created_at: comment.created_at,
            updated_at: comment.updated_at,
        })
    }

    pub async fn pr_comment_list(
        &self,
        ctx: &Session,
        wk_name: &str,
        repo_name: &str,
        number: i64,
    ) -> Result<Vec<PullRequestCommentResponse>, AppError> {
        let (repo_id, _) =
            self.pr_resolve_repo(ctx, wk_name, repo_name).await?;
        let pr = self.pr_resolve(repo_id, number).await?;

        let comments = sqlx::query_as::<_, PullRequestCommentModel>(
            "SELECT id, pull_request, author, body, created_at, updated_at, deleted_at \
             FROM pull_request_comment WHERE pull_request = $1 AND deleted_at IS NULL \
             ORDER BY created_at ASC",
        )
        .bind(pr.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(PullRequestCommentResponse {
                id: comment.id,
                author: crate::issues::types::issue_author(author),
                body: comment.body,
                created_at: comment.created_at,
                updated_at: comment.updated_at,
            });
        }
        Ok(results)
    }

    pub async fn pr_comment_update(
        &self,
        ctx: &Session,
        wk_name: &str,
        repo_name: &str,
        _number: i64,
        comment_id: uuid::Uuid,
        params: UpdatePrComment,
    ) -> Result<PullRequestCommentResponse, AppError> {
        let user_uid = session_user(ctx)?;
        let _repo_id = self.pr_resolve_repo(ctx, wk_name, repo_name).await?;

        let comment = sqlx::query_as::<_, PullRequestCommentModel>(
            "SELECT id, pull_request, author, body, created_at, updated_at, deleted_at \
             FROM pull_request_comment WHERE id = $1 AND deleted_at IS NULL",
        )
        .bind(comment_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 author can update this comment".to_string(),
            ));
        }

        let now = chrono::Utc::now();
        let comment = sqlx::query_as::<_, PullRequestCommentModel>(
            "UPDATE pull_request_comment SET body = $1, updated_at = $2 WHERE id = $3 \
             RETURNING id, pull_request, author, body, created_at, updated_at, deleted_at",
        )
        .bind(&params.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(comment.author).await?;
        Ok(PullRequestCommentResponse {
            id: comment.id,
            author: crate::issues::types::issue_author(author),
            body: comment.body,
            created_at: comment.created_at,
            updated_at: comment.updated_at,
        })
    }

    pub async fn pr_comment_delete(
        &self,
        ctx: &Session,
        wk_name: &str,
        repo_name: &str,
        comment_id: uuid::Uuid,
    ) -> Result<(), AppError> {
        let user_uid = session_user(ctx)?;
        let _repo_id = self.pr_resolve_repo(ctx, wk_name, repo_name).await?;

        let comment = sqlx::query_as::<_, PullRequestCommentModel>(
            "SELECT id, pull_request, author, body, created_at, updated_at, deleted_at \
             FROM pull_request_comment WHERE id = $1 AND deleted_at IS NULL",
        )
        .bind(comment_id)
        .fetch_optional(self.db.reader())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?
        .ok_or(AppError::CommentNotFound)?;

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

        sqlx::query(
            "UPDATE pull_request_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(())
    }
}
