Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

33.8 KB
Raw Blame
//! User-initiated calendar mutation policy.
//!
//! Mutations write through to the provider first — Google stays the write
//! authority, exactly as ingestion assumes — and then persist the provider's
//! normalized echo through the same upsert path sync uses, so the local
//! projection is read-your-writes fresh and the next incremental sync
//! no-ops on the idempotency short-circuit.

use std::collections::HashMap;
mod copy;
mod copy_writes;
mod details;
mod guest_policy;
pub(crate) mod invite;
mod join;
mod organizer_copy;
mod replacement;

use chrono::Utc;
use uuid::Uuid;

use super::{
    models::{
        ActorInboxes, AttendeeResponseStatus, CalendarAttendee, CalendarAttendeeInput,
        CalendarCreationTarget, CalendarEvent, CalendarEventDraft, CalendarEventMutationTarget,
        CalendarEventPatch, CalendarEventUpsert, DisconnectedCalendar, EventReminders, EventTime,
        OccurrenceRange, REMINDER_METHOD_EMAIL, REMINDER_METHOD_POPUP, REMINDER_MINUTES_MAX,
        REMINDER_OVERRIDES_MAX,
    },
    ports::{
        CalendarAccessTokenProvider, CalendarDeletionScope, CalendarEventChange,
        CalendarEventWrite, CalendarEventWriteOutcome, CalendarMutationError,
        CalendarMutationProvider, CalendarMutationService, CalendarProviderError,
        CalendarProviderErrorKind, CalendarRefreshNotifier, CalendarRepository, CalendarRsvpScope,
        CalendarTokenError, CalendarUpdateScope, ProviderImportOutcome,
        ProviderInstanceUpdateOutcome, ProviderInviteOutcome, ProviderRsvpOutcome,
        ProviderSeriesMutationOutcome, RetiredCalendarEvent,
    },
};
use crate::domain::events::{CalendarEventMetadata, CalendarMacroEvent, CalendarTopicEvent};
use macro_event_broker::MacroEventBroker;

const EMPTY_PATCH: &str = "the patch changes nothing";

/// Calendar mutation use cases with provider, token, and persistence
/// details behind ports.
pub struct CalendarMutationServiceImpl<R, G, T, B, N> {
    repository: R,
    provider: G,
    tokens: T,
    macro_event_broker: B,
    refresh: N,
    outlook_writes_enabled: bool,
}

impl<R, G, T, B, N> CalendarMutationServiceImpl<R, G, T, B, N>
where
    R: CalendarRepository,
    G: CalendarMutationProvider,
    T: CalendarAccessTokenProvider,
    B: MacroEventBroker,
    N: CalendarRefreshNotifier,
{
    /// Construct the service from its ports.
    pub fn new(repository: R, provider: G, tokens: T, macro_event_broker: B, refresh: N) -> Self {
        Self {
            repository,
            provider,
            tokens,
            macro_event_broker,
            refresh,
            outlook_writes_enabled: true,
        }
    }

    /// Stop provider writes independently of sync and outcome recovery.
    pub fn with_outlook_writes_enabled(mut self, enabled: bool) -> Self {
        self.outlook_writes_enabled = enabled;
        self
    }

    fn require_provider_writes(
        &self,
        provider: super::models::CalendarProvider,
    ) -> Result<(), CalendarMutationError> {
        if provider == super::models::CalendarProvider::Outlook && !self.outlook_writes_enabled {
            return Err(CalendarMutationError::Retryable(
                "Outlook calendar changes are temporarily paused; try again later".into(),
            ));
        }
        Ok(())
    }

    /// Publish one calendar topic event; failures are logged and dropped.
    ///
    /// The provider and the local projection are already updated by this
    /// point, so a publish failure must not fail the mutation.
    fn publish_calendar_event(&self, event: CalendarTopicEvent) {
        let _ = self
            .macro_event_broker
            .send_event(&CalendarMacroEvent::for_change(event))
            .inspect_err(|error| {
                tracing::error!(error=?error, "failed to publish calendar event");
            });
    }

    /// Announce every event a source retirement touched. Retiring a source
    /// does not necessarily remove the event — the row survives, rewritten
    /// from its next-best remaining source — so each event reports its own
    /// fate.
    fn publish_retirements(&self, retired: Vec<RetiredCalendarEvent>) {
        for event in retired {
            let metadata = CalendarEventMetadata {
                event_id: event.event_id,
                owner_id: event.owner_id,
            };
            self.publish_calendar_event(if event.deleted {
                CalendarTopicEvent::Deleted(metadata)
            } else {
                CalendarTopicEvent::Updated(metadata)
            });
        }
    }

    /// Announce retired events and, when anything was actually retired, nudge
    /// the link's calendar viewers to refetch their projections.
    async fn announce_retirements(
        &self,
        owner_id: &str,
        email_link_id: Uuid,
        retired: Vec<RetiredCalendarEvent>,
    ) {
        if retired.is_empty() {
            return;
        }
        let team_ooo_changed = retired.iter().any(|event| event.team_ooo_changed);
        self.publish_retirements(retired);
        self.refresh
            .calendar_changed(owner_id, email_link_id, team_ooo_changed)
            .await;
    }

    /// Announce what a write did to the canonical row. A write that changed
    /// nothing publishes nothing, so an idempotent replay stays quiet.
    fn publish_write_outcome(&self, outcome: &CalendarEventWriteOutcome) {
        let metadata = CalendarEventMetadata {
            event_id: outcome.event_id,
            owner_id: outcome.owner_id.clone(),
        };
        match outcome.change {
            CalendarEventChange::Created => {
                self.publish_calendar_event(CalendarTopicEvent::Created(metadata));
            }
            CalendarEventChange::Updated => {
                self.publish_calendar_event(CalendarTopicEvent::Updated(metadata));
            }
            CalendarEventChange::Unchanged => {}
        }
    }

    async fn resolve_mutation_target(
        &self,
        requester_id: &str,
        event_id: Uuid,
        calendar_id: Option<Uuid>,
    ) -> Result<CalendarEventMutationTarget, CalendarMutationError> {
        self.repository
            .get_event_mutation_target(requester_id, event_id, calendar_id)
            .await
            .map_err(internal)?
            .ok_or(CalendarMutationError::NotFound)
    }

    async fn fetch_token(
        &self,
        target_identity: &super::models::CalendarLinkTokenIdentity,
    ) -> Result<String, CalendarMutationError> {
        self.tokens
            .fetch_access_token(target_identity)
            .await
            .map_err(|error| match error {
                CalendarTokenError::ReauthRequired(message) => {
                    CalendarMutationError::ReauthRequired(message)
                }
                CalendarTokenError::Transient(message) => CalendarMutationError::Retryable(message),
            })
    }

    /// Persist the provider echo and return the canonical event with its
    /// applied entity id.
    async fn persist_echo(
        &self,
        viewer: Option<&ActorInboxes>,
        upsert: CalendarEventUpsert,
    ) -> Result<CalendarEvent, CalendarMutationError> {
        let event = upsert.event.clone();
        self.persist_echo_as(viewer, upsert, event).await
    }

    /// Persist the provider echo of an occurrence-scoped write and return the
    /// event as that occurrence reads: the series master is what gets stored,
    /// but the caller changed one instance, so the exception's content,
    /// time, and attendees overlay the canonical event in the response.
    async fn persist_occurrence_echo(
        &self,
        viewer: Option<&ActorInboxes>,
        upsert: CalendarEventUpsert,
        recurrence_id: &str,
    ) -> Result<CalendarEvent, CalendarMutationError> {
        let mut event = upsert.event.clone();
        if let Some(exception) = upsert
            .overrides
            .iter()
            .find(|exception| exception.recurrence_id == recurrence_id)
        {
            exception.apply_to(&mut event);
        }
        self.persist_echo_as(viewer, upsert, event).await
    }

    async fn persist_echo_as(
        &self,
        viewer: Option<&ActorInboxes>,
        upsert: CalendarEventUpsert,
        mut event: CalendarEvent,
    ) -> Result<CalendarEvent, CalendarMutationError> {
        let source = upsert.source.details();
        let email_link_id = source.email_link_id;
        let outcome = self
            .repository
            .upsert_event(CalendarEventWrite::UserMutation(upsert))
            .await
            .map_err(|error| CalendarMutationError::PersistFailed(format!("{error:?}")))?;
        event.id = outcome.event_id;
        self.publish_write_outcome(&outcome);
        if outcome.change != CalendarEventChange::Unchanged {
            self.refresh
                .calendar_changed(&outcome.owner_id, email_link_id, outcome.team_ooo_changed)
                .await;
        }
        for copy in &outcome.aligned_copies {
            self.refresh
                .calendar_changed(&copy.owner_id, copy.email_link_id, false)
                .await;
        }
        if let Some(viewer) = viewer {
            viewer.mark_event(&mut event, email_link_id);
        }
        Ok(event)
    }
}

impl<R, G, T, B, N> super::ports::CalendarCopyConfirmation
    for CalendarMutationServiceImpl<R, G, T, B, N>
where
    R: CalendarRepository,
    G: CalendarMutationProvider,
    T: CalendarAccessTokenProvider,
    B: MacroEventBroker,
    N: CalendarRefreshNotifier,
{
    #[tracing::instrument(
        name = "calendar.confirm_copy",
        skip(self, requester_id, inbox),
        fields(event_id = %event_id, change = ?change, copies, refused),
        err
    )]
    async fn confirm_copy(
        &self,
        requester_id: &str,
        event_id: Uuid,
        inbox: Option<&str>,
        change: super::copies::CopyChange,
    ) -> Result<Option<super::copies::CopyRefusal>, CalendarMutationError> {
        let copies = self
            .repository
            .meeting_copies(requester_id, event_id)
            .await
            .map_err(internal)?;
        let refusal = super::copies::check_copy(&copies, event_id, inbox, change);
        let span = tracing::Span::current();
        span.record("copies", copies.len());
        span.record("refused", refusal.is_some());
        Ok(refusal)
    }
}

impl<R, G, T, B, N> CalendarMutationService for CalendarMutationServiceImpl<R, G, T, B, N>
where
    R: CalendarRepository,
    G: CalendarMutationProvider,
    T: CalendarAccessTokenProvider,
    B: MacroEventBroker,
    N: CalendarRefreshNotifier,
{
    #[tracing::instrument(skip(self, requester_id, draft), err)]
    async fn create_event(
        &self,
        requester_id: &str,
        email_link_id: Option<Uuid>,
        calendar_id: Option<Uuid>,
        mut draft: CalendarEventDraft,
    ) -> Result<CalendarEvent, CalendarMutationError> {
        validate_time(&draft.time)?;
        validate_attendee_emails(draft.attendees.iter().map(|attendee| &attendee.email))?;
        if let Some(reminders) = &draft.reminders {
            validate_reminders(reminders)?;
        }
        let target = self
            .repository
            .get_creation_target(requester_id, email_link_id, calendar_id)
            .await
            .map_err(internal)?
            .ok_or(CalendarMutationError::NoWritableCalendar)?;
        if target.is_read_only {
            return Err(CalendarMutationError::ReadOnly);
        }
        self.require_provider_writes(target.token_identity.provider)?;
        validate_conference(target.token_identity.provider, draft.conference)?;
        guest_policy::validate_draft_guest_policy(target.token_identity.provider, &mut draft)?;
        // An out-of-office event carries no attendees and Google auto-declines
        // on the owner's behalf, so it must not gain an organizer guest.
        if draft.out_of_office.is_some() {
            validate_out_of_office_create(&draft, &target)?;
        } else if invites_creator(&target, &draft) {
            ensure_organizer_attendee(&mut draft.attendees, &target.token_identity.email_address);
        }
        let access_token = self.fetch_token(&target.token_identity).await?;
        let upsert = self
            .provider
            .create_event(
                &access_token,
                &target.provider_target(OccurrenceRange::maintenance_horizon(Utc::now())),
                &draft,
            )
            .await
            .map_err(provider_error)?;
        self.persist_echo(target.actor.as_ref(), upsert).await
    }

    #[tracing::instrument(skip(self, requester_id, patch), err)]
    async fn update_event(
        &self,
        requester_id: &str,
        event_id: Uuid,
        calendar_id: Option<Uuid>,
        patch: CalendarEventPatch,
        scope: CalendarUpdateScope,
    ) -> Result<CalendarEvent, CalendarMutationError> {
        if patch.is_empty() {
            return Err(CalendarMutationError::InvalidInput(EMPTY_PATCH.to_string()));
        }
        if let Some(time) = &patch.time {
            validate_time(time)?;
        }
        if let Some(attendees) = &patch.attendees {
            validate_attendee_emails(attendees.iter().map(|attendee| &attendee.email))?;
        }
        if let Some(reminders) = &patch.reminders {
            validate_reminders(reminders)?;
        }
        if matches!(scope, CalendarUpdateScope::ThisEvent { .. })
            && patch.recurrence_lines.is_some()
        {
            return Err(CalendarMutationError::InvalidInput(
                "a single occurrence has no recurrence of its own; update the whole series to \
                 change the recurrence"
                    .to_string(),
            ));
        }
        let target = self
            .resolve_mutation_target(requester_id, event_id, calendar_id)
            .await?;
        match target.organizer_copy_calendar_id {
            Some(organizer_calendar_id) if patch.reaches_meeting() => {
                self.update_through_organizer_copy(
                    requester_id,
                    target,
                    organizer_calendar_id,
                    patch,
                    scope,
                )
                .await
            }
            _ => self.update_copy(requester_id, target, patch, scope).await,
        }
    }

    #[tracing::instrument(skip(self, requester_id), err)]
    async fn delete_event(
        &self,
        requester_id: &str,
        event_id: Uuid,
        calendar_id: Option<Uuid>,
        scope: CalendarDeletionScope,
    ) -> Result<(), CalendarMutationError> {
        let target = self
            .resolve_mutation_target(requester_id, event_id, calendar_id)
            .await?;
        match target.organizer_copy_calendar_id {
            Some(organizer_calendar_id) => {
                self.delete_through_organizer_copy(
                    requester_id,
                    target,
                    organizer_calendar_id,
                    scope,
                )
                .await
            }
            None => self.delete_copy(&target, &scope).await,
        }
    }

    #[tracing::instrument(skip(self, requester_id, responding_email), err)]
    async fn respond_to_event(
        &self,
        requester_id: &str,
        event_id: Uuid,
        calendar_id: Option<Uuid>,
        response: AttendeeResponseStatus,
        scope: CalendarRsvpScope,
        responding_email: Option<String>,
    ) -> Result<CalendarEvent, CalendarMutationError> {
        let target = self
            .resolve_mutation_target(requester_id, event_id, calendar_id)
            .await?;
        if target.is_read_only {
            return Err(CalendarMutationError::ReadOnly);
        }
        // The owner is listed on a snapshot only so the provider accepts it;
        // their answer would never reach the meeting.
        if target.is_snapshot {
            return Err(CalendarMutationError::Snapshot);
        }
        self.require_provider_writes(target.token_identity.provider)?;
        let Some(actor) = target.actor.as_ref() else {
            return Err(CalendarMutationError::NotAttendee);
        };
        // Without an address, answer as the copy's own inbox, as the web does by
        // sending the copy's `is_self` row. A delegate owns no copy inbox and
        // keeps answering as their own inboxes.
        let selected_actor = match responding_email.as_deref() {
            Some(email) => Some(
                actor
                    .select(email)
                    .ok_or(CalendarMutationError::NotAttendee)?,
            ),
            None => actor.through(target.email_link_id),
        };
        let access_token = self.fetch_token(&target.token_identity).await?;
        let outcome = self
            .provider
            .rsvp_event(
                &access_token,
                &target.provider_target(OccurrenceRange::maintenance_horizon(Utc::now())),
                target.master_provider_event_id(),
                selected_actor.as_ref().unwrap_or(actor),
                response,
                &scope,
            )
            .await
            .map_err(provider_error)?;
        match outcome {
            ProviderRsvpOutcome::Applied(upsert) => match &scope {
                CalendarRsvpScope::All => self.persist_echo(target.actor.as_ref(), *upsert).await,
                CalendarRsvpScope::ThisEvent { recurrence_id } => {
                    self.persist_occurrence_echo(target.actor.as_ref(), *upsert, recurrence_id)
                        .await
                }
            },
            ProviderRsvpOutcome::RespondedAndRemoved(mut event) => {
                self.retire_gone_source(&target).await;
                event.id = target.event_id;
                if let Some(actor) = &target.actor {
                    actor.mark_event(&mut event, target.email_link_id);
                }
                Ok(*event)
            }
            ProviderRsvpOutcome::NotAttendee => Err(CalendarMutationError::NotAttendee),
            ProviderRsvpOutcome::Gone => {
                self.retire_gone_source(&target).await;
                Err(CalendarMutationError::NotFound)
            }
        }
    }

    #[tracing::instrument(
        name = "calendar.invite_guests",
        skip(self, requester_id, guests, scope),
        fields(
            event_id = %event_id,
            guest_count = guests.len(),
            scope = match &scope {
                CalendarUpdateScope::All => "all",
                CalendarUpdateScope::ThisEvent { .. } => "this_event",
            },
            provider = tracing::field::Empty,
            outcome = tracing::field::Empty,
        ),
        err
    )]
    async fn invite_guests(
        &self,
        requester_id: &str,
        event_id: Uuid,
        calendar_id: Option<Uuid>,
        guests: Vec<CalendarAttendeeInput>,
        scope: CalendarUpdateScope,
    ) -> Result<CalendarEvent, CalendarMutationError> {
        let result = self
            .add_guests(requester_id, event_id, calendar_id, guests, scope)
            .await;
        tracing::Span::current().record("outcome", invite::invite_outcome(&result));
        result
    }

    #[tracing::instrument(
        name = "calendar.copy_shared_event",
        skip(self, requester_id),
        fields(
            event_id = %event_id,
            copied_event_id = tracing::field::Empty,
            outcome = tracing::field::Empty,
        ),
        err
    )]
    async fn copy_shared_event(
        &self,
        requester_id: &str,
        event_id: Uuid,
        calendar_id: Option<Uuid>,
    ) -> Result<CalendarEvent, CalendarMutationError> {
        let result = self
            .copy_into_calendar(requester_id, event_id, calendar_id)
            .await;
        let span = tracing::Span::current();
        if let Ok(copied) = &result {
            span.record("copied_event_id", tracing::field::display(copied.id));
        }
        span.record("outcome", copy::copy_outcome(&result));
        result
    }

    #[tracing::instrument(
        name = "calendar.respond_to_join_request",
        skip(self, requester_id),
        fields(request_id = %request_id, outcome = tracing::field::Empty),
        err
    )]
    async fn respond_to_join_request(
        &self,
        requester_id: &str,
        request_id: Uuid,
        accept: bool,
    ) -> Result<crate::domain::join_requests::CalendarJoinRequest, CalendarMutationError> {
        let result = self
            .answer_join_request(requester_id, request_id, accept)
            .await;
        tracing::Span::current().record("outcome", join::answer_outcome(accept, &result));
        result
    }

    #[tracing::instrument(skip(self, requester_id), err)]
    async fn list_visible_calendars(
        &self,
        requester_id: &str,
    ) -> Result<Vec<super::models::VisibleCalendar>, CalendarMutationError> {
        self.repository
            .list_visible_calendars(requester_id)
            .await
            .map_err(internal)
    }

    #[tracing::instrument(skip(self, requester_id), err)]
    async fn disconnect_calendar(
        &self,
        requester_id: &str,
        email_link_id: Uuid,
    ) -> Result<(), CalendarMutationError> {
        let disconnected = self
            .repository
            .disconnect_provider_calendar(requester_id, email_link_id)
            .await
            .map_err(internal)?
            .ok_or(CalendarMutationError::NotFound)?;
        self.release_watch_channels(email_link_id, &disconnected)
            .await;
        // The purge removed whole calendars, and no sync echo will ever
        // arrive for a disconnected link, so viewers have to be nudged here.
        self.refresh
            .calendar_changed(requester_id, email_link_id, true)
            .await;
        Ok(())
    }
}

impl<R, G, T, B, N> CalendarMutationServiceImpl<R, G, T, B, N>
where
    R: CalendarRepository,
    G: CalendarMutationProvider,
    T: CalendarAccessTokenProvider,
    B: MacroEventBroker,
    N: CalendarRefreshNotifier,
{
    /// Close the push channels a disconnected calendar left open. Best-effort:
    /// the local calendars are already gone, so a notification that still
    /// arrives resolves to no watch target and is dropped. Stopping the
    /// channels only spares Google the retries until they expire.
    async fn release_watch_channels(
        &self,
        email_link_id: Uuid,
        disconnected: &DisconnectedCalendar,
    ) {
        if disconnected.watch_channels.is_empty() {
            return;
        }
        let access_token = match self
            .tokens
            .fetch_access_token(&disconnected.token_identity)
            .await
        {
            Ok(token) => token,
            Err(error) => {
                tracing::warn!(
                    error=?error,
                    email_link_id=%email_link_id,
                    "no token to close the disconnected calendar's push channels"
                );
                return;
            }
        };
        for channel in &disconnected.watch_channels {
            self.provider
                .stop_watch_channel(
                    &access_token,
                    email_link_id,
                    &channel.channel_id,
                    &channel.resource_id,
                )
                .await
                .inspect_err(|error| {
                    tracing::warn!(
                        error=?error,
                        email_link_id=%email_link_id,
                        channel_id=%channel.channel_id,
                        "failed to close a disconnected calendar's push channel"
                    );
                })
                .ok();
        }
    }

    /// Best-effort cleanup when the provider reports the event gone: the
    /// regular sync converges the projection either way.
    async fn retire_gone_source(&self, target: &CalendarEventMutationTarget) {
        let retired = self
            .repository
            .remove_provider_source(
                target.account_id,
                target.calendar_id,
                target.master_provider_event_id(),
            )
            .await
            .inspect_err(|error| {
                tracing::warn!(
                    error=?error,
                    event_id=%target.event_id,
                    "failed to retire a provider-deleted calendar event source"
                );
            })
            .unwrap_or_default();
        // The row may be gone now, so search cannot rediscover this by
        // re-reading Postgres — the retirement has to be announced.
        self.announce_retirements(&target.owner_id, target.email_link_id, retired)
            .await;
    }
}

fn validate_time(time: &EventTime) -> Result<(), CalendarMutationError> {
    if !time.has_positive_duration() {
        return Err(CalendarMutationError::InvalidInput(
            "event end must be after its start".to_string(),
        ));
    }
    Ok(())
}

/// Enforce Google's rules for creating an out-of-office event so the provider
/// never rejects a write we already accepted: primary calendar only, a timed
/// span rather than whole days, and no attendees, conference, description, or
/// guest policy beyond Google's defaults.
fn validate_out_of_office_create(
    draft: &CalendarEventDraft,
    target: &CalendarCreationTarget,
) -> Result<(), CalendarMutationError> {
    if !target.is_primary {
        return Err(CalendarMutationError::InvalidInput(
            "out-of-office events can only be created on your primary calendar".to_string(),
        ));
    }
    if !matches!(draft.time, EventTime::Timed { .. }) {
        return Err(CalendarMutationError::InvalidInput(
            "out-of-office events must have specific start and end times, not span whole days"
                .to_string(),
        ));
    }
    if !draft.attendees.is_empty() {
        return Err(CalendarMutationError::InvalidInput(
            "out-of-office events cannot have attendees".to_string(),
        ));
    }
    if draft.conference.is_some() {
        return Err(CalendarMutationError::InvalidInput(
            "out-of-office events cannot have a video conference".to_string(),
        ));
    }
    if draft.description.as_deref().is_some_and(|d| !d.is_empty()) {
        return Err(CalendarMutationError::InvalidInput(
            "out-of-office events cannot have a description".to_string(),
        ));
    }
    if draft.guest_policy.is_some() {
        return Err(CalendarMutationError::InvalidInput(
            "out-of-office events have no guests to set permissions for".to_string(),
        ));
    }
    Ok(())
}

/// Enforce Google's own reminder limits so the provider never rejects a
/// write we already accepted: at most five overrides, offsets within four
/// weeks, and only methods Google understands.
fn validate_reminders(reminders: &EventReminders) -> Result<(), CalendarMutationError> {
    if reminders.use_default && !reminders.overrides.is_empty() {
        return Err(CalendarMutationError::InvalidInput(
            "reminder overrides require useDefault to be off".to_string(),
        ));
    }
    if reminders.overrides.len() > REMINDER_OVERRIDES_MAX {
        return Err(CalendarMutationError::InvalidInput(format!(
            "an event allows at most {REMINDER_OVERRIDES_MAX} reminders"
        )));
    }
    for reminder in &reminders.overrides {
        if reminder.method != REMINDER_METHOD_POPUP && reminder.method != REMINDER_METHOD_EMAIL {
            return Err(CalendarMutationError::InvalidInput(format!(
                "unsupported reminder method: {:?}",
                reminder.method
            )));
        }
        if reminder.minutes > REMINDER_MINUTES_MAX {
            return Err(CalendarMutationError::InvalidInput(format!(
                "a reminder can fire at most {REMINDER_MINUTES_MAX} minutes before the event"
            )));
        }
    }
    Ok(())
}

/// Whether a new event lists the inbox it is written through as an accepted
/// guest. On its primary calendar the inbox is the organizer, which every
/// guest should see. Google names any other calendar as the organizer of the
/// events made on it, and lists the inbox only as a guest, which invites the
/// inbox's primary calendar too: as in Google's own client, it joins only a
/// meeting with other guests, and an event of its own stays on the calendar
/// it was made on.
fn invites_creator(target: &CalendarCreationTarget, draft: &CalendarEventDraft) -> bool {
    target.is_primary
        || target.token_identity.provider != super::models::CalendarProvider::Google
        || draft.attendees.iter().any(|attendee| {
            !attendee
                .email
                .eq_ignore_ascii_case(&target.token_identity.email_address)
        })
}

fn ensure_organizer_attendee(attendees: &mut Vec<CalendarAttendeeInput>, organizer_email: &str) {
    let mut kept = false;
    attendees.retain_mut(|attendee| {
        if !attendee.email.eq_ignore_ascii_case(organizer_email) {
            return true;
        }
        if kept {
            return false;
        }
        attendee.response_status = Some(AttendeeResponseStatus::Accepted);
        kept = true;
        true
    });
    if kept {
        return;
    }
    attendees.insert(
        0,
        CalendarAttendeeInput {
            email: organizer_email.to_string(),
            is_optional: false,
            response_status: Some(AttendeeResponseStatus::Accepted),
        },
    );
}

/// Carries each retained attendee's stored RSVP and optional flag forward
/// into a replacement attendee list. A patch replaces the whole list, and
/// Google reads an attendee whose `responseStatus` is omitted as
/// `needs_action` — so without this, adding or dropping one guest would reset
/// everyone else's RSVP and clear their optional flag. The caller's own values
/// still win when supplied (a set `response_status`, an explicit `optional`).
///
/// `stored` is matched by email, later entries winning — so an occurrence's
/// override attendees, appended after the series attendees, take precedence.
fn preserve_retained_attendee_state(
    attendees: &mut [CalendarAttendeeInput],
    stored: &[CalendarAttendee],
) {
    let mut by_email: HashMap<String, &CalendarAttendee> = HashMap::new();
    for attendee in stored {
        by_email.insert(attendee.email.to_lowercase(), attendee);
    }
    for attendee in attendees.iter_mut() {
        let Some(existing) = by_email.get(&attendee.email.to_lowercase()) else {
            continue;
        };
        if attendee.response_status.is_none() {
            attendee.response_status = Some(existing.response_status);
        }
        if !attendee.is_optional {
            attendee.is_optional = existing.is_optional;
        }
    }
}

fn validate_attendee_emails<'a>(
    emails: impl Iterator<Item = &'a String>,
) -> Result<(), CalendarMutationError> {
    for email in emails {
        let trimmed = email.trim();
        if trimmed.is_empty() || !trimmed.contains('@') {
            return Err(CalendarMutationError::InvalidInput(format!(
                "invalid attendee email: {email:?}"
            )));
        }
    }
    Ok(())
}

fn provider_error(error: CalendarProviderError) -> CalendarMutationError {
    match error.kind() {
        CalendarProviderErrorKind::ReauthRequired => {
            CalendarMutationError::ReauthRequired(error.to_string())
        }
        CalendarProviderErrorKind::Transient | CalendarProviderErrorKind::SyncTokenExpired => {
            CalendarMutationError::Retryable(error.to_string())
        }
        CalendarProviderErrorKind::Permanent | CalendarProviderErrorKind::PushUnsupported => {
            CalendarMutationError::ProviderRejected(error.to_string())
        }
    }
}

fn internal(error: rootcause::Report) -> CalendarMutationError {
    CalendarMutationError::Retryable(format!("{error:?}"))
}

#[cfg(test)]
mod test;

impl<R, G, T, B, N> super::ports::CalendarCreationRecoveryService
    for CalendarMutationServiceImpl<R, G, T, B, N>
where
    R: CalendarRepository,
    G: CalendarMutationProvider,
    T: CalendarAccessTokenProvider,
    B: MacroEventBroker,
    N: CalendarRefreshNotifier,
{
    #[tracing::instrument(skip(self, requester_id), err)]
    async fn delete_created_event(
        &self,
        requester_id: &str,
        calendar_id: Uuid,
        creation_key: Uuid,
    ) -> Result<(), CalendarMutationError> {
        // Resolve current access rather than interpreting an inaccessible canonical event as gone.
        let target = self
            .repository
            .get_creation_target(requester_id, None, Some(calendar_id))
            .await
            .map_err(internal)?
            .ok_or(CalendarMutationError::NoWritableCalendar)?;
        if target.is_read_only {
            return Err(CalendarMutationError::ReadOnly);
        }
        self.require_provider_writes(target.token_identity.provider)?;
        let access_token = self.fetch_token(&target.token_identity).await?;
        let ids = self
            .provider
            .delete_created_event(
                &access_token,
                &target.provider_target(OccurrenceRange::maintenance_horizon(Utc::now())),
                creation_key,
            )
            .await
            .map_err(provider_error)?;
        for provider_id in ids {
            let retired = self
                .repository
                .remove_provider_source(target.account_id, target.calendar_id, &provider_id)
                .await
                .map_err(|error| CalendarMutationError::PersistFailed(format!("{error:?}")))?;
            self.announce_retirements(&target.owner_id, target.email_link_id, retired)
                .await;
        }
        Ok(())
    }
}

fn validate_conference(
    provider: super::models::CalendarProvider,
    conference: Option<super::models::ConferenceChange>,
) -> Result<(), CalendarMutationError> {
    use super::models::{CalendarProvider, ConferenceChange};
    if matches!(
        (provider, conference),
        (
            CalendarProvider::Google,
            Some(ConferenceChange::MicrosoftTeams)
        ) | (
            CalendarProvider::Outlook,
            Some(ConferenceChange::GoogleMeet)
        )
    ) {
        return Err(CalendarMutationError::InvalidInput(
            "Choose the meeting provider supported by this calendar".into(),
        ));
    }
    Ok(())
}