use chrono::Local;
use icalendar::{CalendarComponent, Component, DatePerhapsTime, EventLike};

use crate::dav::{DAVClient, DAVCalendar, DAVCredentials};

#[tauri::command]
pub async fn dav_find_scheme(url: &str) -> Result<String, String> {
	let url = reqwest::Url::parse(url).map_err(|e| format!("Invalid URL: {}", e))?;
	let scheme = DAVCredentials::find_scheme(&url).await.map_err(|e| format!("Failed to determine authentication scheme: {}", e))?;
	Ok(scheme.to_string())
}

#[tauri::command]
pub async fn dav_fetch_calendars(url: &str, credentials: DAVCredentials) -> Result<Vec<DAVCalendar>, String> {
	let mut client = DAVClient::new(url, credentials);
	client.init().await.map_err(|e| format!("Failed to initialize DAV client: {}", e))?;
	let calendars = client.get_calendars().await.map_err(|e| format!("Failed to fetch calendars: {}", e))?;
	Ok(calendars)
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DAVEvent {
	summary: String,
	start: Option<String>,
	end: Option<String>,
	description: Option<String>,
	location: Option<String>,
}

pub fn dateperhapstime_to_string(d: DatePerhapsTime) -> Option<String> {
	match d {
		DatePerhapsTime::DateTime(dt) => dt.try_into_utc(),
		DatePerhapsTime::Date(d) => d
			.and_hms_opt(0, 0, 0)
			.and_then(|dt| dt.and_local_timezone(Local).earliest())
			.map(|dt| dt.to_utc()),
	}.map(|dt| dt.to_rfc3339())
}

#[tauri::command]
pub async fn dav_fetch_events(calendar: DAVCalendar, credentials: DAVCredentials) -> Result<Vec<DAVEvent>, String> {
	let events = calendar.clone().get_events(&credentials).await.map_err(|e| format!("Failed to fetch events: {}", e))?;
	let mut res: Vec<DAVEvent> = Vec::new();
	for event in events {
		for component in &event.components {
			if let CalendarComponent::Event(event) = component {
				let location = event.get_location();
				if location.is_none() {
					continue;
				}
				let start_at = event.get_start().and_then(dateperhapstime_to_string);
				let end_at = event.get_end().and_then(dateperhapstime_to_string);
				res.push(DAVEvent {
					summary: event.get_summary().unwrap_or("No summary").to_string(),
					start: start_at,
					end: end_at,
					description: event.get_description().map(|s| s.to_string()),
					location: location.map(|s| s.to_string()),
				});
			}
		}
	}
	Ok(res)
}
