import { Controller, Get, Middleware, Post } from '@overnightjs/core';
import { Request, Response } from 'express';
import { auth, AuthRequest } from '../middleware/auth';
import SubscriptionService from '../services/SubscriptionService';

@Controller('api/subscription')
export class SubscriptionController {

    /**
     * Create a new subscription
     * POST /api/subscription/subscribe
     */
    @Post('subscribe')
    async subscribe(req: Request, res: Response) {
        try {
            const subscription = await SubscriptionService.subscribe(req.body);
            res.status(200).json({
                success: true,
                message: 'Subscription active. Login details sent to email.',
                subscription,
            });
        } catch (error: any) {
            console.error('Subscription error:', error);
            const statusCode = error.message.includes('not found') ? 404 : 400;
            res.status(statusCode).json({
                success: false,
                error: error.message || 'Server error',
            });
        }
    }

    /**
     * Upgrade subscription
     * POST /api/subscription/upgrade
     */
    @Post('upgrade')
    async upgrade(req: Request, res: Response) {
        try {
            const subscription = await SubscriptionService.upgrade(req.body);
            res.status(200).json({
                success: true,
                message: 'Subscription upgraded successfully',
                subscription,
            });
        } catch (error: any) {
            console.error('Subscription upgrade error:', error);
            const statusCode = error.message.includes('not found') ? 404 : 400;
            res.status(statusCode).json({
                success: false,
                error: error.message || 'Server error',
            });
        }
    }

    /**
     * Get current user's active subscription
     * GET /api/subscription/me
     */
    @Get('me')
    @Middleware(auth)
    async getMySubscription(req: Request, res: Response) {
        try {
            const userId = (req as AuthRequest).user.userId;
            const subscriptions = await SubscriptionService.getActiveSubscription(userId);
            res.status(200).json({
                success: true,
                data: subscriptions,
            });
        } catch (error: any) {
            console.error('Get subscription error:', error);
            const statusCode = error.message.includes('not found') ? 404 : 500;
            res.status(statusCode).json({
                success: false,
                error: error.message || 'Server error',
            });
        }
    }

    /**
     * Cancel current user's active subscription
     * POST /api/subscription/cancel
     */
    @Post('cancel')
    @Middleware(auth)
    async cancelMySubscription(req: Request, res: Response) {
        try {
            const userId = (req as AuthRequest).user.userId;
            const { subscriptionId } = req.body || {};
            const subscription = await SubscriptionService.cancelMyActiveSubscription(userId, subscriptionId);
            res.status(200).json({
                success: true,
                message: 'Subscription cancelled successfully',
                data: subscription,
            });
        } catch (error: any) {
            console.error('Cancel subscription error:', error);
            const statusCode = error.message.includes('not found') ? 404 : 400;
            res.status(statusCode).json({
                success: false,
                error: error.message || 'Server error',
            });
        }
    }
}
