// ─── server/src/handshake.rs ───────────────────────────────────────────────
use tonic::{Request, Response, Status};

use crate::camera;
use lesavka_common::lesavka::{
    Empty, HandshakeSet,
    handshake_server::{Handshake, HandshakeServer},
};

pub struct HandshakeSvc;

#[tonic::async_trait]
impl Handshake for HandshakeSvc {
    async fn get_capabilities(
        &self,
        _req: Request<Empty>,
    ) -> Result<Response<HandshakeSet>, Status> {
        let cfg = camera::update_camera_config();
        let (eye_width, eye_height, eye_fps) = crate::video::eye_source_profile();
        let camera_enabled = match cfg.output {
            camera::CameraOutput::Uvc => std::env::var("LESAVKA_DISABLE_UVC").is_err(),
            camera::CameraOutput::Hdmi => true,
        };
        let microphone = std::env::var("LESAVKA_DISABLE_UAC").is_err();
        Ok(Response::new(HandshakeSet {
            camera: camera_enabled,
            microphone,
            camera_output: cfg.output.as_str().to_string(),
            camera_codec: cfg.codec.as_str().to_string(),
            camera_width: cfg.width,
            camera_height: cfg.height,
            camera_fps: cfg.fps,
            eye_width,
            eye_height,
            eye_fps,
            server_version: crate::VERSION.to_string(),
            server_revision: crate::REVISION.to_string(),
            bundled_webcam_media: camera_enabled && microphone,
        }))
    }
}

impl HandshakeSvc {
    pub fn server() -> HandshakeServer<Self> {
        HandshakeServer::new(Self)
    }
}
