import { Body, Controller, Post, UseGuards, Request, Query, Get, Param, Delete, Patch } from '@nestjs/common';
import { StripeService } from './stripe.service';
import * as dto from './dto/index'
import {
  ApiTags,
  ApiQuery,
  ApiOperation,
  ApiResponse,
  ApiBody,
  ApiBearerAuth,
  ApiHeader,
  ApiConsumes,
} from '@nestjs/swagger';
import { query } from 'express';
import { StripeWebhooksService } from './stripe.webhooks';
import { AuthGuard } from '../auth/auth.guard';

@ApiTags('stripe')
@Controller('stripe')
export class StripeController {
  constructor(
    private readonly stripe_service: StripeService,
    private readonly StripeWebhooksService: StripeWebhooksService
  ) { }


  // @UseGuards(AuthGuard)
  // @ApiBearerAuth('access_token')
  @Post('token')
  async create_token(@Request() req: any) {
    try {
      let data = await this.stripe_service.create_token(req)
      return data;
    } catch (error) {
      throw error
    }
  }


  // @UseGuards(AuthGuard)
  // @ApiBearerAuth('access_token')
  @Post('plan')
  async create_plan(@Body() body: dto.plan.plan) {
    try {
      let data = await this.stripe_service.create_plan(body)
      return data;
    } catch (error) {
      throw error
    }
  }

  // @UseGuards(AuthGuard)
  @ApiBearerAuth('access_token')
  @Get('plan')
  async list_plan(@Request() req: any) {
    try {
      let data = await this.stripe_service.list_plan(req)
      return data;
    } catch (error) {
      throw error
    }
  }

  // @UseGuards(AuthGuard)
  // @ApiBearerAuth('access_token')
  @Get('plan/:_id')
  async get_plan(@Param('_id') _id: string) {
    try {
      let data = await this.stripe_service.get_plan(_id)
      return data;
    } catch (error) {
      throw error
    }
  }

  // @UseGuards(AuthGuard)
  // @ApiBearerAuth('access_token')
  @Delete('plan/:_id')
  async delete_plan(@Param('_id') _id: string) {
    try {
      let data = await this.stripe_service.delete_plan(_id)
      return data;
    } catch (error) {
      throw error
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('access_token')
  @Post('subscription')
  async create_subscription(@Body() body: dto.plan.subscription, @Request() req: any) {
    try {
      let data = await this.stripe_service.create_subscription(body, req)
      return data;
    } catch (error) {
      throw error
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('access_token')
  @Patch('subscription/upgrade')
  async upgrade_subscription(@Body() body: dto.plan.upgradeSubscription, @Request() req: any) {
    try {
      let data = await this.stripe_service.upgradeSubscription(body, req)
      return data;
    } catch (error) {
      throw error
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('access_token')
  @Patch('subscription/cancel')
  async cancel_subscription(@Request() req: any) {
    try {
      let data = await this.stripe_service.cancel_subscription(req)
      return data;
    } catch (error) {
      throw error
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('access_token')
  @Get('subscription')
  async get_user_subscription(@Request() req: any) {
    try {
      let data = await this.stripe_service.get_user_subscription(req)
      return data;
    } catch (error) {
      throw error
    }
  }


  // @UseGuards(AuthGuard)
  // @ApiBearerAuth('access_token')
  // @Post('add/card')
  // async generatePaymentMethod(@Body() body: dto.card_details, @Request() req: any) {
  //   try {
  //     let data = await this.stripe_service.generatePaymentMethod(body, req)
  //     return data;
  //   } catch (error) {
  //     throw error
  //   }
  // }


  @Post('web-hooks')
  async webhooks(@Request() req: any) {
    try {
      let data = await this.StripeWebhooksService.webhooks(req)
      return data;
    } catch (error) {
      throw error
    }
  }
}