import type {
  AutomationActionType,
  AutomationRuleConditions,
  AutomationScheduleFrequency,
  AutomationTriggerMode,
  Priority,
  WebhookEventType,
} from "@/types";

export interface AutomationTemplateStep {
  actionType: AutomationActionType;
  taskTitleTemplate?: string;
  taskDescriptionTemplate?: string;
  assigneeId?: string;
  reviewerId?: string;
  noteTitleTemplate?: string;
  noteContentTemplate?: string;
  noteFolderId?: string;
  emailToTemplate?: string;
  emailSubjectTemplate?: string;
  emailBodyTemplate?: string;
  promptTemplate?: string;
  taskPriority?: Priority;
}

export interface AutomationTemplateDefinition {
  id: string;
  name: string;
  description: string;
  category: "Execution" | "Review" | "AI" | "Planning";
  triggerMode: AutomationTriggerMode;
  triggerEvent?: WebhookEventType;
  scheduleFrequency?: AutomationScheduleFrequency;
  scheduleTime?: string;
  scheduleDayOfWeek?: number;
  conditions: AutomationRuleConditions;
  steps: AutomationTemplateStep[];
  requiredIntegrations: Array<"slack" | "telegram" | "google_sheets" | "notion" | "gmail">;
}

export const AUTOMATION_TEMPLATES: AutomationTemplateDefinition[] = [
  {
    id: "done_to_telegram_and_ai_note",
    name: "Done task -> Telegram + AI note",
    description:
      "Notify one Telegram chat when work is completed and keep a lightweight AI-generated trail.",
    category: "Execution",
    triggerMode: "event",
    triggerEvent: "task.updated",
    conditions: {
      task_status_changed_to: "done",
    },
    steps: [
      {
        actionType: "send_telegram",
      },
      {
        actionType: "ai_create_note",
        noteTitleTemplate: "Completion summary: {{task.title}}",
        promptTemplate:
          "Write a short completion note for {{task.title}} with what likely got finished, any follow-up risk, and one suggested next step.",
      },
    ],
    requiredIntegrations: ["telegram"],
  },
  {
    id: "at_risk_project_response",
    name: "At-risk project -> Slack + follow-up",
    description:
      "Escalate unhealthy projects into Slack and create a concrete recovery task in the same workspace.",
    category: "Review",
    triggerMode: "event",
    triggerEvent: "project.updated",
    conditions: {
      project_health_is: "at_risk",
    },
    steps: [
      {
        actionType: "send_slack",
      },
      {
        actionType: "create_task",
        taskTitleTemplate: "Recovery plan: {{project.name}}",
        taskDescriptionTemplate:
          "Project health changed to at risk. Capture blockers, owner, and first recovery move for {{project.name}}.",
        taskPriority: "high",
      },
    ],
    requiredIntegrations: ["slack"],
  },
  {
    id: "meeting_note_to_action_note",
    name: "Meeting note -> AI action note",
    description:
      "Turn fresh meeting notes into a structured AI follow-up with decisions, risks, and next actions.",
    category: "AI",
    triggerMode: "event",
    triggerEvent: "note.created",
    conditions: {
      title_contains: "meeting",
    },
    steps: [
      {
        actionType: "ai_create_note",
        noteTitleTemplate: "AI actions: {{note.title}}",
        promptTemplate:
          "Summarize {{note.title}} into decisions, open questions, blockers, and the next practical action items for the team.",
      },
    ],
    requiredIntegrations: [],
  },
  {
    id: "weekly_planning_note",
    name: "Weekly planning brief",
    description:
      "Generate a planning note every Monday morning to align the week before work starts.",
    category: "Planning",
    triggerMode: "schedule",
    scheduleFrequency: "weekly",
    scheduleDayOfWeek: 1,
    scheduleTime: "09:00",
    conditions: {},
    steps: [
      {
        actionType: "ai_create_note",
        noteTitleTemplate: "Weekly plan",
        promptTemplate:
          "Create a concise weekly planning brief for this workspace with priorities, likely risks, and the best first moves.",
      },
    ],
    requiredIntegrations: [],
  },
  {
    id: "daily_wrap_up_note",
    name: "Daily wrap-up note",
    description:
      "Create a short end-of-day note with progress, unfinished work, and what tomorrow should start with.",
    category: "Planning",
    triggerMode: "schedule",
    scheduleFrequency: "daily",
    scheduleTime: "18:00",
    conditions: {},
    steps: [
      {
        actionType: "ai_create_note",
        noteTitleTemplate: "Daily wrap-up",
        promptTemplate:
          "Write a compact daily wrap-up for this workspace with wins, unfinished items, and the best starting point for tomorrow.",
      },
    ],
    requiredIntegrations: [],
  },
  {
    id: "high_priority_to_notion",
    name: "High priority task -> Notion log",
    description:
      "Mirror urgent incoming work into Notion so it lands in a central operating log or dashboard.",
    category: "Execution",
    triggerMode: "event",
    triggerEvent: "task.created",
    conditions: {
      task_priority_is: "high",
    },
    steps: [
      {
        actionType: "send_notion",
      },
    ],
    requiredIntegrations: ["notion"],
  },
  {
    id: "done_task_to_sheets_log",
    name: "Done task -> Sheets log",
    description:
      "Append completed work to Google Sheets for reporting, exports, or lightweight external dashboards.",
    category: "Execution",
    triggerMode: "event",
    triggerEvent: "task.updated",
    conditions: {
      task_status_changed_to: "done",
    },
    steps: [
      {
        actionType: "send_google_sheets",
      },
    ],
    requiredIntegrations: ["google_sheets"],
  },
  {
    id: "review_requested_notion_and_task",
    name: "Review handoff -> Notion + follow-up",
    description:
      "When a reviewed task moves forward, log it in Notion and create a follow-up task to keep momentum.",
    category: "Review",
    triggerMode: "event",
    triggerEvent: "task.updated",
    conditions: {
      task_status_changed_to: "in_progress",
    },
    steps: [
      {
        actionType: "send_notion",
      },
      {
        actionType: "create_task",
        taskTitleTemplate: "Check review lane: {{task.title}}",
        taskDescriptionTemplate:
          "Review-related follow-up generated from {{task.title}}. Confirm reviewer, owner, and next milestone.",
        taskPriority: "medium",
      },
    ],
    requiredIntegrations: ["notion"],
  },
  {
    id: "overdue_task_to_gmail_draft",
    name: "Overdue task -> Gmail draft",
    description:
      "Prepare a follow-up draft email whenever a tracked task becomes overdue, so you can send updates quickly.",
    category: "Execution",
    triggerMode: "event",
    triggerEvent: "task.updated",
    conditions: {
      task_deadline_state: "overdue",
    },
    steps: [
      {
        actionType: "create_gmail_draft",
        emailSubjectTemplate: "Follow-up needed: {{task.title}}",
        emailBodyTemplate:
          "This is a draft follow-up for {{task.title}}. The task is overdue and may need a status update, blocker note, or new deadline.",
      },
    ],
    requiredIntegrations: ["gmail"],
  },
];
