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

@Controller('api/doctor-manager')
export class DoctorManagerController {

    /**
     * Get team members for owner account
     */
    @Get('team')
    @Middleware(auth)
    async getTeam(req: Request, res: Response): Promise<void> {
        try {
            const userId = (req as AuthRequest).user.userId;
            const team = await DoctorManagerService.getTeam(userId);
            res.json({ success: true, data: team });
        } catch (error: any) {
            console.error('Error fetching team:', error);
            res.status(500).json({ success: false, error: error.message });
        }
    }

    /**
     * Add a new doctor under owner account
     */
    @Post('add')
    @Middleware(auth)
    async addDoctor(req: Request, res: Response): Promise<void> {
        try {
            const userId = (req as AuthRequest).user.userId;
            const result = await DoctorManagerService.addDoctor(userId, req.body);
            res.status(201).json({ success: true, data: result.doctor, message: result.message });
        } catch (error: any) {
            const statusCode = error.message.includes('limit reached') ? 402 :
                error.message.includes('already exists') ? 400 :
                    error.message.includes('Only the account owner') ? 403 : 500;
            res.status(statusCode).json({ success: false, error: error.message });
        }
    }

    /**
     * Delete a doctor under owner account
     */
    @Delete('team/:doctorUserId')
    @Middleware(auth)
    async deleteDoctor(req: Request, res: Response): Promise<void> {
        try {
            const userId = (req as AuthRequest).user.userId;
            const { doctorUserId } = req.params;
            const result = await DoctorManagerService.removeDoctor(userId, doctorUserId);
            res.json({ success: true, message: result.message });
        } catch (error: any) {
            const statusCode = error.message.includes('Only the account owner') ? 403 :
                error.message.includes('cannot be deleted') ? 403 :
                    error.message.includes('not found') ? 404 :
                        error.message.includes('required') ? 400 : 500;
            res.status(statusCode).json({ success: false, error: error.message });
        }
    }

    /**
     * Mock upgrade for testing
     */
    @Post('upgrade-limit')
    @Middleware(auth)
    async upgradeLimit(req: Request, res: Response): Promise<void> {
        try {
            const userId = (req as AuthRequest).user.userId;
            const { newLimit } = req.body;
            const result = await DoctorManagerService.upgradeLimit(userId, newLimit);
            res.json({ success: true, message: result.message });
        } catch (error: any) {
            const statusCode = error.message.includes('not found') ? 404 : 500;
            res.status(statusCode).json({ success: false, error: error.message });
        }
    }
}
