/*
 * For any questions, contact us at: raman@worktif.com
 */

/**
 * User data structure
 */
export interface User {
  id: string;
  email: string;
  name: string;
  createdAt: string;
  updatedAt: string;
}

/**
 * User creation input
 */
export interface CreateUserInput {
  email: string;
  name: string;
  password: string;
}

export interface RuntimeServiceInterface {
}

/**
 * Users Service
 *
 * Handles user management, profile operations, and user data.
 * This service is registered as a singleton in the DI container.
 */
export class UsersService implements RuntimeServiceInterface {
  /**
   * Create a new user
   *
   * @param input - User creation data
   * @returns Created user
   */
  async createUser(input: CreateUserInput): Promise<User> {
    if (!input.email) {
      throw new Error('Email is required');
    }
    if (!input.name) {
      throw new Error('Name is required');
    }
    if (!input.password || input.password.length < 8) {
      throw new Error('Password must be at least 8 characters');
    }

    const userId = `user_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
    const now = new Date().toISOString();

    return {
      id: userId,
      email: input.email,
      name: input.name,
      createdAt: now,
      updatedAt: now,
    };
  }

  /**
   * Get user by ID
   *
   * @param userId - User identifier
   * @returns User data or null if not found
   */
  async getUser(userId: string): Promise<User | null> {
    if (!userId) {
      throw new Error('User ID is required');
    }

    // Simulate user lookup (would query database in real implementation)
    return {
      id: userId,
      email: 'user@example.com',
      name: 'Example User',
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString(),
    };
  }

  /**
   * Update user profile
   *
   * @param userId - User identifier
   * @param updates - Fields to update
   * @returns Updated user
   */
  async updateUser(userId: string, updates: Partial<Pick<User, 'name' | 'email'>>): Promise<User> {
    if (!userId) {
      throw new Error('User ID is required');
    }

    const existingUser = await this.getUser(userId);
    if (!existingUser) {
      throw new Error('User not found');
    }

    return {
      ...existingUser,
      ...updates,
      updatedAt: new Date().toISOString(),
    };
  }

  /**
   * Delete user
   *
   * @param userId - User identifier
   * @returns Deletion result
   */
  async deleteUser(userId: string): Promise<{ success: boolean; userId: string }> {
    if (!userId) {
      throw new Error('User ID is required');
    }

    return {
      success: true,
      userId,
    };
  }
}
