import {
  Controller,
  Get,
  Post,
  Param,
  Query,
  HttpCode,
  HttpStatus,
  ParseIntPipe,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiParam,
} from '@nestjs/swagger';
import { PlentyoneService } from './plentyone.service';
import {
  GetOrdersQueryDto,
  GetStockQueryDto,
  GetStatsQueryDto,
  PaginatedOrdersResponseDto,
  PaginatedStockResponseDto,
  OrderResponseDto,
  OrderStatsResponseDto,
  RevenueStatsResponseDto,
} from './dto';
import { ConnectionTestResult } from '../base-connector';
import { RequirePermissions } from '../../../../auth/permissions/permissions.decorator';
import { Permission } from '../../../../auth/permissions/permissions.enum';

@ApiTags('Integrations - PlentyONE')
@ApiBearerAuth()
@Controller('integrations/plentyone')
export class PlentyoneController {
  constructor(private readonly plentyoneService: PlentyoneService) {}

  @Post('test')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Test PlentyONE connection' })
  @ApiResponse({
    status: 200,
    description: 'Connection test result',
  })
  @RequirePermissions(Permission.INTEGRATIONS_MANAGE)
  async testConnection(): Promise<ConnectionTestResult> {
    return this.plentyoneService.testConnection();
  }

  @Get('orders')
  @ApiOperation({ summary: 'Get list of orders' })
  @ApiResponse({
    status: 200,
    description: 'Paginated list of orders',
    type: PaginatedOrdersResponseDto,
  })
  @RequirePermissions(Permission.INTEGRATIONS_VIEW)
  async getOrders(
    @Query() query: GetOrdersQueryDto,
  ): Promise<PaginatedOrdersResponseDto> {
    return this.plentyoneService.getOrders(query);
  }

  @Get('orders/:id')
  @ApiOperation({ summary: 'Get order by ID' })
  @ApiParam({ name: 'id', description: 'Order ID' })
  @ApiResponse({
    status: 200,
    description: 'Order details',
    type: OrderResponseDto,
  })
  @ApiResponse({ status: 404, description: 'Order not found' })
  @RequirePermissions(Permission.INTEGRATIONS_VIEW)
  async getOrder(
    @Param('id', ParseIntPipe) id: number,
  ): Promise<OrderResponseDto> {
    return this.plentyoneService.getOrder(id);
  }

  @Get('stock')
  @ApiOperation({ summary: 'Get stock levels' })
  @ApiResponse({
    status: 200,
    description: 'Paginated list of stock items',
    type: PaginatedStockResponseDto,
  })
  @RequirePermissions(Permission.INTEGRATIONS_VIEW)
  async getStock(
    @Query() query: GetStockQueryDto,
  ): Promise<PaginatedStockResponseDto> {
    return this.plentyoneService.getStock(query);
  }

  @Get('stock/low')
  @ApiOperation({ summary: 'Get items with low stock' })
  @ApiResponse({
    status: 200,
    description: 'List of items below reorder level',
    type: PaginatedStockResponseDto,
  })
  @RequirePermissions(Permission.INTEGRATIONS_VIEW)
  async getLowStockItems(
    @Query('warehouseId', new ParseIntPipe({ optional: true }))
    warehouseId?: number,
  ): Promise<PaginatedStockResponseDto> {
    return this.plentyoneService.getLowStockItems(warehouseId);
  }

  @Get('stats')
  @ApiOperation({ summary: 'Get order statistics' })
  @ApiResponse({
    status: 200,
    description: 'Order statistics for the specified period',
    type: OrderStatsResponseDto,
  })
  @RequirePermissions(Permission.INTEGRATIONS_VIEW)
  async getStats(@Query() query: GetStatsQueryDto): Promise<OrderStatsResponseDto> {
    return this.plentyoneService.getOrderStats(query);
  }

  @Get('stats/revenue')
  @ApiOperation({ summary: 'Get revenue statistics' })
  @ApiResponse({
    status: 200,
    description: 'Revenue statistics grouped by date',
    type: [RevenueStatsResponseDto],
  })
  @RequirePermissions(Permission.INTEGRATIONS_VIEW)
  async getRevenueStats(
    @Query() query: GetStatsQueryDto,
  ): Promise<RevenueStatsResponseDto[]> {
    return this.plentyoneService.getRevenueStats(query);
  }
}
