import api from './api';

// Types
export type ConnectionStatus = 'PENDING' | 'ACCEPTED' | 'REJECTED';

export interface ConnectionRequest {
  id: string;
  userId: string;
  agentProfileId: string;
  status: ConnectionStatus;
  message: string | null;
  createdAt: string;
  updatedAt: string;
  respondedAt: string | null;
  user?: {
    id: string;
    email: string;
    avatar: string | null;
    userProfile?: {
      firstName: string | null;
      lastName: string | null;
      avatar: string | null;
    } | null;
  };
  agentProfile?: {
    id: string;
    firstName: string | null;
    lastName: string | null;
    avatar: string | null;
    headline: string | null;
    city: string | null;
    state: string | null;
    isVerified: boolean;
    agentType?: {
      name: string;
    } | null;
  };
}

export interface CreateConnectionRequestDto {
  agentProfileId: string;
  message?: string;
}

export interface RespondConnectionRequestDto {
  status: 'ACCEPTED' | 'REJECTED';
}

export interface ConnectionRequestCounts {
  pending: number;
  accepted: number;
  rejected: number;
  total: number;
}

export interface ConnectionStatusResponse {
  id: string;
  status: ConnectionStatus;
  createdAt: string;
  respondedAt: string | null;
}

interface ApiResponse<T> {
  success: boolean;
  data: T;
  message?: string;
}

class ConnectionRequestsService {
  private basePath = '/connection-requests';

  /**
   * Create a new connection request to an agent
   */
  async createRequest(dto: CreateConnectionRequestDto): Promise<ConnectionRequest> {
    const response = await api.post<ApiResponse<ConnectionRequest>>(this.basePath, dto);
    return response.data.data;
  }

  /**
   * Get all connection requests sent by the current user
   */
  async getMyRequests(status?: ConnectionStatus): Promise<ConnectionRequest[]> {
    const params = status ? { status } : {};
    const response = await api.get<ApiResponse<ConnectionRequest[]>>(
      `${this.basePath}/my-requests`,
      { params }
    );
    return response.data.data;
  }

  /**
   * Get all connection requests received by the current agent
   */
  async getReceivedRequests(status?: ConnectionStatus): Promise<ConnectionRequest[]> {
    const params = status ? { status } : {};
    const response = await api.get<ApiResponse<ConnectionRequest[]>>(
      `${this.basePath}/received`,
      { params }
    );
    return response.data.data;
  }

  /**
   * Get connection request counts for the current agent
   */
  async getRequestCounts(): Promise<ConnectionRequestCounts> {
    const response = await api.get<ApiResponse<ConnectionRequestCounts>>(
      `${this.basePath}/received/counts`
    );
    return response.data.data;
  }

  /**
   * Get connection status between current user and an agent
   */
  async getConnectionStatus(agentProfileId: string): Promise<ConnectionStatusResponse | null> {
    const response = await api.get<ApiResponse<ConnectionStatusResponse | null>>(
      `${this.basePath}/status/${agentProfileId}`
    );
    return response.data.data;
  }

  /**
   * Get a specific connection request by ID
   */
  async getRequestById(requestId: string): Promise<ConnectionRequest> {
    const response = await api.get<ApiResponse<ConnectionRequest>>(
      `${this.basePath}/${requestId}`
    );
    return response.data.data;
  }

  /**
   * Respond to a connection request (accept/reject)
   */
  async respondToRequest(
    requestId: string,
    dto: RespondConnectionRequestDto
  ): Promise<ConnectionRequest> {
    const response = await api.patch<ApiResponse<ConnectionRequest>>(
      `${this.basePath}/${requestId}/respond`,
      dto
    );
    return response.data.data;
  }

  /**
   * Cancel a pending connection request
   */
  async cancelRequest(requestId: string): Promise<void> {
    await api.delete(`${this.basePath}/${requestId}`);
  }
}

export const connectionRequestsService = new ConnectionRequestsService();
