import { FastifyRequest, FastifyReply } from 'fastify';
import { EntityManager } from '@mikro-orm/core';
import { PromptService } from '../services/PromptService';
import { PromptType } from '../entities/Prompt';
import { App } from '../entities/app/_App';
import { User } from '../entities/user/_User';

interface CreatePromptBody {
    content: string;
    type: PromptType;
    appId: number;
    metadata?: Record<string, any>;
}

interface PromptParams {
    id: string;
}

interface AppParams {
    appId: string;
}

export class PromptController {
    private promptService: PromptService;

    constructor(private readonly em: EntityManager) {
        this.promptService = new PromptService(em);
    }

    async createPrompt(
        request: FastifyRequest<{ Body: CreatePromptBody }>,
        reply: FastifyReply
    ) {
        try {
            const { content, type, appId, metadata } = request.body;
            const user = request.user as User;
            const app = await this.em.findOne(App, { id: appId });

            if (!app) {
                return reply.status(404).send({ error: 'App not found' });
            }

            const prompt = await this.promptService.createPrompt(
                content,
                type,
                user,
                app,
                metadata
            );

            return reply.status(201).send(prompt);
        } catch (error) {
            return reply.status(500).send({ error: 'Failed to create prompt' });
        }
    }

    async processPrompt(
        request: FastifyRequest<{ Params: PromptParams }>,
        reply: FastifyReply
    ) {
        try {
            const id = parseInt(request.params.id, 10);

            if (isNaN(id)) {
                return reply.status(400).send({ error: 'Invalid prompt ID' });
            }

            const prompt = await this.promptService.processPrompt(id);
            return reply.send(prompt);
        } catch (error) {
            return reply.status(500).send({ error: 'Failed to process prompt' });
        }
    }

    async getPrompt(
        request: FastifyRequest<{ Params: PromptParams }>,
        reply: FastifyReply
    ) {
        try {
            const id = parseInt(request.params.id, 10);

            if (isNaN(id)) {
                return reply.status(400).send({ error: 'Invalid prompt ID' });
            }

            const prompt = await this.promptService.getPromptById(id);
            if (!prompt) {
                return reply.status(404).send({ error: 'Prompt not found' });
            }

            return reply.send(prompt);
        } catch (error) {
            return reply.status(500).send({ error: 'Failed to get prompt' });
        }
    }

    async getUserPrompts(request: FastifyRequest, reply: FastifyReply) {
        try {
            const user = request.user as User;
            const prompts = await this.promptService.getPromptsByUser(user.id);
            return reply.send(prompts);
        } catch (error) {
            return reply.status(500).send({ error: 'Failed to get user prompts' });
        }
    }

    async getAppPrompts(
        request: FastifyRequest<{ Params: AppParams }>,
        reply: FastifyReply
    ) {
        try {
            const appId = parseInt(request.params.appId, 10);

            if (isNaN(appId)) {
                return reply.status(400).send({ error: 'Invalid app ID' });
            }

            const prompts = await this.promptService.getPromptsByApp(appId);
            return reply.send(prompts);
        } catch (error) {
            return reply.status(500).send({ error: 'Failed to get app prompts' });
        }
    }
} 