import { Product } from '@/entities/product.entity';
import CoreController from '@/system/core/core-controller';
import { Body, Controller, Param, Post, Put, Req, Sse } from '@nestjs/common';
import { ProductsService } from './products.service';
import { CreateProductDto } from './dtos/create-product.dto';
import { HistoryDto } from './dtos/publist-finish.dto';
import { SyncDto } from './dtos/syncs.dto';
import { UpdateProductDto } from './dtos/update-product.dto';
import { Request } from 'express';

@Controller('products')
export class ProductsController extends CoreController<
  Product,
  ProductsService
> {
  constructor(protected readonly service: ProductsService) {
    super(service);
  }

  @Post('')
  async create(@Body() data: CreateProductDto): Promise<any> {
    return this.service.create(data);
  }
  @Put(':id')
  async update(
    @Body() data: UpdateProductDto,
    @Param('id') id: Product['id'],
    @Req() request: Request,
  ): Promise<any> {
    return this.service.update(id, data as any, request);
  }

  @Post('sync')
  async sync(@Body() data: SyncDto): Promise<any> {
    return this.service.sync(data);
  }

  @Post('publist/:id')
  async publist(@Param('id') id: Product['id']): Promise<any> {
    return this.service.publist(id);
  }

  @Post('unlist/:id')
  async unlist(@Param('id') id: Product['id']): Promise<any> {
    return this.service.unlist(id);
  }

  @Post('re-publist/:id')
  async rePublist(@Param('id') id: Product['id']): Promise<any> {
    return this.service.rePublist(id);
  }

  @Post('publist-finish/:id')
  async publistFinish(
    @Param('id') id: Product['id'],
    @Body() data: HistoryDto,
  ): Promise<any> {
    return this.service.publistFinish(id, data);
  }

  @Post('delete-finish/:id')
  async deleteFinish(
    @Param('id') id: Product['id'],
    @Body() data: HistoryDto,
  ): Promise<any> {
    return this.service.deleteFinish(id, data);
  }

  @Post('update-finish/:id')
  async updateFinish(
    @Param('id') id: Product['id'],
    @Body() data: HistoryDto,
  ): Promise<any> {
    return this.service.updateFinish(id, data);
  }

  @Sse('publist-stream')
  async publistStream() {
    return this.service.sendPublistEvents();
  }

  @Sse('delete-stream')
  async deleteStream() {
    return this.service.sendDeleteEvents();
  }

  @Sse('edit-stream')
  async editStream() {
    return this.service.sendEditEvents();
  }
}
