import {
  Controller,
  DefaultValuePipe,
  Get,
  ParseIntPipe,
  Post,
  Query,
  UseGuards,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiQuery,
  ApiTags,
} from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { SyncService } from './sync.service';
import { SyncLogDto } from './dto/sync-log.dto';

@ApiTags('sync')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('sync')
export class SyncController {
  constructor(private readonly service: SyncService) {}

  @Post('categories')
  @ApiOperation({ summary: 'Manually trigger category sync' })
  syncCategories() {
    return this.service.syncCategories();
  }

  @Post('products')
  @ApiOperation({ summary: 'Manually trigger product sync' })
  syncProducts() {
    return this.service.syncProducts();
  }

  @Get('status')
  @ApiOperation({ summary: 'Recent sync log entries' })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async status(
    @Query('limit', new DefaultValuePipe(20), ParseIntPipe) limit: number,
  ): Promise<SyncLogDto[]> {
    const logs = await this.service.getStatus(limit);
    return logs.map((l) => SyncLogDto.from(l));
  }
}
