import { Controller, Get, NotFoundException, Param,  Post, Res } from "@nestjs/common";
import { PaymentService } from "./payment.service";
import { Public } from "@edr/api-common";
import { Response } from "express"

@Public()
@Controller("payments")
export class PaymentController {
  constructor(private readonly paymentService: PaymentService,) { }

  @Post("/initiate")
  initiate() {
    return this.paymentService.initBookingTelebirr("123", "web")
  }

  @Post("/bookings/check-payment/:orderId")
  checkPayment(@Param("orderId") orderId: string) {
    return this.paymentService.checkStatusAndUpdate(orderId)
  }

  @Get("/bookings/telebirr/redirect/:orderId")
  async pay(@Param("orderId") orderId: string, @Res() res: Response) {
    const payment = await this.paymentService.getActivePaymentByOrderIdAndMethod(orderId, "telebirr")
    if (!payment) {
      throw new NotFoundException('payment not found')
    }

    return res.send(`
     <!DOCTYPE html>
<html>
<head>
  <title>Redirecting...</title>
</head>
<body>
  <p>Redirecting...</p>

  <script>
    window.location.href = "${payment.clientAction?.url}";
  </script>
</body>
</html>
    `);
  }
}
