import { IUserRepository } from '../../domain/irepositories/IUserRepository.js';
import { User } from '../../domain/models/User.js';
import DatabaseConnection from '../db/DatabaseConnection.js';

export class UserRepository extends IUserRepository {
  constructor() {
    super();
    this.prisma = DatabaseConnection.getClient();
  }

  async findById(id) {
    const user = await this.prisma.user.findUnique({
      where: { id: parseInt(id) }
    });
    return user ? new User(user) : null;
  }

  async findByEmail(email) {
    const user = await this.prisma.user.findUnique({
      where: { email }
    });
    return user ? new User(user) : null;
  }

  async findAll() {
    const users = await this.prisma.user.findMany({
      orderBy: { createdAt: 'desc' }
    });
    return users.map(user => new User(user));
  }

  async create(userData) {
    const user = await this.prisma.user.create({
      data: userData
    });
    return new User(user);
  }

  async update(id, userData) {
    const user = await this.prisma.user.update({
      where: { id: parseInt(id) },
      data: userData
    });
    return new User(user);
  }

  async delete(id) {
    await this.prisma.user.delete({
      where: { id: parseInt(id) }
    });
    return true;
  }
}
