//! 项目域 Repo:ProjectRepo / BranchRepo / ReleaseRepo / WorkflowRepo / NodeExecutionRepo

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::{
    BranchRecord, NodeExecutionRecord, ProjectRecord, ReleaseRecord, WorkflowRecord,
};

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

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

fn project_from_row(row: &Row<'_>) -> std::result::Result<ProjectRecord, rusqlite::Error> {
    Ok(ProjectRecord {
        id: row.get("id")?,
        name: row.get("name")?,
        description: row.get("description")?,
        status: row.get("status")?,
        idea_id: row.get("idea_id")?,
        path: row.get("path")?,
        stack: row.get("stack")?,
        created_at: row.get("created_at")?,
        updated_at: row.get("updated_at")?,
    })
}

fn branch_from_row(row: &Row<'_>) -> std::result::Result<BranchRecord, rusqlite::Error> {
    Ok(BranchRecord {
        id: row.get("id")?,
        project_id: row.get("project_id")?,
        task_id: row.get("task_id")?,
        name: row.get("name")?,
        base: row.get("base")?,
        status: row.get("status")?,
        created_at: row.get("created_at")?,
        updated_at: row.get("updated_at")?,
        merged_at: row.get("merged_at")?,
    })
}

fn release_from_row(row: &Row<'_>) -> std::result::Result<ReleaseRecord, rusqlite::Error> {
    Ok(ReleaseRecord {
        id: row.get("id")?,
        project_id: row.get("project_id")?,
        version: row.get("version")?,
        status: row.get("status")?,
        task_ids: row.get("task_ids")?,
        changelog: row.get("changelog")?,
        created_at: row.get("created_at")?,
        released_at: row.get("released_at")?,
    })
}

fn workflow_from_row(row: &Row<'_>) -> std::result::Result<WorkflowRecord, rusqlite::Error> {
    Ok(WorkflowRecord {
        id: row.get("id")?,
        name: row.get("name")?,
        dag_json: row.get("dag_json")?,
        status: row.get("status")?,
        triggered_by: row.get("triggered_by")?,
        project_id: row.get("project_id")?,
        task_id: row.get("task_id")?,
        created_at: row.get("created_at")?,
        completed_at: row.get("completed_at")?,
    })
}

fn node_execution_from_row(
    row: &Row<'_>,
) -> std::result::Result<NodeExecutionRecord, rusqlite::Error> {
    Ok(NodeExecutionRecord {
        id: row.get("id")?,
        workflow_id: row.get("workflow_id")?,
        node_id: row.get("node_id")?,
        node_type: row.get("node_type")?,
        status: row.get("status")?,
        input_json: row.get("input_json")?,
        output_json: row.get("output_json")?,
        error_message: row.get("error_message")?,
        started_at: row.get("started_at")?,
        completed_at: row.get("completed_at")?,
    })
}

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

impl_repo!(
    /// 项目表 CRUD
    ProjectRepo,
    ProjectRecord,
    "projects",
    from_row => |row| project_from_row(row),
    insert  => |conn, rec| {
        conn.execute(
            "INSERT INTO projects (id, name, description, status, idea_id, path, stack, created_at, updated_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
            params![
                rec.id, rec.name, rec.description, rec.status, rec.idea_id,
                rec.path, rec.stack, rec.created_at, rec.updated_at
            ],
        )
    },
    update  => |conn, rec| {
        conn.execute(
            "UPDATE projects SET name = ?1, description = ?2, status = ?3, idea_id = ?4, path = ?5, stack = ?6, updated_at = ?7 WHERE id = ?8",
            params![
                rec.name, rec.description, rec.status, rec.idea_id,
                rec.path, rec.stack, rec.updated_at, rec.id
            ],
        )
    }
);

impl ProjectRepo {
    /// 列出未删除项目(deleted_at IS NULL)
    pub async fn list_active(&self) -> Result<Vec<ProjectRecord>> {
        let conn = self.conn.clone();
        tokio::task::spawn_blocking(move || {
            let guard = conn.blocking_lock();
            let mut stmt = guard
                .prepare("SELECT id, name, description, status, idea_id, path, stack, created_at, updated_at FROM projects WHERE deleted_at IS NULL ORDER BY created_at DESC")
                .map_err(storage_err)?;
            let rows = stmt
                .query_map([], |row| project_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(进回收站,可恢复)。仅作用于未删项目,返回是否命中。
    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 projects 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(从回收站还原)
    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 projects 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)?
    }

    /// 查找已绑定该规范化路径的项目(排除 exclude_id 自身)。无冲突返回 None。
    ///
    /// DRY(R-PD-11):统一 project.rs::find_binding_conflict 与 tool_registry.rs::bind_dir_to_project
    /// 的「列项目→排除自身→按规范化路径比较」逻辑。本 crate 不依赖 df-project,故 norm_path 须由
    /// 调用方先经 `df_project::scan::normalize_path`(内含 canonicalize,防 `C:\a\b` vs `C:/a/b/` 绕过)。
    pub async fn find_path_conflict(
        &self,
        norm_path: &str,
        exclude_id: Option<&str>,
    ) -> Result<Option<ProjectRecord>> {
        let projects = self.list_active().await?;
        Ok(projects.into_iter().find(|p| {
            let excluded = exclude_id.is_some_and(|eid| p.id == eid);
            !excluded && p.path.as_ref().is_some_and(|pp| {
                // 路径规范化由调用方完成目标侧;这里为已存项目路径补规范化
                // (历史路径写法可能不规范,统一规范化比较避免误判/漏判)
                normalize_stored_path(pp) == norm_path
            })
        }))
    }

    /// 列出回收站(deleted_at IS NOT NULL),按更新时间(≈删除时间)降序
    pub async fn list_deleted(&self) -> Result<Vec<ProjectRecord>> {
        let conn = self.conn.clone();
        tokio::task::spawn_blocking(move || {
            let guard = conn.blocking_lock();
            let mut stmt = guard
                .prepare("SELECT id, name, description, status, idea_id, path, stack, created_at, updated_at FROM projects WHERE deleted_at IS NOT NULL ORDER BY updated_at DESC")
                .map_err(storage_err)?;
            let rows = stmt
                .query_map([], |row| project_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)?
    }

    /// 彻底删除:事务级联删 branches→releases→tasks→projects(不可恢复)
    ///
    /// SQLite 已开 PRAGMA foreign_keys=ON 但表无 ON DELETE CASCADE,ALTER 改不了 FK 约束,
    /// 故应用层级联:子表先于父表删,单事务保证一致性。
    pub async fn purge_with_descendants(&self, id: &str) -> Result<bool> {
        let conn = self.conn.clone();
        let id = id.to_owned();
        tokio::task::spawn_blocking(move || {
            let mut guard = conn.blocking_lock();
            let tx = guard.transaction().map_err(storage_err)?;
            tx.execute("DELETE FROM branches WHERE project_id = ?1", params![id])
                .map_err(storage_err)?;
            tx.execute("DELETE FROM releases WHERE project_id = ?1", params![id])
                .map_err(storage_err)?;
            tx.execute("DELETE FROM tasks WHERE project_id = ?1", params![id])
                .map_err(storage_err)?;
            let affected = tx
                .execute("DELETE FROM projects WHERE id = ?1", params![id])
                .map_err(storage_err)?;
            tx.commit().map_err(storage_err)?;
            Ok(affected > 0)
        })
        .await
        .map_err(storage_err)?
    }
}

impl_repo!(
    /// 分支表 CRUD
    BranchRepo,
    BranchRecord,
    "branches",
    from_row => |row| branch_from_row(row),
    insert  => |conn, rec| {
        conn.execute(
            "INSERT INTO branches (id, project_id, task_id, name, base, status, created_at, updated_at, merged_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
            params![
                rec.id, rec.project_id, rec.task_id, rec.name, rec.base,
                rec.status, rec.created_at, rec.updated_at, rec.merged_at
            ],
        )
    },
    update  => |conn, rec| {
        conn.execute(
            "UPDATE branches SET project_id = ?1, task_id = ?2, name = ?3, base = ?4, status = ?5, updated_at = ?6, merged_at = ?7 WHERE id = ?8",
            params![
                rec.project_id, rec.task_id, rec.name, rec.base,
                rec.status, rec.updated_at, rec.merged_at, rec.id
            ],
        )
    }
);

impl_repo!(
    /// 发布表 CRUD
    ReleaseRepo,
    ReleaseRecord,
    "releases",
    from_row => |row| release_from_row(row),
    insert  => |conn, rec| {
        conn.execute(
            "INSERT INTO releases (id, project_id, version, status, task_ids, changelog, created_at, released_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
            params![
                rec.id, rec.project_id, rec.version, rec.status, rec.task_ids,
                rec.changelog, rec.created_at, rec.released_at
            ],
        )
    },
    update  => |conn, rec| {
        conn.execute(
            "UPDATE releases SET project_id = ?1, version = ?2, status = ?3, task_ids = ?4, changelog = ?5, released_at = ?6 WHERE id = ?7",
            params![
                rec.project_id, rec.version, rec.status, rec.task_ids,
                rec.changelog, rec.released_at, rec.id
            ],
        )
    }
);

impl_repo!(
    /// 工作流执行表 CRUD
    WorkflowRepo,
    WorkflowRecord,
    "workflow_executions",
    from_row => |row| workflow_from_row(row),
    insert  => |conn, rec| {
        conn.execute(
            "INSERT INTO workflow_executions (id, name, dag_json, status, triggered_by, project_id, task_id, created_at, completed_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
            params![
                rec.id, rec.name, rec.dag_json, rec.status, rec.triggered_by,
                rec.project_id, rec.task_id, rec.created_at, rec.completed_at
            ],
        )
    },
    update  => |conn, rec| {
        conn.execute(
            "UPDATE workflow_executions SET name = ?1, dag_json = ?2, status = ?3, triggered_by = ?4, project_id = ?5, task_id = ?6, completed_at = ?7 WHERE id = ?8",
            params![
                rec.name, rec.dag_json, rec.status, rec.triggered_by,
                rec.project_id, rec.task_id, rec.completed_at, rec.id
            ],
        )
    }
);

impl_repo!(
    /// 节点执行表 CRUD
    NodeExecutionRepo,
    NodeExecutionRecord,
    "node_executions",
    from_row => |row| node_execution_from_row(row),
    insert  => |conn, rec| {
        conn.execute(
            "INSERT INTO node_executions (id, workflow_id, node_id, node_type, status, input_json, output_json, error_message, started_at, completed_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
            params![
                rec.id, rec.workflow_id, rec.node_id, rec.node_type, rec.status,
                rec.input_json, rec.output_json, rec.error_message, rec.started_at, rec.completed_at
            ],
        )
    },
    update  => |conn, rec| {
        conn.execute(
            "UPDATE node_executions SET workflow_id = ?1, node_id = ?2, node_type = ?3, status = ?4, input_json = ?5, output_json = ?6, error_message = ?7, started_at = ?8, completed_at = ?9 WHERE id = ?10",
            params![
                rec.workflow_id, rec.node_id, rec.node_type, rec.status,
                rec.input_json, rec.output_json, rec.error_message, rec.started_at, rec.completed_at, rec.id
            ],
        )
    }
);
