use crate::cli::args::Method;
use crate::core::collection::{
    Auth, Collection, CollectionItem, Folder, KVParam, Request, RequestBody,
};
use crate::core::parser::SourceParser;
use crate::core::parser::models::{FieldType, Model, ModelField, ModelRegistry};
use regex::Regex;
use std::path::Path;
use walkdir::WalkDir;

pub struct FastApiParser;

impl FastApiParser {
    fn parse_python_type(type_str: &str) -> FieldType {
        let type_str = type_str.trim();
        if type_str.is_empty() {
            return FieldType::Unknown;
        }

        match type_str.to_lowercase().as_str() {
            "str" => FieldType::String,
            "int" | "float" => FieldType::Number,
            "bool" => FieldType::Boolean,
            "datetime" | "date" | "time" => FieldType::DateTime,
            t if t.starts_with("dict[") || t.starts_with("dict") => {
                FieldType::Map(Box::new(FieldType::String), Box::new(FieldType::Unknown))
            }
            t if t.starts_with("list[") || t.starts_with("list") => {
                if let Some(inner) = t.strip_prefix("list[").and_then(|s| s.strip_suffix(']')) {
                    FieldType::Array(Box::new(Self::parse_python_type(inner)))
                } else {
                    FieldType::Array(Box::new(FieldType::Unknown))
                }
            }
            _ => FieldType::Object(type_str.to_string()),
        }
    }
}

impl SourceParser for FastApiParser {
    fn parse(&self, project_path: &Path) -> anyhow::Result<Collection> {
        let mut collection = Collection::new(format!(
            "{} (FastAPI)",
            project_path
                .file_name()
                .unwrap_or_default()
                .to_string_lossy()
        ));

        collection.env_vars.push(KVParam {
            key: "baseUrl".to_string(),
            value: "http://localhost:8000".to_string(),
            enabled: true,
            description: Some("Base URL for the service".to_string()),
        });

        let mut registry = ModelRegistry::new();

        // Pass 1: Model Discovery
        let class_regex = Regex::new(r"(?m)^class\s+([a-zA-Z0-9_]+)(?:\s*\(.*\))?:").unwrap();
        let field_regex = Regex::new(r"^\s+([a-zA-Z0-9_]+)\s*:\s*([a-zA-Z0-9_\[\]]+)").unwrap();

        for entry in WalkDir::new(project_path)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.path().extension().map_or(false, |ext| ext == "py"))
        {
            let path_str = entry.path().to_string_lossy();
            if path_str.contains("venv")
                || path_str.contains("__pycache__")
                || path_str.contains(".git")
            {
                continue;
            }

            if let Ok(content) = std::fs::read_to_string(entry.path()) {
                let lines: Vec<&str> = content.lines().collect();
                let mut i = 0;
                while i < lines.len() {
                    if let Some(cap) = class_regex.captures(lines[i]) {
                        let class_name = cap[1].to_string();
                        let mut fields = Vec::new();
                        i += 1;
                        while i < lines.len()
                            && (lines[i].starts_with(' ')
                                || lines[i].starts_with('\t')
                                || lines[i].is_empty())
                        {
                            if let Some(fcap) = field_regex.captures(lines[i]) {
                                fields.push(ModelField {
                                    name: fcap[1].to_string(),
                                    field_type: Self::parse_python_type(&fcap[2]),
                                });
                            }
                            i += 1;
                        }
                        registry.add_model(Model {
                            name: class_name,
                            fields,
                        });
                    } else {
                        i += 1;
                    }
                }
            }
        }

        // Pass 2: Endpoint Extraction
        let route_regex =
            Regex::new(r#"@(?:app|router)\.(get|post|put|patch|delete)\s*\(\s*['"]([^'"]+)['"]"#)
                .unwrap();
        // Capture function signature to find typed parameters
        let func_regex = Regex::new(r"def\s+[a-zA-Z0-9_]+\s*\(([^)]*)\)").unwrap();

        for entry in WalkDir::new(project_path)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.path().extension().map_or(false, |ext| ext == "py"))
        {
            let path_str = entry.path().to_string_lossy();
            if path_str.contains("venv")
                || path_str.contains("__pycache__")
                || path_str.contains(".git")
            {
                continue;
            }

            if let Ok(content) = std::fs::read_to_string(entry.path()) {
                // Detect router prefix
                let router_prefix_regex =
                    Regex::new(r#"APIRouter\s*\(\s*(?:[\s\S]*?prefix\s*=\s*)?['"]([^'"]+)['"]"#).unwrap();
                let router_prefix = router_prefix_regex
                    .captures(&content)
                    .map(|c| c[1].to_string())
                    .unwrap_or_default();

                let mut requests = Vec::new();

                for cap in route_regex.captures_iter(&content) {
                    let method_str = &cap[1];
                    let url_path = &cap[2];

                    let method = match method_str.to_lowercase().as_str() {
                        "post" => Method::Post,
                        "put" => Method::Put,
                        "patch" => Method::Patch,
                        "delete" => Method::Delete,
                        _ => Method::Get,
                    };

                    let mut body = RequestBody::default();

                    // Look for the function definition immediately following the decorator
                    let start_pos = cap.get(0).unwrap().end();
                    if let Some(func_cap) = func_regex.captures(&content[start_pos..]) {
                        let params = &func_cap[1];
                        for param in params.split(',') {
                            if let Some((_, type_hint)) = param.split_once(':') {
                                let type_hint = type_hint.trim();
                                if registry.models.contains_key(type_hint) {
                                    if let Some(json_body) = registry.generate_json(type_hint) {
                                        body = RequestBody::raw(
                                            json_body,
                                            "application/json".to_string(),
                                        );
                                    }
                                }
                            }
                        }
                    }

                    let full_path = format!(
                        "{}/{}",
                        router_prefix.trim_end_matches('/'),
                        url_path.trim_start_matches('/')
                    );
                    let full_path = if full_path.is_empty() {
                        String::new()
                    } else if full_path.starts_with('/') {
                        full_path
                    } else {
                        format!("/{}", full_path)
                    };

                    requests.push(CollectionItem::Request(Request {
                        id: uuid::Uuid::new_v4().to_string(),
                        name: format!("{} {}", method_str.to_uppercase(), full_path),
                        method,
                        url: format!("{{{{baseUrl}}}}{}", full_path),
                        params: Vec::new(),
                        headers: Vec::new(),
                        auth: Auth::default(),
                        body,
                        pre_request_script: None,
                        post_response_script: None,
                    }));
                }

                if !requests.is_empty() {
                    let file_name = entry
                        .path()
                        .file_name()
                        .unwrap_or_default()
                        .to_string_lossy()
                        .to_string();
                    let mut folder = Folder::new(file_name);
                    folder.items = requests;
                    collection.items.push(CollectionItem::Folder(folder));
                }
            }
        }

        Ok(collection)
    }
}
