const {
  getAllBrandSparepartsDb,
  getBrandSparepartByIdDb,
  createBrandSparepartDb,
  updateBrandSparepartDb,
  deleteBrandSparepartDb,
  checkBrandSparepartNameExistsDb,
} = require('../db/brand_sparepart.db');

const { ErrorHandler } = require('../helpers/error');

class BrandSparepartService {
  static async getAllBrandSparepart(param) {
    try {
      const results = await getAllBrandSparepartsDb(param);
      results.data.map((item) => {});
      return results;
    } catch (error) {
      throw new ErrorHandler(error.statusCode || 500, error.message);
    }
  }

  static async getBrandSparepartById(id) {
    try {
      const brandSparepart = await getBrandSparepartByIdDb(id);
      if (!brandSparepart) throw new ErrorHandler(404, 'Brand sparepart not found');
      return brandSparepart;
    } catch (error) {
      throw new ErrorHandler(error.statusCode || 500, error.message);
    }
  }

  static async createBrandSparepart(data) {
    try {
      if (!data || typeof data !== 'object') data = {};

      if (data.sparepart_name) {
        const exists = await checkBrandSparepartNameExistsDb(data.sparepart_name);
        if (exists) throw new ErrorHandler(400, 'Brand sparepart name already exists');
      }

      const insertData = {
        sparepart_name: data.sparepart_name,
        brand_sparepart_description: data.brand_sparepart_description,
        path_foto: data.path_foto,
        error_code_id: data.error_code_id,
        is_active: data.is_active,
        created_by: data.created_by,
      };

      const created = await createBrandSparepartDb(insertData);
      if (!created) throw new ErrorHandler(500, 'Failed to create brand sparepart');

      return created;
    } catch (error) {
      throw new ErrorHandler(error.statusCode || 500, error.message);
    }
  }

  static async updateBrandSparepart(id, data) {
    try {
      const existing = await getBrandSparepartByIdDb(id);
      if (!existing) throw new ErrorHandler(404, 'Brand sparepart not found');

      if (
        data.sparepart_name &&
        data.sparepart_name !== existing.sparepart_name
      ) {
        const exists = await checkBrandSparepartNameExistsDb(data.sparepart_name, id);
        if (exists) throw new ErrorHandler(400, 'Brand sparepart name already exists');
      }

      const updateData = {
        sparepart_name: data.sparepart_name,
        brand_sparepart_description: data.brand_sparepart_description,
        path_foto: data.path_foto || existing.path_foto,
        error_code_id: data.error_code_id,
        is_active: data.is_active ?? existing.is_active,
        updated_by: data.updated_by || null,
      };

      const updated = await updateBrandSparepartDb(id, updateData);
      if (!updated) throw new ErrorHandler(500, 'Failed to update brand sparepart');

      return await this.getBrandSparepartById(id);
    } catch (error) {
      throw new ErrorHandler(error.statusCode || 500, error.message);
    }
  }

  static async deleteBrandSparepart(id, userId) {
    try {
      const existing = await getBrandSparepartByIdDb(id);
      if (!existing) throw new ErrorHandler(404, 'Brand sparepart not found');

      const deleted = await deleteBrandSparepartDb(id, userId);
      if (!deleted) throw new ErrorHandler(500, 'Failed to delete brand sparepart');

      return deleted;
    } catch (error) {
      throw new ErrorHandler(error.statusCode || 500, error.message);
    }
  }
}

module.exports = BrandSparepartService;
