import { BaseApiClient } from '../base/BaseApiClient';
import type { UserDto, UserListResponse, ListUsersQuery, DashboardStats } from '@/lib/types/admin';

/**
 * Admin API Client
 *
 * Provides methods for admin operations like user management.
 * Only accessible to users with Owner or Super Admin roles.
 */
export class AdminApiClient extends BaseApiClient {
  /**
   * List all users with filtering, sorting, and pagination
   * Requires Owner or Super Admin role
   */
  async listUsers(query: ListUsersQuery = {}): Promise<UserListResponse> {
    const params = new URLSearchParams();
    
    if (query.role) params.append('role', query.role);
    if (query.status) params.append('status', query.status);
    if (query.email) params.append('email', query.email);
    if (query.search) params.append('search', query.search);
    if (query.page) params.append('page', query.page.toString());
    if (query.limit) params.append('limit', query.limit.toString());
    if (query.sortBy) params.append('sortBy', query.sortBy);
    if (query.sortDirection) params.append('sortDirection', query.sortDirection);

    return this.get<UserListResponse>(`/admin/users?${params.toString()}`);
  }

  /**
   * Get a single user by ID
   * Requires Owner or Super Admin role
   */
  async getUser(userId: string): Promise<UserDto> {
    return this.get<UserDto>(`/admin/users/${userId}`);
  }

  /**
   * Update user roles
   * Requires Owner role only
   */
  async updateUserRoles(userId: string, roles: string[]): Promise<UserDto> {
    return this.patch<UserDto>(`/admin/users/${userId}/roles`, { roles });
  }

  /**
   * Update user status (activate/suspend/delete)
   * Requires Owner or Super Admin role
   */
  async updateUserStatus(userId: string, status: string): Promise<UserDto> {
    return this.patch<UserDto>(`/admin/users/${userId}/status`, { status });
  }

  /**
   * Delete a user (soft delete)
   * Requires Owner or Super Admin role
   */
  async deleteUser(userId: string): Promise<void> {
    return this.delete(`/admin/users/${userId}`);
  }

  /**
   * Create a new user
   * Requires Owner or Super Admin role
   */
  async createUser(userData: {
    email: string;
    displayName: string;
    roles: string[];
    primaryDriverId?: string;
  }): Promise<UserDto> {
    return this.post<UserDto>(`/admin/users`, userData);
  }

  /**
   * Get dashboard statistics
   * Requires Owner or Super Admin role
   */
  async getDashboardStats(): Promise<DashboardStats> {
    return this.get<DashboardStats>(`/admin/dashboard/stats`);
  }
}