import { Request, Response } from 'express';
import Stripe from 'stripe';
import dotenv from 'dotenv';

dotenv.config();

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string, {
    // apiVersion: '2025-01-27.acacia', // Let default or specific version if needed
});

export const createPaymentIntent = async (req: Request, res: Response) => {
    try {
        const { amount, currency = 'inr' } = req.body;

        const paymentIntent = await stripe.paymentIntents.create({
            amount: Math.round(amount * 100), // Stripe expects amount in paise/cents
            currency,
            automatic_payment_methods: {
                enabled: true,
            },
        });

        res.json({ clientSecret: paymentIntent.client_secret });
    } catch (error: any) {
        console.error("Stripe Error:", error);
        res.status(500).json({ message: error.message });
    }
};
