import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CompanySize } from './entities/company_sizes.entity';

@Injectable()
export class CompanySizesService {
  constructor(
    @InjectRepository(CompanySize)
    private readonly companySizeRepository: Repository<CompanySize>,
  ) {}

  async findAll() {
    return this.companySizeRepository.find({
      where: { status: 1 },
      order: { label: 'ASC' },
    });
  }

  async findAllPaginated(page: number = 1, limit: number = 10) {
    const skip = (page - 1) * limit;
    const [data, total] = await this.companySizeRepository.findAndCount({
      skip,
      take: limit,
      order: { label: 'ASC' },
    });

    return {
      success: true,
      data,
      meta: {
        totalItems: total,
        itemsPerPage: limit,
        currentPage: page,
        totalPages: Math.ceil(total / limit),
      },
    };
  }

  async create(createDto: any) {
    const newItem = this.companySizeRepository.create(createDto);
    return this.companySizeRepository.save(newItem);
  }

  async update(id: number, updateDto: any) {
    return this.companySizeRepository.update(id, updateDto);
  }

  async remove(id: number) {
    return this.companySizeRepository.delete(id);
  }
}
