// Enums
export enum OperationType {
  CREATE = "CREATE",
  UPDATE = "UPDATE",
  DELETE = "DELETE",
  READ = "READ",
  LOGIN = "LOGIN",
  LOGOUT = "LOGOUT",
  APPROVE = "APPROVE",
  REJECT = "REJECT",
  ACTIVATE = "ACTIVATE",
  DEACTIVATE = "DEACTIVATE",
  EXPORT = "EXPORT",
  IMPORT = "IMPORT",
  BULK_UPDATE = "BULK_UPDATE",
  BULK_DELETE = "BULK_DELETE",
}

export enum UserType {
  SYSTEM = "SYSTEM",
  ADMIN = "ADMIN",
  USER = "USER",
}

export enum HttpMethod {
  GET = "GET",
  POST = "POST",
  PUT = "PUT",
  PATCH = "PATCH",
  DELETE = "DELETE",
}

// Main interfaces
export interface AccessLog {
  id: string;
  operationType: OperationType;
  userType: UserType;
  entityName: string;
  entityId?: string;
  actionDescription?: string;
  oldValues?: string; // JSON string
  newValues?: string; // JSON string
  ipAddress?: string;
  userAgent?: string;
  endpoint?: string;
  httpMethod?: HttpMethod;
  statusCode?: number;
  errorMessage?: string;
  metadata?: Record<string, any>; // JSON object
  userId?: string;
  sessionId?: string;
  requestId?: string;
  executionTime?: number; // in milliseconds
  createdAt: string; // ISO date string
  updatedAt: string; // ISO date string
  user?: {
    id: string;
    firstName: string;
    lastName: string;
    email: string;
    phone: string;
  };
}

// Query interfaces
export interface AccessLogQuery {
  // Pagination
  page?: number;
  limit?: number;
  sortBy?: string;
  sortOrder?: "ASC" | "DESC";

  // Filters
  operationType?: OperationType;
  userType?: UserType;
  entityName?: string;
  entityId?: string;
  userId?: string;
  ipAddress?: string;
  endpoint?: string;
  httpMethod?: HttpMethod;
  statusCode?: number;

  // Date range
  startDate?: string; // ISO date string
  endDate?: string; // ISO date string

  // Search
  search?: string; // Searches in actionDescription and entityName

  // Performance
  minExecutionTime?: number; // milliseconds
  maxExecutionTime?: number; // milliseconds
}

export interface AccessLogStatsQueryDto {
  startDate?: string;
  endDate?: string;
  page?: number;
  limit?: number;
}

export interface EntityIdQueryDto {
  entityId?: string;
  page?: number;
  limit?: number;
}

// Response interfaces
export interface AccessLogsResponse {
  accessLogs: AccessLog[];
  count: number;
  paginate: boolean;
  data?: {
    accessLogs: AccessLog[];
    pager: {
      page: number;
      limit: number;
      totalItems: number;
      totalPages: number;
      prevPage: boolean;
      nextPage: boolean;
    };
  };
}

export interface OperationStatsResponse {
  operationStats: Array<{
    operationType: OperationType;
    userType: UserType;
    entityName: string;
    count: number;
  }>;
  count: number;
  paginate: boolean;
}

export interface TopUsersResponse {
  topUsers: Array<{
    userId: string;
    firstName: string;
    lastName: string;
    email: string;
    activityCount: number;
  }>;
  count: number;
  paginate: boolean;
}

// UI State interfaces
export interface AccessLogsState {
  logs: AccessLog[];
  stats: OperationStatsResponse | null;
  topUsers: TopUsersResponse | null;
  errorLogs: AccessLog[];
  currentLog: AccessLog | null;
  loading: boolean;
  error: string | null;
  filters: AccessLogQuery;
  pagination: {
    page: number;
    limit: number;
    total: number;
  };
}

// Persian translations
export const OperationTypeLabels: Record<OperationType, string> = {
  [OperationType.CREATE]: "ایجاد",
  [OperationType.UPDATE]: "به‌روزرسانی",
  [OperationType.DELETE]: "حذف",
  [OperationType.READ]: "مشاهده",
  [OperationType.LOGIN]: "ورود",
  [OperationType.LOGOUT]: "خروج",
  [OperationType.APPROVE]: "تایید",
  [OperationType.REJECT]: "رد",
  [OperationType.ACTIVATE]: "فعال‌سازی",
  [OperationType.DEACTIVATE]: "غیرفعال‌سازی",
  [OperationType.EXPORT]: "خروجی",
  [OperationType.IMPORT]: "ورودی",
  [OperationType.BULK_UPDATE]: "به‌روزرسانی گروهی",
  [OperationType.BULK_DELETE]: "حذف گروهی",
};

export const UserTypeLabels: Record<UserType, string> = {
  [UserType.SYSTEM]: "سیستم",
  [UserType.ADMIN]: "مدیر",
  [UserType.USER]: "کاربر",
};

export const HttpMethodLabels: Record<HttpMethod, string> = {
  [HttpMethod.GET]: "GET",
  [HttpMethod.POST]: "POST",
  [HttpMethod.PUT]: "PUT",
  [HttpMethod.PATCH]: "PATCH",
  [HttpMethod.DELETE]: "DELETE",
};
