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

export interface AccountantSession {
  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 AccountantActivityLog {
  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;
}

export interface MFAStatus {
  requires_mfa: boolean;
  mfa_enabled: boolean;
  is_enforced: boolean;
  enforcement_reason?: string;
  backup_codes_count: number;
}

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

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

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

  async revokeAllSessions(): Promise<{ status: string; data: { revoked_count: number } }> {
    const response = await apiClient.post('/accountant/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: AccountantActivityLog[]; pagination: { total: number; page: number; limit: number; totalPages: number } } }> {
    const response = await apiClient.get('/accountant/security/activity-logs', { params });
    return response.data;
  }

  async getMFAStatus(): Promise<{ status: string; data: MFAStatus }> {
    const response = await apiClient.get('/accountant/security/mfa-status');
    return response.data;
  }
}

const accountantSecurityService = new AccountantSecurityService();
export default accountantSecurityService;

