import apiClient from '../../../shared/services/apiClient';

export interface AdminSession {
  id: number;
  ip_address?: string;
  user_agent?: string;
  country?: string;
  city?: string;
  last_activity: string;
  step_up_authenticated: boolean;
  step_up_expires_at?: string;
  created_at: string;
  expires_at: string;
}

export interface AdminActivityLog {
  id: number;
  user_id: number;
  activity_type: string;
  activity_description: string;
  ip_address?: string;
  country?: string;
  city?: string;
  risk_level: 'low' | 'medium' | 'high' | 'critical';
  is_unusual: boolean;
  metadata?: Record<string, unknown>;
  created_at: string;
}

class AdminSecurityService {
  async verifyStepUp(data: {
    mfa_token?: string;
    password?: string;
    session_token?: string;
  }): Promise<{ status: string; data: { step_up_completed: boolean } }> {
    const response = await apiClient.post('/admin/security/step-up/verify', data);
    return response.data;
  }

  async getSessions(): Promise<{ status: string; data: { sessions: AdminSession[] } }> {
    const response = await apiClient.get('/admin/security/sessions');
    return response.data;
  }

  async revokeSession(sessionId: number): Promise<{ status: string; message: string }> {
    const response = await apiClient.post(`/admin/security/sessions/${sessionId}/revoke`);
    return response.data;
  }

  async revokeAllSessions(): Promise<{ status: string; data: { revoked_count: number } }> {
    const response = await apiClient.post('/admin/security/sessions/revoke-all');
    return response.data;
  }

  async getActivityLogs(params?: {
    page?: number;
    limit?: number;
    risk_level?: string;
    is_unusual?: boolean;
  }): Promise<{ status: string; data: { logs: AdminActivityLog[]; pagination: { total: number; page: number; limit: number; totalPages: number } } }> {
    const response = await apiClient.get('/admin/security/activity-logs', { params });
    return response.data;
  }
}

const adminSecurityService = new AdminSecurityService();
export default adminSecurityService;

