import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Headers,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { OrderService } from '../../application/services/order.service';
import { CreateOrderDto } from '../../application/dtos/order.dto';

@Controller('orders')
export class OrderController {
  constructor(private orderService: OrderService) {}

  /**
   * Create a new order
   */
  @Post()
  @HttpCode(HttpStatus.CREATED)
  async createOrder(
    @Headers('x-user-id') userId: string,
    @Body() dto: CreateOrderDto,
  ) {
    const order = await this.orderService.createOrder({
      userId,
      ...dto,
    });

    return {
      success: true,
      data: order,
    };
  }

  /**
   * Get user's orders
   */
  @Get()
  async getUserOrders(@Headers('x-user-id') userId: string) {
    const orders = await this.orderService.findByUserId(userId);
    return {
      success: true,
      data: orders,
    };
  }

  /**
   * Get order by ID
   */
  @Get(':id')
  async getOrder(@Param('id') orderId: string) {
    const order = await this.orderService.findById(orderId);
    return {
      success: true,
      data: order,
    };
  }

  /**
   * Get order status
   */
  @Get(':id/status')
  async getOrderStatus(@Param('id') orderId: string) {
    const order = await this.orderService.findById(orderId);
    return {
      success: true,
      data: {
        orderId: order.id,
        status: order.status,
        paidAt: order.paidAt,
        completedAt: order.completedAt,
      },
    };
  }

  /**
   * Cancel order
   */
  @Post(':id/cancel')
  @HttpCode(HttpStatus.OK)
  async cancelOrder(@Param('id') orderId: string) {
    const order = await this.orderService.cancelOrder(orderId);
    return {
      success: true,
      data: {
        orderId: order.id,
        status: order.status,
      },
    };
  }
}
