use std::path::Path;

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

pub struct XcodePlugin;

impl Plugin for XcodePlugin {
    fn name(&self) -> &str {
        "xcode"
    }

    fn description(&self) -> &str {
        "Xcode simulators, caches, and derived data"
    }

    #[cfg(target_os = "macos")]
    fn is_available(&self) -> bool {
        let home = utils::home_dir();
        home.join("Library/Developer/CoreSimulator").is_dir()
            || home.join("Library/Developer/Xcode").is_dir()
    }

    #[cfg(not(target_os = "macos"))]
    fn is_available(&self) -> bool {
        false
    }

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

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

        // CoreSimulator devices (~10GB)
        let sim_devices = home.join("Library/Developer/CoreSimulator/Devices");
        if sim_devices.is_dir() {
            let size = utils::dir_size(&sim_devices);
            targets.push(CleanTarget {
                label: format!("{}", sim_devices.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(sim_devices),
                plugin_name: self.name().to_string(),
                safety: Safety::Unsafe,
                explanation: "iOS simulator devices and data".to_string(),
            });
        }

        // CoreSimulator caches
        let sim_caches = home.join("Library/Developer/CoreSimulator/Caches");
        if sim_caches.is_dir() {
            let size = utils::dir_size(&sim_caches);
            targets.push(CleanTarget {
                label: format!("{}", sim_caches.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(sim_caches),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "iOS simulator caches, regenerated automatically".to_string(),
            });
        }

        // DerivedData
        let derived_data = home.join("Library/Developer/Xcode/DerivedData");
        if derived_data.is_dir() {
            let size = utils::dir_size(&derived_data);
            targets.push(CleanTarget {
                label: format!("{}", derived_data.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(derived_data),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Xcode derived data, regenerated on next build".to_string(),
            });
        }

        // Archives
        let archives = home.join("Library/Developer/Xcode/Archives");
        if archives.is_dir() {
            let size = utils::dir_size(&archives);
            targets.push(CleanTarget {
                label: format!("{}", archives.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(archives),
                plugin_name: self.name().to_string(),
                safety: Safety::Unsafe,
                explanation: "Xcode build archives".to_string(),
            });
        }

        // iOS DeviceSupport
        let device_support = home.join("Library/Developer/Xcode/iOS DeviceSupport");
        if device_support.is_dir() {
            let size = utils::dir_size(&device_support);
            targets.push(CleanTarget {
                label: format!("{}", device_support.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(device_support),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "iOS device support files, re-downloaded as needed".to_string(),
            });
        }

        // Xcode caches
        let xcode_cache = home.join("Library/Caches/com.apple.dt.Xcode");
        if xcode_cache.is_dir() {
            let size = utils::dir_size(&xcode_cache);
            targets.push(CleanTarget {
                label: format!("{}", xcode_cache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(xcode_cache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Xcode caches, regenerated automatically".to_string(),
            });
        }

        targets
    }
}
