//! PoE2 Wiki scraper and cache.
//!
//! Fetches and caches data from the Path of Exile 2 wiki.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum WikiError {
    #[error("HTTP error: {0}")]
    Http(#[from] reqwest::Error),

    #[error("Parse error: {0}")]
    Parse(String),

    #[error("Cache error: {0}")]
    Cache(String),

    #[error("Not found: {0}")]
    NotFound(String),
}

/// Wiki client with caching.
pub struct WikiClient {
    client: reqwest::Client,
    cache_dir: PathBuf,
    base_url: String,
}

/// Skill gem data from the wiki.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SkillGem {
    pub name: String,
    pub tags: Vec<String>,
    pub description: String,
    pub base_damage: Option<DamageInfo>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DamageInfo {
    pub min: f64,
    pub max: f64,
    pub damage_type: String,
}

/// Unique item data from the wiki.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UniqueItem {
    pub name: String,
    pub base_type: String,
    pub modifiers: Vec<String>,
    pub requirements: HashMap<String, i32>,
}

impl WikiClient {
    /// Create a new wiki client.
    pub fn new(cache_dir: PathBuf) -> Self {
        Self {
            client: reqwest::Client::new(),
            cache_dir,
            base_url: "https://www.poewiki.net/wiki/".to_owned(),
        }
    }

    /// Fetch skill gem data.
    pub async fn get_skill(&self, name: &str) -> Result<SkillGem, WikiError> {
        // TODO: Check cache first
        // TODO: Fetch from wiki API
        // TODO: Parse and cache result

        tracing::debug!("Fetching skill: {}", name);

        // Placeholder
        Err(WikiError::NotFound(name.to_owned()))
    }

    /// Fetch unique item data.
    pub async fn get_unique(&self, name: &str) -> Result<UniqueItem, WikiError> {
        // TODO: Implement
        tracing::debug!("Fetching unique: {}", name);
        Err(WikiError::NotFound(name.to_owned()))
    }

    /// Search for items/skills matching a query.
    pub async fn search(&self, query: &str) -> Result<Vec<String>, WikiError> {
        // TODO: Implement wiki search
        tracing::debug!("Searching wiki for: {}", query);
        Ok(Vec::new())
    }

    /// Clear the cache.
    pub fn clear_cache(&self) -> Result<(), WikiError> {
        // TODO: Clear cache directory
        Ok(())
    }
}
