import { requireCapability, type MarketingAdapter } from './index.js'

export type Automation = {
  id: string
  name: string
}

export type AutomationCapable = {
  createAutomation(def: { name: string; listId: string }): Promise<Automation>
}

export function asAutomationCapable(adapter: MarketingAdapter): AutomationCapable {
  return adapter as MarketingAdapter & AutomationCapable
}

export async function createAutomation(
  adapter: MarketingAdapter,
  def: { name: string; listId: string },
): Promise<Automation> {
  requireCapability(adapter, 'automation')
  const capable = asAutomationCapable(adapter)
  const created = await capable.createAutomation(def)
  return { id: String(created.id), name: created.name }
}
