import { Injectable } from '@nestjs/common';
import { CreateCategoryDto } from './dto/create-category.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Category } from './entities/category.entity';
import { LANGUAGES } from 'src/shared/enum/languages.enum';
import { CreateCategoryReqDto } from './dto/create-category-req.dto';

@Injectable()
export class CategoryService {
  constructor(@InjectRepository(Category) private categoryRepository) {}

  async create(createCategoryDto: CreateCategoryReqDto): Promise<void> {
    const categoriyEN: CreateCategoryDto = {
      name: createCategoryDto.nameEN,
      language: LANGUAGES.EN,
    };

    const insertedId: number = (await this.categoryRepository.insert(categoriyEN)).raw.insertId;

    await this.categoryRepository.insert({
      idCategory: insertedId,
      name: createCategoryDto.nameES,
      language: LANGUAGES.ES,
    });
  }

  async findAll(lang: string) {
    return await this.categoryRepository.find({ where: { language: lang }, select: ['idCategory', 'name'] });
  }

  async remove(idCategory: number) {
    await this.categoryRepository.delete({ idCategory });
  }

  async findOne(idCategory: number): Promise<Category> {
    return await this.categoryRepository.findOne({ where: { idCategory } });
  }
}
