use super::{FIXTURE_FORMAT_VERSION, FixtureDocument, FixtureValidationReport};

pub fn validate_fixture_document(document: &FixtureDocument) -> FixtureValidationReport {
    let mut issues = Vec::new();

    if document.format_version != FIXTURE_FORMAT_VERSION {
        issues.push(format!(
            "unsupported format_version {} (expected {})",
            document.format_version, FIXTURE_FORMAT_VERSION
        ));
    }
    if document.fixture_id.trim().is_empty() {
        issues.push("fixture_id must not be empty".to_string());
    }
    if document.source.kind.trim().is_empty() {
        issues.push("source.kind must not be empty".to_string());
    }
    if document.commands.is_empty() {
        issues.push("fixture must contain at least one command".to_string());
    }
    if let Err(err) = document.state.validate() {
        issues.push(format!("invalid runtime state: {err}"));
    }

    for (index, command) in document.commands.iter().enumerate() {
        if let Err(err) = command.validate() {
            issues.push(format!("invalid command at index {index}: {err}"));
        }
    }

    FixtureValidationReport {
        fixture_id: document.fixture_id.clone(),
        valid: issues.is_empty(),
        issue_count: issues.len(),
        issues,
    }
}
