//! Response DTOs for Aphoria endpoints.

use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

use super::types::{FindingDto, PatternDto, ScanSummaryDto};

// ============================================================================
// Bless Endpoint DTOs
// ============================================================================

/// Response from blessing a code pattern.
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct BlessResponse {
    /// Whether the operation succeeded.
    pub success: bool,

    /// Status message.
    pub message: String,

    /// The concept path that was blessed.
    pub concept_path: String,
}

// ============================================================================
// Export Policy DTOs
// ============================================================================

/// Response from exporting a policy.
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct ExportPolicyResponse {
    /// Path where the pack was saved (if output_path was provided).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pack_path: Option<String>,

    /// Number of assertions exported.
    pub assertions_count: usize,

    /// Number of aliases exported.
    pub aliases_count: usize,

    /// Hex-encoded issuer public key (first 8 chars).
    pub issuer_hex: String,

    /// Name of the exported pack.
    pub pack_name: String,

    /// Version of the exported pack.
    pub pack_version: String,
}

// ============================================================================
// Import Policy DTOs
// ============================================================================

/// Response from importing a policy.
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct ImportPolicyResponse {
    /// Whether the import succeeded.
    pub success: bool,

    /// Number of assertions imported.
    pub assertions_imported: usize,

    /// Number of aliases imported.
    pub aliases_imported: usize,

    /// Name of the imported pack.
    pub pack_name: String,

    /// Version of the imported pack.
    pub pack_version: String,

    /// Hex-encoded issuer public key (first 8 chars).
    pub issuer_hex: String,
}

// ============================================================================
// Scan Endpoint DTOs
// ============================================================================

/// Response from a scan operation.
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct ScanResponse {
    /// Project name.
    pub project: String,

    /// Unique scan ID.
    pub scan_id: String,

    /// Number of files scanned.
    pub files_scanned: usize,

    /// Number of claims extracted.
    pub claims_extracted: usize,

    /// Findings (conflicts detected).
    pub findings: Vec<FindingDto>,

    /// Summary counts by verdict.
    pub summary: ScanSummaryDto,
}

// ============================================================================
// Push Observations Endpoint DTOs (Hosted Mode)
// ============================================================================

/// Response from pushing observations.
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct PushObservationsResponse {
    /// Number of observations accepted (new or updated).
    pub accepted: usize,

    /// Number of observations deduplicated (already existed with same hash).
    pub deduplicated: usize,

    /// Hashes of created assertions (hex-encoded).
    pub hashes: Vec<String>,
}

// ============================================================================
// Community Corpus Contribution DTOs
// ============================================================================

/// Response from pushing community observations.
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct PushCommunityObservationsResponse {
    /// Number of observations recorded.
    pub recorded: usize,

    /// Number of new patterns discovered (not seen before).
    pub new_patterns: usize,

    /// Number of existing patterns updated (incremented counts).
    pub updated_patterns: usize,
}

/// Response containing popular patterns from the community corpus.
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct GetPatternsResponse {
    /// The patterns matching the query criteria.
    pub patterns: Vec<PatternDto>,

    /// Total number of patterns matching (before limit applied).
    pub total_matching: usize,
}

// ============================================================================
// Scan History Endpoint DTOs
// ============================================================================

/// A single scan from the scan history.
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct ScanListItem {
    /// Unique scan ID.
    pub scan_id: String,

    /// Project name.
    pub project: String,

    /// Number of files scanned.
    pub files_scanned: usize,

    /// Number of claims extracted.
    pub claims_extracted: usize,

    /// Summary counts by verdict.
    pub summary: ScanSummaryDto,

    /// Unix timestamp of when the scan was performed.
    pub timestamp: u64,

    /// Findings (conflicts detected).
    pub findings: Vec<FindingDto>,
}

/// Response containing recent scan history.
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct ListScansResponse {
    /// Recent scans, newest first.
    pub scans: Vec<ScanListItem>,
}
