//! Integration tests for scale-adaptive promotion thresholds.
//!
//! Verifies that promotion criteria automatically adjust based on organization size,
//! enabling small teams to see value immediately while maintaining quality gates
//! for larger organizations.

use aphoria::corpus::thresholds::{PromotionDecision, ScaleAdaptiveThresholds, ScaleTier};
use stemedb_core::types::SourceClass;

#[test]
fn test_micro_team_sees_patterns() {
    let thresholds = ScaleAdaptiveThresholds::default();

    // Micro team with 3 projects, pattern appears in 2
    let decision = thresholds.evaluate(
        2,     // project_count
        3,     // total_projects
        false, // no authority
        None,
    );

    // With adaptive thresholds:
    // - Scale tier: Micro (1-5 projects)
    // - Emerging min_projects: max(2, 0.50*3) = max(2, 1.5) = 2
    // - Adoption rate: 2/3 = 67% >= 50%
    // Micro emerging auto-promotes for immediate visibility
    assert_eq!(decision, PromotionDecision::AutoPromote(SourceClass::Community));
}

#[test]
fn test_micro_team_regulatory_disabled() {
    let thresholds = ScaleAdaptiveThresholds::default();

    // Micro team with 5 projects, pattern appears in all 5 with RFC match
    let decision = thresholds.evaluate(
        5,                  // project_count
        5,                  // total_projects
        true,               // has authority
        Some("rfc://1234"), // RFC scheme
    );

    // Regulatory tier is disabled for micro teams
    // Falls through to emerging tier, which auto-promotes for immediate visibility
    assert_eq!(decision, PromotionDecision::AutoPromote(SourceClass::Community));
}

#[test]
fn test_small_team_enables_all_tiers() {
    let thresholds = ScaleAdaptiveThresholds::default();

    // Small team with 10 projects, pattern in 9 with RFC match
    let decision = thresholds.evaluate(
        9,                  // project_count
        10,                 // total_projects
        true,               // has authority
        Some("rfc://5246"), // RFC scheme
    );

    // Small tier regulatory: max(5, 0.90*10) = max(5, 9) = 9
    // Adoption rate: 9/10 = 90% >= 90%
    // Should auto-promote to regulatory
    assert_eq!(decision, PromotionDecision::AutoPromote(SourceClass::Regulatory));
}

#[test]
fn test_enterprise_maintains_strict_thresholds() {
    let thresholds = ScaleAdaptiveThresholds::default();

    // Enterprise with 1000 projects, pattern in 950 with RFC match
    let decision = thresholds.evaluate(
        950,                // project_count
        1000,               // total_projects
        true,               // has authority
        Some("rfc://9110"), // RFC scheme
    );

    // Enterprise tier: max(100, 0.95*1000) = max(100, 950) = 950
    // Adoption rate: 950/1000 = 95% >= 95%
    // Should auto-promote to regulatory (backward compatible behavior)
    assert_eq!(decision, PromotionDecision::AutoPromote(SourceClass::Regulatory));
}

#[test]
fn test_scale_tier_progression() {
    // Verify scale tier boundaries
    assert_eq!(ScaleTier::from_total_projects(1), ScaleTier::Micro);
    assert_eq!(ScaleTier::from_total_projects(5), ScaleTier::Micro);
    assert_eq!(ScaleTier::from_total_projects(6), ScaleTier::Small);
    assert_eq!(ScaleTier::from_total_projects(25), ScaleTier::Small);
    assert_eq!(ScaleTier::from_total_projects(26), ScaleTier::Medium);
    assert_eq!(ScaleTier::from_total_projects(100), ScaleTier::Medium);
    assert_eq!(ScaleTier::from_total_projects(101), ScaleTier::Large);
    assert_eq!(ScaleTier::from_total_projects(500), ScaleTier::Large);
    assert_eq!(ScaleTier::from_total_projects(501), ScaleTier::Enterprise);
}

#[test]
fn test_adaptive_floor_prevents_noise() {
    let thresholds = ScaleAdaptiveThresholds::default();

    // Micro team with 3 projects, pattern appears in only 1
    let decision = thresholds.evaluate(
        1,     // project_count
        3,     // total_projects
        false, // no authority
        None,
    );

    // Even though 1/3 = 33% meets percentage (50% of 3 = 1.5),
    // the floor of 2 prevents single-project noise
    // Adoption rate: 1/3 = 33% < 50%
    assert_eq!(decision, PromotionDecision::Skip);
}

#[test]
fn test_medium_team_clinical_tier() {
    let thresholds = ScaleAdaptiveThresholds::default();

    // Medium team with 50 projects, pattern in 38 with OWASP match
    let decision = thresholds.evaluate(
        38,                         // project_count
        50,                         // total_projects
        true,                       // has authority
        Some("owasp://top-10/a01"), // OWASP scheme
    );

    // Medium tier clinical: max(10, 0.75*50) = max(10, 37.5) = 38
    // Adoption rate: 38/50 = 76% >= 75%
    // Should auto-promote to clinical
    assert_eq!(decision, PromotionDecision::AutoPromote(SourceClass::Clinical));
}
