// Cooperative matrix support.
// Prepared for wgpu's upcoming cooperative matrix API for GPU-accelerated matrix ops.
// Note: EXPERIMENTAL_COOPERATIVE_MATRIX is not yet in published wgpu 27.
// This module provides the data structures and API surface so consumers are ready
// when the feature ships. Currently always reports as disabled.

/// Cooperative matrix context — queries and stores hardware capabilities.
#[derive(Default)]
pub struct CooperativeMatrixContext {
    pub enabled: bool,
    pub configs: Vec<CoopMatConfig>,
}

/// A hardware-supported cooperative matrix configuration.
#[derive(Debug, Clone, Copy)]
pub struct CoopMatConfig {
    pub m: u32,
    pub n: u32,
    pub k: u32,
    pub saturating: bool,
}

impl CooperativeMatrixContext {
    /// Query cooperative matrix support from the adapter.
    /// Currently always returns disabled — the wgpu API for cooperative matrices
    /// is not yet published in wgpu 27. When it ships, this will query the adapter.
    pub fn init_if_supported(_adapter: &wgpu::Adapter, _features: wgpu::Features) -> Self {
        Self::default()
    }

    /// Find a configuration matching the given tile dimensions.
    pub fn find_config(&self, m: u32, n: u32, k: u32) -> Option<&CoopMatConfig> {
        self.configs.iter().find(|c| c.m == m && c.n == n && c.k == k)
    }

    /// Check if any f32 configuration is available.
    pub fn has_f32_support(&self) -> bool {
        !self.configs.is_empty()
    }

    /// Summary for display.
    pub fn summary(&self) -> String {
        if !self.enabled {
            return "CoopMat: not available".into();
        }
        let tiles: Vec<String> = self
            .configs
            .iter()
            .map(|c| format!("{}x{}x{}", c.m, c.n, c.k))
            .collect();
        format!("CoopMat: {}", tiles.join(", "))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_disabled() {
        let ctx = CooperativeMatrixContext::default();
        assert!(!ctx.enabled);
        assert!(ctx.configs.is_empty());
    }

    #[test]
    fn summary_when_disabled() {
        let ctx = CooperativeMatrixContext::default();
        assert_eq!(ctx.summary(), "CoopMat: not available");
    }

    #[test]
    fn find_config_empty() {
        let ctx = CooperativeMatrixContext::default();
        assert!(ctx.find_config(8, 8, 8).is_none());
    }
}
