import { Controller, Get, Post, Body, Param } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBody, ApiBearerAuth } from '@nestjs/swagger';
import { StripeService } from './stripe.service';

class CreateCustomerDto {
    email: string;
    name: string;
}

class CreateSubscriptionDto {
    customerId: string;
    priceId: string;
}

class CreateCheckoutDto {
    customerId: string;
    priceId: string;
    successUrl: string;
    cancelUrl: string;
}

class BillingPortalDto {
    customerId: string;
    returnUrl: string;
}

class CancelSubscriptionDto {
    subscriptionId: string;
}

@ApiTags('Stripe')
@ApiBearerAuth()
@Controller('stripe')
export class StripeController {
    constructor(private readonly stripeService: StripeService) { }

    @Post('customers')
    @ApiOperation({ summary: 'Create a Stripe customer' })
    @ApiBody({ type: CreateCustomerDto })
    async createCustomer(@Body() body: CreateCustomerDto) {
        return this.stripeService.createCustomer(body.email, body.name);
    }

    @Post('subscriptions')
    @ApiOperation({ summary: 'Create a subscription' })
    @ApiBody({ type: CreateSubscriptionDto })
    async createSubscription(@Body() body: CreateSubscriptionDto) {
        return this.stripeService.createSubscription(body.customerId, body.priceId);
    }

    @Get('subscriptions/:customerId')
    @ApiOperation({ summary: 'List subscriptions for a customer' })
    async listSubscriptions(@Param('customerId') customerId: string) {
        return this.stripeService.listSubscriptions(customerId);
    }

    @Post('subscriptions/cancel')
    @ApiOperation({ summary: 'Cancel a subscription' })
    @ApiBody({ type: CancelSubscriptionDto })
    async cancelSubscription(@Body() body: CancelSubscriptionDto) {
        return this.stripeService.cancelSubscription(body.subscriptionId);
    }

    @Post('checkout')
    @ApiOperation({ summary: 'Create a checkout session' })
    @ApiBody({ type: CreateCheckoutDto })
    async createCheckoutSession(@Body() body: CreateCheckoutDto) {
        return this.stripeService.createCheckoutSession(
            body.customerId,
            body.priceId,
            body.successUrl,
            body.cancelUrl,
        );
    }

    @Post('billing-portal')
    @ApiOperation({ summary: 'Create a billing portal session' })
    @ApiBody({ type: BillingPortalDto })
    async createBillingPortalSession(@Body() body: BillingPortalDto) {
        return this.stripeService.createBillingPortalSession(
            body.customerId,
            body.returnUrl,
        );
    }
}
