/**
 * Users API
 *
 * User management API calls
 */

import { apiClient } from './client';

export interface User {
  id: string;
  organizationId: string;
  email: string;
  firstName: string;
  lastName: string;
  role: 'admin' | 'manager' | 'user' | 'viewer';
  phoneNumber?: string;
  isEmailVerified: boolean;
  isActive: boolean;
  lastLoginAt?: string;
  createdAt: string;
  updatedAt: string;
}

export interface CreateUserRequest {
  organizationId: string;
  email: string;
  firstName: string;
  lastName: string;
  role: 'admin' | 'manager' | 'user' | 'viewer';
  phoneNumber?: string;
  password?: string;
}

export interface UpdateUserRequest {
  firstName?: string;
  lastName?: string;
  phoneNumber?: string;
  isActive?: boolean;
}

export interface ChangePasswordRequest {
  currentPassword: string;
  newPassword: string;
}

export const usersApi = {
  /**
   * Get users in current organization
   */
  async list(): Promise<User[]> {
    return apiClient.get<User[]>('/api/v1/users');
  },

  /**
   * Get user by ID
   */
  async getById(id: string): Promise<User> {
    return apiClient.get<User>(`/api/v1/users/${id}`);
  },

  /**
   * Create/invite user
   */
  async create(data: CreateUserRequest): Promise<User> {
    return apiClient.post<User>('/api/v1/users', data);
  },

  /**
   * Update user
   */
  async update(id: string, data: UpdateUserRequest): Promise<User> {
    return apiClient.patch<User>(`/api/v1/users/${id}`, data);
  },

  /**
   * Change user role
   */
  async changeRole(id: string, role: 'admin' | 'manager' | 'user' | 'viewer'): Promise<User> {
    return apiClient.patch<User>(`/api/v1/users/${id}/role`, { role });
  },

  /**
   * Deactivate user
   */
  async deactivate(id: string): Promise<void> {
    return apiClient.patch<void>(`/api/v1/users/${id}`, { isActive: false });
  },

  /**
   * Activate user
   */
  async activate(id: string): Promise<void> {
    return apiClient.patch<void>(`/api/v1/users/${id}`, { isActive: true });
  },

  /**
   * Delete user
   */
  async delete(id: string): Promise<void> {
    return apiClient.delete<void>(`/api/v1/users/${id}`);
  },

  /**
   * Change password
   */
  async changePassword(data: ChangePasswordRequest): Promise<{ message: string }> {
    return apiClient.patch<{ message: string }>('/api/v1/users/me/password', data);
  },
};
