use std::path::Path;

use crate::plugin::{CleanAction, CleanTarget, Plugin, Safety};
use crate::utils;

pub struct SpotifyPlugin;

impl Plugin for SpotifyPlugin {
    fn name(&self) -> &str {
        "spotify"
    }

    fn description(&self) -> &str {
        "Spotify caches and offline data"
    }

    fn scan_directory(&self, _dir: &Path) -> Vec<CleanTarget> {
        Vec::new()
    }

    fn global_targets(&self) -> Vec<CleanTarget> {
        let mut targets = Vec::new();

        // Spotify cache (streaming cache, album art, etc.)
        let cache_dir = spotify_cache_dir();
        if cache_dir.is_dir() {
            let size = utils::dir_size(&cache_dir);
            targets.push(CleanTarget {
                label: format!("{}", cache_dir.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(cache_dir),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Spotify streaming cache, regenerated automatically".to_string(),
            });
        }

        // Spotify data in Application Support (offline storage, local files cache)
        let support_dir = spotify_support_dir();
        if support_dir.is_dir() {
            // PersistentCache stores offline tracks and large blobs
            let persistent_cache = support_dir.join("PersistentCache");
            if persistent_cache.is_dir() {
                let size = utils::dir_size(&persistent_cache);
                targets.push(CleanTarget {
                    label: format!("{}", persistent_cache.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(persistent_cache),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Spotify persistent cache, regenerated automatically".to_string(),
                });
            }

            // Storage directory
            let storage = support_dir.join("storage");
            if storage.is_dir() {
                let size = utils::dir_size(&storage);
                targets.push(CleanTarget {
                    label: format!("{}", storage.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(storage),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Spotify storage data, regenerated automatically".to_string(),
                });
            }

            // Watch history / playback data
            let watch_history = support_dir.join("watch-history.bnk");
            if watch_history.is_file() {
                let size = watch_history
                    .metadata()
                    .map(|m| utils::physical_size(&m))
                    .unwrap_or(0);
                targets.push(CleanTarget {
                    label: format!("{}", watch_history.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(watch_history),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Spotify playback history data".to_string(),
                });
            }
        }

        targets
    }

    fn is_available(&self) -> bool {
        spotify_cache_dir().is_dir() || spotify_support_dir().is_dir()
    }
}

fn spotify_support_dir() -> std::path::PathBuf {
    if cfg!(target_os = "macos") {
        utils::home_dir().join("Library/Application Support/Spotify")
    } else if cfg!(target_os = "linux") {
        utils::home_dir().join(".config/spotify")
    } else {
        utils::home_dir().join("AppData/Roaming/Spotify")
    }
}

fn spotify_cache_dir() -> std::path::PathBuf {
    if cfg!(target_os = "macos") {
        utils::home_dir().join("Library/Caches/com.spotify.client")
    } else if cfg!(target_os = "linux") {
        utils::home_dir().join(".cache/spotify")
    } else {
        utils::home_dir().join("AppData/Local/Spotify/Storage")
    }
}
