import axios from 'axios';

const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3020';

export interface SessionInfo {
  id: string;
  deviceFingerprint: string;
  platform?: string;
  lastUsed: Date;
  createdAt: Date;
  ipAddress?: string;
  isCurrent: boolean;
}

export const sessionsApi = {
  // Get all sessions
  async getSessions(): Promise<{ success: boolean; sessions: SessionInfo[]; totalCount: number }> {
    const response = await axios.get(`${API_BASE_URL}/api/v1/auth/sessions`, {
      headers: {
        Authorization: `Bearer ${localStorage.getItem('accessToken')}`,
      },
    });
    return response.data;
  },

  // Get session count
  async getSessionCount(): Promise<{ success: boolean; activeSessionCount: number }> {
    const response = await axios.get(`${API_BASE_URL}/api/v1/auth/sessions/count`, {
      headers: {
        Authorization: `Bearer ${localStorage.getItem('accessToken')}`,
      },
    });
    return response.data;
  },

  // Revoke specific session
  async revokeSession(sessionId: string): Promise<{ success: boolean; message: string }> {
    const response = await axios.delete(`${API_BASE_URL}/api/v1/auth/sessions/${sessionId}`, {
      headers: {
        Authorization: `Bearer ${localStorage.getItem('accessToken')}`,
      },
    });
    return response.data;
  },

  // Revoke all sessions except current
  async revokeAllSessions(): Promise<{ success: boolean; message: string; revokedCount: number }> {
    const response = await axios.delete(`${API_BASE_URL}/api/v1/auth/sessions`, {
      headers: {
        Authorization: `Bearer ${localStorage.getItem('accessToken')}`,
      },
    });
    return response.data;
  },
};
