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

@Injectable()
export class OrganizationTypesService {
  constructor(
    @InjectRepository(OrganizationType)
    private readonly organizationTypeRepository: Repository<OrganizationType>,
  ) {}

  async findAll() {
    return this.organizationTypeRepository.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.organizationTypeRepository.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.organizationTypeRepository.create(createDto);
    return this.organizationTypeRepository.save(newItem);
  }

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

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