import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';

@Injectable()
export class DepartmentsService {
  constructor(private prisma: PrismaService) {}

  async findAll() {
    return this.prisma.department.findMany({
      orderBy: { name: 'asc' },
      include: { _count: { select: { employees: true } } },
    });
  }

  async findById(id: number) {
    return this.prisma.department.findUnique({
      where: { id },
      include: { employees: true },
    });
  }

  async create(data: { name: string; code?: string; description?: string }) {
    return this.prisma.department.create({
      data: {
        name: data.name,
        code: data.code,
        description: data.description,
      },
    });
  }

  async update(id: number, data: { name?: string; code?: string; description?: string }) {
    return this.prisma.department.update({
      where: { id },
      data,
    });
  }

  async delete(id: number) {
    return this.prisma.department.delete({
      where: { id },
    });
  }
}
