//! SIP protocol library for the proxy engine.
//!
//! Provides SIP message parsing/serialization, dialog state management,
//! SDP handling, Digest authentication, and URI rewriting.
//! Ported from the TypeScript `ts/sip/` library.

pub mod dialog;
pub mod helpers;
pub mod message;
pub mod rewrite;

/// Network endpoint (address + port + optional negotiated codec).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Endpoint {
    pub address: String,
    pub port: u16,
    /// First payload type from the SDP `m=audio` line (the preferred codec).
    pub codec_pt: Option<u8>,
    /// SDP media kind from the `m=` line.
    pub media_kind: SdpMediaKind,
    /// SDP transport token from the `m=` line (e.g. `RTP/AVP`, `udptl`).
    pub transport: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SdpMediaKind {
    Audio,
    Image,
    Application,
    Unknown,
}

impl SdpMediaKind {
    pub fn as_sdp_token(&self) -> &'static str {
        match self {
            Self::Audio => "audio",
            Self::Image => "image",
            Self::Application => "application",
            Self::Unknown => "unknown",
        }
    }

    pub fn from_sdp_token(token: &str) -> Self {
        match token.to_ascii_lowercase().as_str() {
            "audio" => Self::Audio,
            "image" => Self::Image,
            "application" => Self::Application,
            _ => Self::Unknown,
        }
    }
}

impl Endpoint {
    pub fn is_audio_rtp(&self) -> bool {
        self.media_kind == SdpMediaKind::Audio
            && self.transport.to_ascii_uppercase().starts_with("RTP/")
    }

    pub fn is_t38_udptl(&self) -> bool {
        self.media_kind == SdpMediaKind::Image && self.transport.eq_ignore_ascii_case("udptl")
    }
}
