/**
 * Activity API Client
 * Handles activity-log-related API requests
 */

import { apiGet, type ApiResponse } from "./client";

/**
 * Activity action enum (matches backend ActivityAction)
 */
export enum ActivityAction {
  CREATED = "CREATED",
  UPDATED = "UPDATED",
  DELETED = "DELETED",
  COMPLETED = "COMPLETED",
  ASSIGNED = "ASSIGNED",
}

/**
 * Entity type enum (matches backend EntityType)
 */
export enum EntityType {
  TASK = "TASK",
  EVENT = "EVENT",
  PROJECT = "PROJECT",
  WORKSPACE = "WORKSPACE",
  USER = "USER",
  DOMAIN = "DOMAIN",
  IDEA = "IDEA",
}

/**
 * Activity log response interface (matches Prisma ActivityLog model)
 */
export interface ActivityLog {
  id: string;
  workspaceId: string;
  userId: string;
  action: ActivityAction;
  entityType: EntityType;
  entityId: string;
  details: Record<string, unknown> | null;
  ipAddress: string | null;
  userAgent: string | null;
  createdAt: string;
  user?: {
    id: string;
    name: string | null;
    email: string;
  };
}

/**
 * Filters for querying activity logs
 */
export interface ActivityLogFilters {
  workspaceId?: string;
  userId?: string;
  action?: ActivityAction;
  entityType?: EntityType;
  entityId?: string;
  startDate?: string;
  endDate?: string;
  page?: number;
  limit?: number;
}

/**
 * Paginated activity logs response
 */
export interface PaginatedActivityLogs {
  data: ActivityLog[];
  meta: {
    total: number;
    page: number;
    limit: number;
    totalPages: number;
  };
}

/**
 * Fetch activity logs with optional filters
 */
export async function fetchActivityLogs(filters?: ActivityLogFilters): Promise<ActivityLog[]> {
  const params = new URLSearchParams();

  if (filters?.userId) {
    params.append("userId", filters.userId);
  }
  if (filters?.action) {
    params.append("action", filters.action);
  }
  if (filters?.entityType) {
    params.append("entityType", filters.entityType);
  }
  if (filters?.entityId) {
    params.append("entityId", filters.entityId);
  }
  if (filters?.startDate) {
    params.append("startDate", filters.startDate);
  }
  if (filters?.endDate) {
    params.append("endDate", filters.endDate);
  }
  if (filters?.page !== undefined) {
    params.append("page", String(filters.page));
  }
  if (filters?.limit !== undefined) {
    params.append("limit", String(filters.limit));
  }

  const queryString = params.toString();
  const endpoint = queryString ? `/api/activity?${queryString}` : "/api/activity";

  const response = await apiGet<PaginatedActivityLogs>(endpoint, filters?.workspaceId);
  return response.data;
}

/**
 * Fetch a single activity log by ID
 */
export async function fetchActivityLog(id: string, workspaceId?: string): Promise<ActivityLog> {
  return apiGet<ActivityLog>(`/api/activity/${id}`, workspaceId);
}

/**
 * Fetch audit trail for a specific entity
 */
export async function fetchAuditTrail(
  entityType: EntityType,
  entityId: string,
  workspaceId?: string
): Promise<ActivityLog[]> {
  const response = await apiGet<ApiResponse<ActivityLog[]>>(
    `/api/activity/audit/${entityType}/${entityId}`,
    workspaceId
  );
  return response.data;
}
