import type { McpServer, McpInstance, AuditLog, AuditEvent, Secret, InstanceStatus } from '@prisma/client';
import type { CreateMcpServerInput, UpdateMcpServerInput } from '../validation/mcp-server.schema.js';
import type { CreateSecretInput, UpdateSecretInput } from '../validation/secret.schema.js';

export interface IMcpServerRepository {
  findAll(): Promise<McpServer[]>;
  findById(id: string): Promise<McpServer | null>;
  findByName(name: string): Promise<McpServer | null>;
  create(data: CreateMcpServerInput): Promise<McpServer>;
  update(id: string, data: UpdateMcpServerInput): Promise<McpServer>;
  delete(id: string): Promise<void>;
}

export interface IMcpInstanceRepository {
  findAll(serverId?: string): Promise<McpInstance[]>;
  findById(id: string): Promise<McpInstance | null>;
  findByContainerId(containerId: string): Promise<McpInstance | null>;
  create(data: { serverId: string; containerId?: string; status?: InstanceStatus; port?: number; metadata?: Record<string, unknown> }): Promise<McpInstance>;
  updateStatus(id: string, status: InstanceStatus, fields?: { containerId?: string; port?: number; metadata?: Record<string, unknown>; healthStatus?: string; lastHealthCheck?: Date; events?: unknown[] }): Promise<McpInstance>;
  delete(id: string): Promise<void>;
}

export interface ISecretRepository {
  findAll(): Promise<Secret[]>;
  findById(id: string): Promise<Secret | null>;
  findByName(name: string): Promise<Secret | null>;
  create(data: CreateSecretInput): Promise<Secret>;
  update(id: string, data: UpdateSecretInput): Promise<Secret>;
  delete(id: string): Promise<void>;
}

export interface AuditLogFilter {
  userId?: string;
  action?: string;
  resource?: string;
  resourceId?: string;
  since?: Date;
  until?: Date;
  limit?: number;
  offset?: number;
}

export interface IAuditLogRepository {
  findAll(filter?: AuditLogFilter): Promise<AuditLog[]>;
  findById(id: string): Promise<AuditLog | null>;
  create(data: { userId: string; action: string; resource: string; resourceId?: string; details?: Record<string, unknown> }): Promise<AuditLog>;
  count(filter?: AuditLogFilter): Promise<number>;
  deleteOlderThan(date: Date): Promise<number>;
}

// ── Audit Events (pipeline/gate traces from mcplocal) ──

export interface AuditEventFilter {
  sessionId?: string;
  projectName?: string;
  eventKind?: string;
  serverName?: string;
  correlationId?: string;
  userName?: string;
  from?: Date;
  to?: Date;
  limit?: number;
  offset?: number;
}

export interface AuditEventCreateInput {
  timestamp: string;
  sessionId: string;
  projectName: string;
  eventKind: string;
  source: string;
  verified: boolean;
  serverName?: string;
  correlationId?: string;
  parentEventId?: string;
  userName?: string;
  payload: Record<string, unknown>;
}

export interface AuditSessionSummary {
  sessionId: string;
  projectName: string;
  userName: string | null;
  firstSeen: Date;
  lastSeen: Date;
  eventCount: number;
  eventKinds: string[];
}

export interface IAuditEventRepository {
  findAll(filter?: AuditEventFilter): Promise<AuditEvent[]>;
  findById(id: string): Promise<AuditEvent | null>;
  createMany(events: AuditEventCreateInput[]): Promise<number>;
  count(filter?: AuditEventFilter): Promise<number>;
  listSessions(filter?: { projectName?: string; userName?: string; from?: Date; to?: Date; limit?: number; offset?: number }): Promise<AuditSessionSummary[]>;
  countSessions(filter?: { projectName?: string; userName?: string; from?: Date; to?: Date }): Promise<number>;
}
