import { User, CreateUserDto, UpdateUserDto } from '../models/User';

const store: User[] = [];
let nextId = 1;
export const userRepository = {

  findAll(): User[] {
    return [...store];
  },

  findById(id: number): User | undefined {
    return store.find(u => u.id === id);
  },

  create(dto: CreateUserDto): User {
    const user: User = {
      id: nextId++,
      name: dto.name,
      email: dto.email,
      createdAt: new Date(),
    };
    store.push(user);
    return user;
  },

  update(id: number, dto: UpdateUserDto): User | undefined {
    const user = store.find(u => u.id === id);
    if (!user) return undefined;
    if (dto.name) user.name = dto.name;
    if (dto.email) user.email = dto.email;
    return user;
  },

  delete(id: number): boolean {
    const idx = store.findIndex(u => u.id === id);
    if (idx === -1) return false;
    store.splice(idx, 1);
    return true;
  },

};