import { userRepository } from '../repositories/userRepository';
import { CreateUserDto, UpdateUserDto, User } from '../models/User';
import { AppError } from '../middleware/errorHandler';
export const userService = {
  getAll(): User[] {
    return userRepository.findAll();
  },
  getById(id: number): User {
    const user = userRepository.findById(id);
    if (!user) throw new AppError(404, `User ${id} not found`);
    return user;
  },
  create(dto: CreateUserDto): User {
    if (!dto.name?.trim()) throw new AppError(400, 'Name is required');
    if (!dto.email?.trim()) throw new AppError(400, 'Email is required');
    // In a real app: check email uniqueness, hash password, etc.
    return userRepository.create(dto);
  },
  update(id: number, dto: UpdateUserDto): User {
    const updated = userRepository.update(id, dto);
    if (!updated) throw new AppError(404, `User ${id} not found`);
    return updated;
  },
  delete(id: number): void {
    const deleted = userRepository.delete(id);
    if (!deleted) throw new AppError(404, `User ${id} not found`);
  },
};