use git2::Repository;
use std::collections::HashMap;
use std::path::PathBuf;

use crate::errors::KamError;
use crate::types::kam_toml::KamToml;
use crate::types::kam_toml::enums::ModuleType;

const KERNELSU_RESERVED_REPO_NAMES: &[&str] = &[
    ".github",
    "submission",
    "developers",
    "modules",
    "org.kernelsu.example",
    "module_release",
];

fn trim_description(input: &str) -> String {
    let trimmed = input.trim();
    if trimmed.chars().count() > 200 {
        let shortened: String = trimmed.chars().take(200).collect();
        format!("{shortened}...")
    } else {
        trimmed.to_string()
    }
}

fn is_readme_description_line(line: &str) -> bool {
    let trimmed = line.trim();
    let lower = trimmed.to_ascii_lowercase();

    if trimmed.len() <= 10
        || trimmed.starts_with('#')
        || trimmed.starts_with("```")
        || trimmed.starts_with("<!--")
        || trimmed.starts_with('!')
        || trimmed.starts_with('|')
        || trimmed.starts_with("---")
        || trimmed.starts_with("***")
        || trimmed.starts_with("===")
    {
        return false;
    }

    if lower.starts_with("<div")
        || lower.starts_with("</div")
        || lower.starts_with("<p")
        || lower.starts_with("</p")
        || lower.starts_with("<img")
        || lower.starts_with("<a ")
        || lower.starts_with("<br")
        || lower.contains("img.shields.io")
        || lower.contains("github.com/")
        || lower.contains("badge")
    {
        return false;
    }

    trimmed
        .chars()
        .any(|c| c.is_alphanumeric() || ('\u{4e00}'..='\u{9fff}').contains(&c))
}

fn extract_readme_description(content: &str) -> Option<String> {
    for line in content.lines() {
        if is_readme_description_line(line) {
            return Some(trim_description(line));
        }
    }
    None
}

// 初始化前的数据结构
// 把各种参数和默认值都收集到这里
/// AUTO-GENERATED BY Kam/scripts/add_missing_docs.py: placeholder documentation for public struct 'PreInitData'. Replace with real documentation. (2025-12-30T22:23:21Z)
pub struct PreInitData {
    /// AUTO-GENERATED BY Kam/scripts/add_missing_docs.py: placeholder documentation for public field 'path' in 'PreInitData'. Replace with real documentation. (2025-12-30T22:23:21Z)
    pub path: PathBuf,
    /// AUTO-GENERATED BY Kam/scripts/add_missing_docs.py: placeholder documentation for public field 'id' in 'PreInitData'. Replace with real documentation. (2025-12-30T22:23:21Z)
    pub id: String,
    /// AUTO-GENERATED BY Kam/scripts/add_missing_docs.py: placeholder documentation for public field 'name' in 'PreInitData'. Replace with real documentation. (2025-12-30T22:23:21Z)
    pub name: String,
    /// AUTO-GENERATED BY Kam/scripts/add_missing_docs.py: placeholder documentation for public field 'version' in 'PreInitData'. Replace with real documentation. (2025-12-30T22:23:21Z)
    pub version: String,
    /// AUTO-GENERATED BY Kam/scripts/add_missing_docs.py: placeholder documentation for public field 'author' in 'PreInitData'. Replace with real documentation. (2025-12-30T22:23:21Z)
    pub author: String,
    /// AUTO-GENERATED BY Kam/scripts/add_missing_docs.py: placeholder documentation for public field 'description' in 'PreInitData'. Replace with real documentation. (2025-12-30T22:23:21Z)
    pub description: String,
    /// AUTO-GENERATED BY Kam/scripts/add_missing_docs.py: placeholder documentation for public field 'template_vars' in 'PreInitData'. Replace with real documentation. (2025-12-30T22:23:21Z)
    pub template_vars: HashMap<String, String>,
    /// AUTO-GENERATED BY Kam/scripts/add_missing_docs.py: placeholder documentation for public field 'impl_template' in 'PreInitData'. Replace with real documentation. (2025-12-30T22:23:21Z)
    pub impl_template: String,
    /// AUTO-GENERATED BY Kam/scripts/add_missing_docs.py: placeholder documentation for public field 'module_type' in 'PreInitData'. Replace with real documentation. (2025-12-30T22:23:21Z)
    pub module_type: ModuleType,
    /// AUTO-GENERATED BY Kam/scripts/add_missing_docs.py: placeholder documentation for public field 'update_json' in 'PreInitData'. Replace with real documentation. (2025-12-30T22:23:21Z)
    pub update_json: Option<String>,
    /// KernelSU module source URL for module.json sourceUrl.
    pub source_url: Option<String>,
    /// AUTO-GENERATED BY Kam/scripts/add_missing_docs.py: placeholder documentation for public field 'kam_toml' in 'PreInitData'. Replace with real documentation. (2025-12-30T22:23:21Z)
    pub kam_toml: KamToml,
}
