import {
  Controller,
  Get,
  Post,
  Param,
  Req,
  UseGuards,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { PaymentService } from './payment.service';
import { JwtGuard } from 'src/common/guard/jwt/jwt.guard';
import { ResponseUtils } from 'src/common/utils/utils.response';
import { AuthContext } from 'src/modules/auth/types/auth.types';
import type { Request } from 'express';

@Controller('payment')
export class PaymentController {
  constructor(private readonly paymentService: PaymentService) {}

  @Get('info')
  @HttpCode(HttpStatus.OK)
  getPaymentInfo() {
    const info = this.paymentService.getPaymentInfo();
    return ResponseUtils.successfully(info, 'Payment info retrieved');
  }

  @Get('status')
  @UseGuards(JwtGuard)
  @HttpCode(HttpStatus.OK)
  async getPaymentStatus(@Req() req: Request) {
    const { username } = req.user as AuthContext;
    const status = await this.paymentService.getRegistrationStatus(username);
    const paymentInfo = status.needsPayment ? this.paymentService.getPaymentInfo() : null;
    return ResponseUtils.successfully(
      { needsPayment: status.needsPayment, payment: status.payment, paymentInfo },
      'Payment status retrieved',
    );
  }

  @Post('submit/proof/:attachmentId')
  @UseGuards(JwtGuard)
  @HttpCode(HttpStatus.OK)
  async submitProof(@Req() req: Request, @Param('attachmentId') attachmentId: string) {
    const { id, username } = req.user as AuthContext;
    const result = await this.paymentService.submitProof(id, username, attachmentId);
    return ResponseUtils.successfully(result, 'Payment proof submitted successfully');
  }

  @Get('history')
  @UseGuards(JwtGuard)
  @HttpCode(HttpStatus.OK)
  async getPaymentHistory(@Req() req: Request) {
    const { username } = req.user as AuthContext;
    const history = await this.paymentService.getUserPaymentHistory(username);
    return ResponseUtils.successfully(history, 'Payment history retrieved');
  }
}
