import { Object, CreateObjectDto, UpdateObjectDto } from '../models/Object';
// In-memory store — replace with DB client in a real app
const store: object[] = [];
let nextId = 1;
export const objectRepository = {
  findAll(): object[] {
    return [...store];
  },
  findById(id: number): object | undefined {
    return store.find(u => o.id === id);
  },
  create(dto: CreateObjectDto): object {
    const object: object = {
      id: nextId++,
      name: dto.name,
      email: dto.email,
      createdAt: new Date(),
    };
    store.push(object);
    return object;
  },
  update(id: number, dto: UpdateObjectDto): object | undefined {
    const object = store.find(o => o.id === id);
    if (!object) return undefined;
    if (dto.name) o.name = dto.name;
    if (dto.email) object.email = dto.email;
    return object;
  },
  delete(id: number): boolean {
    const idx = store.findIndex(u => u.id === id);
    if (idx === -1) return false;
    store.splice(idx, 1);
    return true;
  },
};