import { Controller, Get, Post } from '@overnightjs/core';
import { Request, Response } from 'express';
import SurveyService from '../services/SurveyService';

@Controller('api/surveys')
export class SurveyController {

    @Post()
    async createSurvey(req: Request, res: Response): Promise<void> {
        try {
            const survey = await SurveyService.create(req.body);
            res.status(201).json({ success: true, data: survey });
        } catch (error: any) {
            console.error('Create survey error:', error);
            const statusCode = error.message.includes('not found') ? 404 : 400;
            res.status(statusCode).json({ success: false, error: error.message });
        }
    }

    @Get()
    async getSurveys(req: Request, res: Response): Promise<void> {
        try {
            const surveys = await SurveyService.getAllSurveys();
            res.json({ success: true, data: surveys });
        } catch (error: any) {
            res.status(500).json({ success: false, error: error.message });
        }
    }

    @Get('patient/:patientId')
    async getPatientSurveys(req: Request, res: Response): Promise<void> {
        try {
            const { patientId } = req.params;
            const surveys = await SurveyService.getByPatientId(patientId);
            res.json({ success: true, data: surveys });
        } catch (error: any) {
            res.status(500).json({ success: false, error: error.message });
        }
    }
}
