import { Controller } from '@nestjs/common';
import { MessagePattern, RpcException } from '@nestjs/microservices';
import { ShopService } from './shop.service';

@Controller()
export class ShopController {
  constructor(private readonly shopItemService: ShopService) {}

  @MessagePattern({ cmd: 'shop_item_add' })
  async add(data: { body: any }) {
    try {
      const { body } = data;
      return await this.shopItemService.add(body);
    } catch (error) {
      throw new RpcException('Failed to add shop item');
    }
  }

  @MessagePattern({ cmd: 'shop_item_get_all' })
  async getAll() {
    try {
      console.log('Mormal conmtroller also calling!');
      return await this.shopItemService.getAll();
    } catch (error) {
      throw new RpcException('Failed to get all shop items');
    }
  }

  @MessagePattern({ cmd: 'shop_item_get_by_id' })
  async getById(id: number) {
    try {
      return await this.shopItemService.getById(id);
    } catch (error) {
      throw new RpcException('Failed to get shop item by ID');
    }
  }

  @MessagePattern({ cmd: 'shop_item_update' })
  async update(data: { id: number; body: any }) {
    try {
      const { id, body } = data;
      return await this.shopItemService.update(id, body);
    } catch (error) {
      throw new RpcException('Failed to update shop item');
    }
  }

  @MessagePattern({ cmd: 'get_shop_item_by_merchant_id' })
  async getShopItemByMerchantId(data: { Merchant_idMerchant: number }) {
    try {
      const { Merchant_idMerchant } = data;
      return await this.shopItemService.getShopItemByMerchantId(
        Merchant_idMerchant,
      );
    } catch (err) {
      throw new RpcException('Failed to fetch shop item by merchant id');
    }
  }
}
