//! 任务域 Repo:TaskRepo(含 advance_status_atomic 状态机收口)

use std::sync::Arc;

use rusqlite::{params, Connection, OptionalExtension, Row};
use tokio::sync::Mutex;

use df_types::error::Result;

use crate::db::Database;
use crate::models::TaskRecord;

use super::impl_repo;
use super::{now_millis_str, storage_err, validate_column_name};

// ============================================================
// from_row 辅助函数
// ============================================================

fn task_from_row(row: &Row<'_>) -> std::result::Result<TaskRecord, rusqlite::Error> {
    Ok(TaskRecord {
        id: row.get("id")?,
        project_id: row.get("project_id")?,
        title: row.get("title")?,
        description: row.get("description")?,
        status: row.get("status")?,
        priority: row.get("priority")?,
        branch_name: row.get("branch_name")?,
        assignee: row.get("assignee")?,
        workflow_def_id: row.get("workflow_def_id")?,
        base_branch: row.get("base_branch")?,
        review_rounds: row.get("review_rounds")?,
        output_json: row.get("output_json")?,
        created_at: row.get("created_at")?,
        updated_at: row.get("updated_at")?,
    })
}

// ============================================================
// Repo 实现
// ============================================================

impl_repo!(
    /// 任务表 CRUD
    TaskRepo,
    TaskRecord,
    "tasks",
    from_row => |row| task_from_row(row),
    insert  => |conn, rec| {
        conn.execute(
            "INSERT INTO tasks (id, project_id, title, description, status, priority, branch_name, assignee, workflow_def_id, base_branch, review_rounds, output_json, created_at, updated_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14)",
            params![
                rec.id, rec.project_id, rec.title, rec.description, rec.status, rec.priority,
                rec.branch_name, rec.assignee, rec.workflow_def_id, rec.base_branch,
                rec.review_rounds, rec.output_json, rec.created_at, rec.updated_at
            ],
        )
    },
    update  => |conn, rec| {
        conn.execute(
            "UPDATE tasks SET project_id = ?1, title = ?2, description = ?3, status = ?4, priority = ?5, branch_name = ?6, assignee = ?7, workflow_def_id = ?8, base_branch = ?9, review_rounds = ?10, output_json = ?11, updated_at = ?12 WHERE id = ?13",
            params![
                rec.project_id, rec.title, rec.description, rec.status, rec.priority,
                rec.branch_name, rec.assignee, rec.workflow_def_id, rec.base_branch,
                rec.review_rounds, rec.output_json, rec.updated_at, rec.id
            ],
        )
    }
);

impl TaskRepo {
    /// 列出未删除任务(deleted_at IS NULL)— 对标 ProjectRepo::list_active
    ///
    /// 显式列出 14 个 TaskRecord 列名(同 ProjectRepo::list_active 写法),
    /// 不 SELECT deleted_at:TaskRecord 不带该字段,取了 from_row 会因未知列报错。
    pub async fn list_active(&self) -> Result<Vec<TaskRecord>> {
        let conn = self.conn.clone();
        tokio::task::spawn_blocking(move || {
            let guard = conn.blocking_lock();
            let mut stmt = guard
                .prepare("SELECT id, project_id, title, description, status, priority, branch_name, assignee, workflow_def_id, base_branch, review_rounds, output_json, created_at, updated_at FROM tasks WHERE deleted_at IS NULL ORDER BY created_at DESC")
                .map_err(storage_err)?;
            let rows = stmt
                .query_map([], |row| task_from_row(row))
                .map_err(storage_err)?;
            let mut results = Vec::new();
            for r in rows {
                results.push(r.map_err(storage_err)?);
            }
            Ok(results)
        })
        .await
        .map_err(storage_err)?
    }

    /// 软删:标记 deleted_at(进回收站,可恢复)。仅作用于未删任务,返回是否命中。
    /// 对标 ProjectRepo::soft_delete。
    pub async fn soft_delete(&self, id: &str) -> Result<bool> {
        let conn = self.conn.clone();
        let id = id.to_owned();
        let now = now_millis_str();
        tokio::task::spawn_blocking(move || {
            let guard = conn.blocking_lock();
            let affected = guard
                .execute(
                    "UPDATE tasks SET deleted_at = ?1, updated_at = ?1 WHERE id = ?2 AND deleted_at IS NULL",
                    params![now, id],
                )
                .map_err(storage_err)?;
            Ok(affected > 0)
        })
        .await
        .map_err(storage_err)?
    }

    /// 恢复:清 deleted_at(从回收站还原)。仅作用于已删任务,返回是否命中。
    /// 对标 ProjectRepo::restore。
    pub async fn restore(&self, id: &str) -> Result<bool> {
        let conn = self.conn.clone();
        let id = id.to_owned();
        let now = now_millis_str();
        tokio::task::spawn_blocking(move || {
            let guard = conn.blocking_lock();
            let affected = guard
                .execute(
                    "UPDATE tasks SET deleted_at = NULL, updated_at = ?1 WHERE id = ?2 AND deleted_at IS NOT NULL",
                    params![now, id],
                )
                .map_err(storage_err)?;
            Ok(affected > 0)
        })
        .await
        .map_err(storage_err)?
    }

    /// 原子推进任务状态(任务推进链 F-260616-02 唯一 status 写入路径)
    ///
    /// 下沉 SQL `WHERE id=? AND status=?expected` 做 CAS(Compare-And-Swap)防 TOCTOU:
    /// 并发推进/旁路修改若已改 status,affected_rows==0,本方法返回 None,调用方
    /// (task_advance_node)据此报「状态已变,推进中止」。`review_rounds` 不进通用
    /// update_field 白名单(收口:仅本方法可改 status 与 review_rounds)。
    ///
    /// - `expected`:调用方读取的当前 status(状态机校验时的 from),CAS 前置。
    /// - `new_status`:目标 status(状态机 can_transition 已校验合法)。
    /// - `bump_rounds`:退回转换(in_review→in_progress / testing→in_review)传 true,
    ///   一并 `review_rounds = review_rounds + 1`(同 UPDATE 原子,避免读改写竞争)。
    ///   前向推进 / 进出 blocked / 进 cancelled 传 false,不动 review_rounds。
    ///
    /// 返回:成功推进返回更新后的 TaskRecord;affected==0(状态已变/任务不存在)返回 None。
    pub async fn advance_status_atomic(
        &self,
        id: &str,
        expected: &str,
        new_status: &str,
        bump_rounds: bool,
    ) -> Result<Option<TaskRecord>> {
        let conn = self.conn.clone();
        let id = id.to_owned();
        let expected = expected.to_owned();
        let new_status = new_status.to_owned();
        let now = now_millis_str();
        tokio::task::spawn_blocking(move || {
            let guard = conn.blocking_lock();
            // CAS:WHERE id AND status=expected 锁定当前态;affected==0 即并发已改动。
            // deleted_at IS NULL:回收站任务(soft_delete 设了 deleted_at)CAS 必败→affected=0,
            // 返回 None,杜绝回收站任务被推进(D-02 软删语义收口,一处关闭)。
            let sql = if bump_rounds {
                "UPDATE tasks SET status = ?1, review_rounds = review_rounds + 1, updated_at = ?2 \
                 WHERE id = ?3 AND status = ?4 AND deleted_at IS NULL"
            } else {
                "UPDATE tasks SET status = ?1, updated_at = ?2 \
                 WHERE id = ?3 AND status = ?4 AND deleted_at IS NULL"
            };
            let affected = guard
                .execute(sql, params![new_status, now, id, expected])
                .map_err(storage_err)?;
            if affected == 0 {
                return Ok(None);
            }
            // 回读更新后的记录(含新 status / 累加后的 review_rounds / 新 updated_at)。
            let mut stmt = guard
                .prepare("SELECT id, project_id, title, description, status, priority, branch_name, assignee, workflow_def_id, base_branch, review_rounds, output_json, created_at, updated_at FROM tasks WHERE id = ?1")
                .map_err(storage_err)?;
            let row = stmt
                .query_row(params![id], |row| task_from_row(row))
                .optional()
                .map_err(storage_err)?;
            Ok(row)
        })
        .await
        .map_err(storage_err)?
    }

    /// 列出回收站(deleted_at IS NOT NULL),按更新时间(≈删除时间)降序。对标 ProjectRepo::list_deleted。
    ///
    /// 注:任务表无专用 list_active_by_project 方法,按项目列活跃任务由 commands/task.rs
    /// 的 list_tasks 用 list_active 后内存过滤 project_id 实现(任务量小,无需 SQL 下推)。
    pub async fn list_deleted(&self) -> Result<Vec<TaskRecord>> {
        let conn = self.conn.clone();
        tokio::task::spawn_blocking(move || {
            let guard = conn.blocking_lock();
            let mut stmt = guard
                .prepare("SELECT id, project_id, title, description, status, priority, branch_name, assignee, workflow_def_id, base_branch, review_rounds, output_json, created_at, updated_at FROM tasks WHERE deleted_at IS NOT NULL ORDER BY updated_at DESC")
                .map_err(storage_err)?;
            let rows = stmt
                .query_map([], |row| task_from_row(row))
                .map_err(storage_err)?;
            let mut results = Vec::new();
            for r in rows {
                results.push(r.map_err(storage_err)?);
            }
            Ok(results)
        })
        .await
        .map_err(storage_err)?
    }
}
