import type { z } from 'zod'
import type { CreateTaskParams, Task } from '../../../../commands/queue/types'
import type { CreateScheduleInput, TaskSchedule } from '../../../scheduler/types'
import type { CreateNotificationParams, Notification } from '../../../../operations/notifications/types'

/**
 * Command Queue Service interface for platform tools.
 * Matches the structure of CommandQueueService in apps/api.
 */
export interface ICommandQueueService {
  createTask(params: CreateTaskParams): Promise<Task>

  /**
   * Delete pending HITL tasks matching metadata filter
   * Uses JSONB containment (.contains()) for metadata-based batch deletion
   */
  deleteByMetadata(
    organizationId: string,
    metadata: Record<string, unknown>,
    status?: string
  ): Promise<{ deleted: number }>
}

/**
 * Task Scheduler Service interface for platform tools.
 * Matches the structure of TaskScheduleService in apps/api.
 */
export interface ITaskSchedulerService {
  createSchedule(input: CreateScheduleInput): Promise<TaskSchedule>
  deleteScheduleByIdempotencyKey(idempotencyKey: string, organizationId: string): Promise<void>

  /**
   * List schedules with optional metadata filtering
   * Supports JSONB containment for metadata-based queries
   */
  listSchedules(params: {
    organizationId: string
    targetResourceId?: string
    status?: string
    metadata?: Record<string, unknown>
    limit?: number
    offset?: number
  }): Promise<{ schedules: TaskSchedule[]; total: number }>

  /**
   * Get a single schedule by ID
   */
  getSchedule(id: string, organizationId: string): Promise<TaskSchedule | null>

  /**
   * Cancel a schedule (soft delete - sets status to 'cancelled')
   */
  cancelSchedule(id: string, organizationId: string): Promise<TaskSchedule>

  /**
   * Cancel all schedules matching metadata filter (batch soft delete)
   * Uses JSONB containment (.contains()) for metadata-based filtering
   */
  cancelSchedulesByMetadata(
    organizationId: string,
    metadata: Record<string, unknown>,
    status?: string
  ): Promise<{ cancelled: number }>

  /**
   * Cancel a schedule by idempotency key (soft delete)
   * Finds schedule by key and cancels it. No-op if schedule not found.
   * @see TaskScheduleService.cancelScheduleByIdempotencyKey (apps/api/src/execution/task-scheduler/service.ts)
   */
  cancelScheduleByIdempotencyKey(idempotencyKey: string, organizationId: string): Promise<{ cancelled: boolean }>
}

/**
 * Notifications Service interface for platform tools.
 * Matches the structure of NotificationService in apps/api.
 */
export interface INotificationsService {
  create(params: CreateNotificationParams): Promise<Notification>
}

/**
 * Configuration for creating an approval tool.
 * Approval tools create tasks in the command queue that require human approval.
 */
export interface ApprovalToolConfig {
  /** Unique name for the tool */
  name: string
  /** Description of what the tool does */
  description: string
  /** Zod schema for validating input */
  inputSchema: z.ZodSchema
  /** Zod schema for validating output */
  outputSchema: z.ZodSchema
  /** Resource ID to execute when task is approved */
  approveTargetResourceId: string
  /** Optional resource ID to execute when task is rejected */
  rejectTargetResourceId?: string
  /** Task priority (1-10, default: 5) */
  priority?: number
  /** Optional expiration date for the task */
  expiresAt?: Date
}

/**
 * Configuration for creating a scheduler tool.
 * Scheduler tools create scheduled tasks that execute at a specific time.
 */
export interface SchedulerToolConfig {
  /** Unique name for the tool */
  name: string
  /** Description of what the tool does (shown to LLM) */
  description: string
  /** Zod schema for validating input */
  inputSchema: z.ZodSchema
  /** Zod schema for validating output */
  outputSchema: z.ZodSchema
  /** Resource ID to execute */
  targetResourceId: string
  /** Type of resource to execute */
  targetResourceType: 'agent' | 'workflow'
  /** Maximum number of retry attempts (default: 3) */
  maxRetries?: number
  /** When to run the scheduled task */
  runAt: Date
  /** Optional task description (for internal tracking) */
  taskDescription?: string
  /** Optional idempotency key */
  idempotencyKey?: string
}

/**
 * Configuration for creating a notification tool.
 * Notification tools send platform notifications to users.
 */
export interface NotificationToolConfig {
  /** Unique name for the tool */
  name: string
  /** Description of what the tool does (shown to LLM) */
  description: string
  /** Zod schema for validating input */
  inputSchema: z.ZodSchema
  /** Zod schema for validating output */
  outputSchema: z.ZodSchema
  /** User ID to send notification to (if static, otherwise from input) */
  userId?: string
  /** Notification category (if static, otherwise from input) */
  category?: CreateNotificationParams['category']
}
