import {
  Controller,
  Post,
  Req,
  Headers,
  BadRequestException,
  Get,
  Body,
  Query,
  Res,
} from "@nestjs/common";
import { StripeService } from "./stripe.service";
import Stripe from "stripe";
import { get } from "http";

@Controller("stripe/webhook")
export class StripeWebhookController {
  constructor(private readonly stripeService: StripeService) {}

  @Post()
  async handleWebhook(
    @Req() req: any,
    @Headers("stripe-signature") signature: string,
  ) {
    const event = this.stripeService.constructWebhookEvent(req.body, signature);
    switch (event.type) {
      case "account.updated":
        await this.stripeService.handleAccountUpdated(
          event.data.object as Stripe.Account,
        );
        break;
      case "account.application.authorized":
        await this.stripeService.handleAccountUpdated(event.data.object as any);
        break;

      case "account.application.deauthorized":
        await this.stripeService.handleAccountDisconnected(
          (event.data.object as any).account,
        );
        break;
      case "checkout.session.completed":
        await this.stripeService.handleCheckoutSessionCompleted(
          event.data.object as Stripe.Checkout.Session,
        );
        break;
    }

    return { received: true };
  }

@Get("payment-status")
async getPaymentStatus(
  @Req() req: any,
  @Res() res:any,
  @Query("sessionId") sessionId: string,
) {
  console.log(sessionId, "sessionId");
  return this.stripeService.getPaymentStatus(res, sessionId);
}

}
