use axum::extract::State;
use conduwuit::{Err, Result, utils::content_disposition::make_content_disposition};
use conduwuit_service::media::{Dim, FileMeta};
use ruma::api::federation::authenticated_media::{
	Content, ContentMetadata, FileOrLocation, get_content, get_content_thumbnail,
};
use service::media::mxc::Mxc;

use crate::Ruma;

/// # `GET /_matrix/federation/v1/media/download/{mediaId}`
///
/// Load media from our server.
#[tracing::instrument(name = "media_get", level = "debug", skip_all)]
pub(crate) async fn get_content_route(
	State(services): State<crate::State>,
	body: Ruma<get_content::v1::Request>,
) -> Result<get_content::v1::Response> {
	let mxc = Mxc {
		server_name: services.globals.server_name(),
		media_id: &body.media_id,
	};

	let Some(FileMeta {
		content,
		content_type,
		content_disposition,
	}) = services.media.get(&mxc).await?
	else {
		return Err!(Request(NotFound("Media not found.")));
	};

	let content_disposition =
		make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), None);
	let content = Content::new(
		content.expect("entire file contents"),
		content_type.unwrap_or_default(),
		content_disposition,
	);

	Ok(get_content::v1::Response::new(
		ContentMetadata::new(),
		FileOrLocation::File(content),
	))
}

/// # `GET /_matrix/federation/v1/media/thumbnail/{mediaId}`
///
/// Load media thumbnail from our server.
#[tracing::instrument(name = "media_thumbnail_get", level = "debug", skip_all)]
pub(crate) async fn get_content_thumbnail_route(
	State(services): State<crate::State>,
	body: Ruma<get_content_thumbnail::v1::Request>,
) -> Result<get_content_thumbnail::v1::Response> {
	let dim = Dim::from_ruma(body.width, body.height, body.method.clone())?;
	let mxc = Mxc {
		server_name: services.globals.server_name(),
		media_id: &body.media_id,
	};

	let Some(FileMeta {
		content,
		content_type,
		content_disposition,
	}) = services.media.get_thumbnail(&mxc, &dim).await?
	else {
		return Err!(Request(NotFound("Media not found.")));
	};

	let content_disposition =
		make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), None);
	let content = Content::new(
		content.expect("entire file contents"),
		content_type.unwrap_or_default(),
		content_disposition,
	);

	Ok(get_content_thumbnail::v1::Response::new(
		ContentMetadata::new(),
		FileOrLocation::File(content),
	))
}
