use std::fs;
use std::path::Path;

use crate::db::DbConnection as Connection;
use walkdir::WalkDir;

use crate::db::queries::script as qs;
use crate::engine::{EngineError, EngineResult};
use crate::model::{Script, ScriptStatus};
use crate::util::frontmatter::read_script_file;

/// Report returned by `rebuild_scripts_from_filesystem`.
#[derive(Debug, Default)]
pub struct ScriptRebuildReport {
    pub created: usize,
    pub updated: usize,
    pub errors: Vec<String>,
}

pub type ItemProgressFn = Box<dyn Fn(usize, usize, &str) -> bool + Send>;

/// Rebuild scripts from the filesystem into the database.
///
/// Walks the `scripts/` directory for `*.lua` files, parses each
/// via frontmatter, and either creates or updates the corresponding DB row.
/// Published scripts (those present on disk) have `content = None` in the DB.
pub fn rebuild_scripts_from_filesystem(
    conn: &Connection,
    data_dir: &Path,
    project_id: &str,
) -> EngineResult<ScriptRebuildReport> {
    rebuild_scripts_from_filesystem_with_progress(conn, data_dir, project_id, None)
}

pub fn rebuild_scripts_from_filesystem_with_progress(
    conn: &Connection,
    data_dir: &Path,
    project_id: &str,
    on_item: Option<ItemProgressFn>,
) -> EngineResult<ScriptRebuildReport> {
    let mut report = ScriptRebuildReport::default();
    let scripts_dir = data_dir.join("scripts");

    if !scripts_dir.exists() {
        return Ok(report);
    }

    let files = WalkDir::new(&scripts_dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|entry| entry.path().is_file())
        .filter(|entry| entry.path().extension().and_then(|ext| ext.to_str()) == Some("lua"))
        .collect::<Vec<_>>();

    for (index, entry) in files.iter().enumerate() {
        let path = entry.path();
        let name = path
            .file_stem()
            .and_then(|stem| stem.to_str())
            .unwrap_or("?");
        if let Some(ref callback) = on_item
            && !callback(index + 1, files.len(), name)
        {
            return Err(EngineError::Cancelled);
        }

        match rebuild_single_script(conn, data_dir, project_id, path) {
            Ok(created) => {
                if created {
                    report.created += 1;
                } else {
                    report.updated += 1;
                }
            }
            Err(e) => {
                report.errors.push(format!("{}: {e}", path.display()));
            }
        }
    }

    Ok(report)
}

/// Rebuild a single script from a `.lua` file.
/// Returns `true` if created, `false` if updated.
pub(crate) fn rebuild_single_script(
    conn: &Connection,
    data_dir: &Path,
    project_id: &str,
    path: &Path,
) -> EngineResult<bool> {
    let content = fs::read_to_string(path)?;
    let (fm, _body) = read_script_file(&content).map_err(EngineError::Parse)?;

    let rel_path = path
        .strip_prefix(data_dir)
        .unwrap_or(path)
        .to_string_lossy()
        .to_string();

    let kind = fm.kind.parse().map_err(EngineError::Parse)?;
    // File exists on disk -> Published; content is None in DB
    let status = ScriptStatus::Published;

    let existing = qs::get_script_by_id(conn, &fm.id);
    match existing {
        Ok(mut script) => {
            script.slug = fm.slug;
            script.title = fm.title;
            script.kind = kind;
            script.entrypoint = fm.entrypoint;
            script.enabled = fm.enabled;
            script.version = fm.version;
            script.file_path = rel_path;
            script.status = status;
            script.content = None;
            script.created_at = fm.created_at;
            script.updated_at = fm.updated_at;
            qs::update_script(conn, &script)?;
            Ok(false)
        }
        Err(_) => {
            let script = Script {
                id: fm.id,
                project_id: project_id.to_string(),
                slug: fm.slug,
                title: fm.title,
                kind,
                entrypoint: fm.entrypoint,
                enabled: fm.enabled,
                version: fm.version,
                file_path: rel_path,
                status,
                content: None,
                created_at: fm.created_at,
                updated_at: fm.updated_at,
            };
            qs::insert_script(conn, &script)?;
            Ok(true)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::Database;
    use crate::db::queries::project::{insert_project, make_test_project};
    use crate::model::ScriptKind;
    use tempfile::TempDir;

    fn setup() -> (Database, TempDir) {
        let db = Database::open_in_memory().unwrap();
        db.migrate().unwrap();
        insert_project(db.conn(), &make_test_project("p1", "blog")).unwrap();
        let dir = TempDir::new().unwrap();
        (db, dir)
    }

    #[test]
    fn rebuild_creates_script_from_lua() {
        let (db, dir) = setup();
        let scripts_dir = dir.path().join("scripts");
        fs::create_dir_all(&scripts_dir).unwrap();

        let content = "\
---
id: \"lua-script-1\"
slug: \"my-macro\"
title: \"My Macro\"
kind: \"macro\"
entrypoint: \"render\"
enabled: true
version: 1
createdAt: \"2024-01-01T00:00:00.000Z\"
updatedAt: \"2024-01-01T00:00:00.000Z\"
---
function render()
  return \"<p>hello</p>\"
end
";
        fs::write(scripts_dir.join("my-macro.lua"), content).unwrap();

        let report = rebuild_scripts_from_filesystem(db.conn(), dir.path(), "p1").unwrap();

        assert_eq!(report.created, 1);
        assert_eq!(report.updated, 0);
        assert!(report.errors.is_empty(), "errors: {:?}", report.errors);

        let script = qs::get_script_by_id(db.conn(), "lua-script-1").unwrap();
        assert_eq!(script.slug, "my-macro");
        assert_eq!(script.title, "My Macro");
        assert_eq!(script.kind, ScriptKind::Macro);
        assert_eq!(script.entrypoint, "render");
        assert!(script.enabled);
        assert_eq!(script.version, 1);
        assert_eq!(script.status, ScriptStatus::Published);
        assert!(
            script.content.is_none(),
            "published script should have content=None in DB"
        );
        assert_eq!(script.file_path, "scripts/my-macro.lua");
    }

    #[test]
    fn rebuild_defaults_missing_entrypoint_from_script_kind() {
        let (db, dir) = setup();
        let scripts_dir = dir.path().join("scripts");
        fs::create_dir_all(&scripts_dir).unwrap();

        for (id, kind, expected_entrypoint) in [
            ("legacy-macro", "macro", "render"),
            ("legacy-utility", "utility", "main"),
            ("legacy-transform", "transform", "main"),
        ] {
            let content = format!(
                "---\nid: \"{id}\"\nslug: \"{id}\"\ntitle: \"{id}\"\nkind: \"{kind}\"\nenabled: true\nversion: 1\ncreatedAt: \"2024-01-01T00:00:00.000Z\"\nupdatedAt: \"2024-01-01T00:00:00.000Z\"\n---\nfunction {expected_entrypoint}() end\n"
            );
            fs::write(scripts_dir.join(format!("{id}.lua")), content).unwrap();
        }

        let report = rebuild_scripts_from_filesystem(db.conn(), dir.path(), "p1").unwrap();

        assert_eq!(report.created, 3);
        assert!(report.errors.is_empty(), "errors: {:?}", report.errors);
        assert_eq!(
            qs::get_script_by_id(db.conn(), "legacy-macro")
                .unwrap()
                .entrypoint,
            "render"
        );
        assert_eq!(
            qs::get_script_by_id(db.conn(), "legacy-utility")
                .unwrap()
                .entrypoint,
            "main"
        );
        assert_eq!(
            qs::get_script_by_id(db.conn(), "legacy-transform")
                .unwrap()
                .entrypoint,
            "main"
        );
    }

    #[test]
    fn rebuild_updates_existing() {
        let (db, dir) = setup();

        // Insert a script in DB first
        let existing = Script {
            id: "existing-script".to_string(),
            project_id: "p1".to_string(),
            slug: "old-script".to_string(),
            title: "Old Script".to_string(),
            kind: ScriptKind::Macro,
            entrypoint: "render".to_string(),
            enabled: false,
            version: 1,
            file_path: "scripts/old-script.lua".to_string(),
            status: ScriptStatus::Draft,
            content: Some("old content".to_string()),
            created_at: 1000,
            updated_at: 2000,
        };
        qs::insert_script(db.conn(), &existing).unwrap();

        // Write updated file
        let scripts_dir = dir.path().join("scripts");
        fs::create_dir_all(&scripts_dir).unwrap();

        let content = "\
---
id: \"existing-script\"
slug: \"updated-script\"
title: \"Updated Script\"
kind: \"transform\"
entrypoint: \"process\"
enabled: true
version: 5
createdAt: \"2024-01-01T00:00:00.000Z\"
updatedAt: \"2024-01-01T00:00:00.000Z\"
---
function process()
  return \"updated\"
end
";
        fs::write(scripts_dir.join("updated-script.lua"), content).unwrap();

        let report = rebuild_scripts_from_filesystem(db.conn(), dir.path(), "p1").unwrap();

        assert_eq!(report.created, 0);
        assert_eq!(report.updated, 1);
        assert!(report.errors.is_empty());

        let script = qs::get_script_by_id(db.conn(), "existing-script").unwrap();
        assert_eq!(script.slug, "updated-script");
        assert_eq!(script.title, "Updated Script");
        assert_eq!(script.kind, ScriptKind::Transform);
        assert_eq!(script.entrypoint, "process");
        assert!(script.enabled);
        assert_eq!(script.version, 5);
        assert_eq!(script.status, ScriptStatus::Published);
        assert_eq!(script.updated_at, 1_704_067_200_000);
        assert!(script.content.is_none());
    }

    #[test]
    fn rebuild_idempotent() {
        let (db, dir) = setup();
        let scripts_dir = dir.path().join("scripts");
        fs::create_dir_all(&scripts_dir).unwrap();

        let content = "\
---
id: \"idem-script\"
slug: \"idem\"
title: \"Idempotent\"
kind: \"utility\"
entrypoint: \"run\"
enabled: true
version: 1
createdAt: \"2024-01-01T00:00:00.000Z\"
updatedAt: \"2024-01-01T00:00:00.000Z\"
---
function run() end
";
        fs::write(scripts_dir.join("idem.lua"), content).unwrap();

        // First run
        let r1 = rebuild_scripts_from_filesystem(db.conn(), dir.path(), "p1").unwrap();
        assert_eq!(r1.created, 1);
        assert_eq!(r1.updated, 0);

        // Second run - should update, not create
        let r2 = rebuild_scripts_from_filesystem(db.conn(), dir.path(), "p1").unwrap();
        assert_eq!(r2.created, 0);
        assert_eq!(r2.updated, 1);

        // Still only one script in DB
        let list = qs::list_scripts_by_project(db.conn(), "p1").unwrap();
        assert_eq!(list.len(), 1);
    }
}
