import { IUserRepository } from '../../domain/repositories/IUserRepository.js';
import { User } from '../../domain/models/User.js';
import { prisma } from '../database/prisma.js';

/**
 * UserRepository implementáció Prisma-val
 * Implementálja az IUserRepository interface-t
 */
export class UserRepository extends IUserRepository {
  async findById(id) {
    const user = await prisma.user.findUnique({
      where: { id }
    });
    return user ? new User(user) : null;
  }

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

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

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

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

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

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