//! Frame encoding module
//!
//! Encodes captured frames for transmission. Supports:
//! - Raw BGRA + Zstd compression (lowest latency, LAN mode)
//! - VP9 software encoding (universal fallback)
//! - H264 hardware encoding (when GPU available)

mod raw;

pub use raw::RawEncoder;

use crate::capture::CapturedFrame;
use crate::proto::{VideoFrame, RawFrame, DirtyRect as ProtoDirtyRect};
use anyhow::Result;

/// Encoded frame ready for transmission
#[derive(Debug)]
pub struct EncodedFrame {
    /// Protobuf video frame message
    pub frame: VideoFrame,

    /// Size in bytes after encoding
    pub size: usize,

    /// Whether this is a keyframe (full frame)
    pub is_keyframe: bool,
}

/// Frame encoder trait
pub trait Encoder: Send {
    /// Encode a captured frame
    fn encode(&mut self, frame: &CapturedFrame) -> Result<EncodedFrame>;

    /// Request a keyframe on next encode
    fn request_keyframe(&mut self);

    /// Get encoder name/type
    fn name(&self) -> &str;
}

/// Create an encoder based on configuration
pub fn create_encoder(codec: &str, quality: u32) -> Result<Box<dyn Encoder>> {
    match codec.to_lowercase().as_str() {
        "raw" | "zstd" => Ok(Box::new(RawEncoder::new(quality)?)),
        // "vp9" => Ok(Box::new(Vp9Encoder::new(quality)?)),
        // "h264" => Ok(Box::new(H264Encoder::new(quality)?)),
        "auto" | _ => {
            // Default to raw for now (best for LAN)
            Ok(Box::new(RawEncoder::new(quality)?))
        }
    }
}
