import { Injectable } from "@nestjs/common";
import { CreateDiagnosisSubCategoryListDto } from "./dto/create-diagnosis-sub-category-list.dto";
import { DataSource } from "typeorm";

@Injectable()
export class DiagnosisSubCategoryListService {
  constructor(private readonly connection: DataSource) {}
  async create(
    createDiagnosisSubCategoryListDto: CreateDiagnosisSubCategoryListDto
  ) {
    await this.connection.query(
      `insert into diagnosis_test_sub_category
       (sub_category,category_id) values (?,?)`,
      [
        createDiagnosisSubCategoryListDto.sub_category,
        createDiagnosisSubCategoryListDto.category_id,
      ]
    );
    return {
      status: "success",
      message: "diagnosis sub category added successfully",
    };
  }

  async findAll(category_id: any, search: any) {
    if (!category_id) {
      return {
        status: "failed",
        message: "Enter category_id to get sub category",
      };
    }
    let getAllCategoryList;
    if (search) {
      search.toLocaleLowerCase();
      getAllCategoryList = await this.connection.query(
        `select * from diagnosis_test_sub_category 
        WHERE category_id = ? and sub_category LIKE ? 
        limit 10`,
        [category_id, "%" + search + "%"]
      );
    } else {
      getAllCategoryList = await this.connection.query(
        `select * from diagnosis_test_sub_category WHERE category_id = ?  limit 10`,
        [category_id]
      );
    }
    return {
      status: "success",
      message: "diagnosis sub category list fetched successfully",
      details: getAllCategoryList,
    };
  }

  async findunits() {
    let getunits = await this.connection.query(
      `select * from bundle_diagnostic_units`
    );
    return {
      status: "success",
      message: "bundle_diagnostic_units list fetched successfully",
      details: getunits,
    };
  }

  async insertunits(unit_name: string) {
    let getunits = await this.connection.query(
      `INSERT INTO bundle_diagnostic_units (unit_name) VALUES (?)`,
      [unit_name]
    );
    return {
      status: "success",
      message: "bundle_diagnostic_units inserted successfully",
    };
  }
}
