use crate::aws::config;
use aws_sdk_sesv2::types::{Body, Content, Destination, EmailContent, Message};
use aws_sdk_sesv2::{Client, Error};
const HTML_KEYWORD: &str = "<html";
/// メールを送信します
pub async fn send_email(from: &str, to: &str, subject: &str, body: &str) -> Result<(), Error> {
	// 1. 設定読み込み（環境変数 / プロファイル など）
	let sdk_config = config().await;
	let client = Client::new(&sdk_config);

	// 2. メール本文の作成
	let subject = Content::builder().data(subject).charset("UTF-8").build()?;

	let text_body = Content::builder().data(body).charset("UTF-8").build()?;

	let body = if body.contains(HTML_KEYWORD) {
		// '>' でなく '<html' くらいが無難
		Body::builder().html(text_body).build()
	} else {
		Body::builder().text(text_body).build()
	};

	let message = Message::builder().subject(subject).body(body).build();

	let email_content = EmailContent::builder().simple(message).build();

	// 3. 宛先
	let dest = Destination::builder().to_addresses(to).build();

	// 4. 送信
	let resp = client
		.send_email()
		.from_email_address(from)
		.destination(dest)
		.content(email_content)
		.send()
		.await?;

	println!("Email sent! Message ID: {:?}", resp.message_id());

	Ok(())
}
#[cfg(test)]
mod tests {
	use super::*;

	// 実行すると本当に SES 経由でメールが送られるので注意
	#[tokio::test]
	async fn test_send_email() {
		let from = "info@surfic.com"; // SES で Verify 済み
		let to = "misumi3104@gmail.com";
		let subject = "mimamori メールアドレスの認証";
		let body = "something";

		let result = send_email(from, to, subject, &body).await;
		result.unwrap();
	}
}
