import { Controller } from '@nestjs/common';
import { MessagePattern, RpcException } from '@nestjs/microservices';
import { ShopItemCategoryService } from './shopitemcategory.service';

@Controller()
export class ShopItemCategoryController {
  constructor(private readonly categoryService: ShopItemCategoryService) {}

  @MessagePattern({ cmd: 'shop_item_category_add' })
  async add(data: { body: any }) {
    try {
      const { body } = data;
      return await this.categoryService.add(body);
    } catch (error) {
      throw new RpcException('Failed to add shop item category');
    }
  }

  @MessagePattern({ cmd: 'shop_item_category_get_all' })
  async getAll() {
    try {
      return await this.categoryService.getAll();
    } catch (error) {
      throw new RpcException('Failed to get all categories');
    }
  }

  @MessagePattern({ cmd: 'shop_item_category_get_by_id' })
  async getById(id: number) {
    try {
      return await this.categoryService.getById(id);
    } catch (error) {
      throw new RpcException('Failed to get category by ID');
    }
  }

  @MessagePattern({ cmd: 'shop_item_category_update' })
  async update(data: { id: number; body: any }) {
    try {
      return await this.categoryService.update(data.id, data.body);
    } catch (error) {
      throw new RpcException('Failed to update category');
    }
  }

  @MessagePattern({ cmd: 'shop_item_category_get_by_merchant_id' })
  async getByMerchantId(data: { Merchant_idMerchant: number }) {
    try {
      return await this.categoryService.getByMerchantId(
        data.Merchant_idMerchant,
      );
    } catch (error) {
      throw new RpcException('Failed to get categories by merchant ID');
    }
  }
}
