import { Controller, Middleware, Post } from '@overnightjs/core';
import { Request, Response } from 'express';
import { upload } from '../middleware/upload';
import UploadService from '../services/UploadService';

@Controller('api/upload')
export class UploadController {

    @Post('patient-image')
    @Middleware(upload.single('image'))
    async uploadPatientImage(req: Request, res: Response) {
        try {
            if (!req.file) {
                return res.status(400).json({ success: false, error: 'No file uploaded' });
            }

            const result = await UploadService.uploadPatientImage(
                req.file.path
            );

            return res.status(200).json({
                success: true,
                message: result.message,
                filePath: result.filePath
            });
        } catch (error: any) {
            console.error('Upload error:', error);
            return res.status(400).json({ success: false, error: error.message });
        }
    }

    @Post('profile-image')
    @Middleware(upload.single('profileImage'))
    async uploadProfileImage(req: Request, res: Response) {
        try {
            if (!req.file) {
                return res.status(400).json({ success: false, error: 'No file uploaded' });
            }

            const { userId } = req.body;

            const result = await UploadService.uploadProfileImage({
                userId,
                filePath: req.file.path
            });

            return res.status(200).json({
                success: true,
                message: result.message,
                filePath: result.filePath,
                user: result.user
            });
        } catch (error: any) {
            console.error('Upload error:', error);
            const statusCode = error.message.includes('not found') ? 404 : 400;
            return res.status(statusCode).json({ success: false, error: error.message });
        }
    }

    @Post('signature')
    @Middleware(upload.single('signature'))
    async uploadSignature(req: Request, res: Response) {
        try {
            if (!req.file) {
                return res.status(400).json({ success: false, error: 'No file uploaded' });
            }

            const { userId } = req.body;

            const result = await UploadService.uploadSignature({
                userId,
                filePath: req.file.path
            });

            return res.status(200).json({
                success: true,
                message: result.message,
                filePath: result.filePath,
                doctor: result.doctor
            });
        } catch (error: any) {
            console.error('Upload error:', error);
            const statusCode = error.message.includes('not found') ? 404 : 400;
            return res.status(statusCode).json({ success: false, error: error.message });
        }
    }
    @Post('clinical-record-file')
    @Middleware(upload.single('file'))
    async uploadClinicalFile(req: Request, res: Response) {
        try {
            if (!req.file) {
                return res.status(400).json({ success: false, error: 'No file uploaded' });
            }

            const result = await UploadService.uploadClinicalFile(
                req.file.path,
                req.file.originalname
            );

            return res.status(200).json({
                success: true,
                message: result.message,
                filePath: result.filePath,
                originalName: result.originalName
            });
        } catch (error: any) {
            console.error('Upload error:', error);
            return res.status(400).json({ success: false, error: error.message });
        }
    }
}
