import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import { PromptController } from '../controllers/PromptController';
import { authMiddleware } from '../middleware/auth.middleware';
import { PromptType } from '../entities/Prompt';

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

interface PromptParams {
    id: string;
}

interface AppParams {
    appId: string;
}

const createPromptSchema = {
    body: {
        type: 'object',
        required: ['content', 'type', 'appId'],
        properties: {
            content: { type: 'string' },
            type: { type: 'string', enum: Object.values(PromptType) },
            appId: { type: 'number' },
            metadata: { type: 'object' }
        }
    }
};

const promptParamsSchema = {
    params: {
        type: 'object',
        required: ['id'],
        properties: {
            id: { type: 'string' }
        }
    }
};

const appParamsSchema = {
    params: {
        type: 'object',
        required: ['appId'],
        properties: {
            appId: { type: 'string' }
        }
    }
};

export default async function promptRoutes(fastify: FastifyInstance) {
    const promptController = new PromptController(fastify.orm.em);

    // Create a new prompt
    fastify.post<{ Body: CreatePromptBody }>(
        '/prompts',
        {
            schema: createPromptSchema,
            preHandler: authMiddleware
        },
        async (request, reply) => {
            return promptController.createPrompt(request, reply);
        }
    );

    // Process a prompt
    fastify.post<{ Params: PromptParams }>(
        '/prompts/:id/process',
        {
            schema: promptParamsSchema,
            preHandler: authMiddleware
        },
        async (request, reply) => {
            return promptController.processPrompt(request, reply);
        }
    );

    // Get a prompt by ID
    fastify.get<{ Params: PromptParams }>(
        '/prompts/:id',
        {
            schema: promptParamsSchema,
            preHandler: authMiddleware
        },
        async (request, reply) => {
            return promptController.getPrompt(request, reply);
        }
    );

    // Get all prompts for the current user
    fastify.get(
        '/prompts',
        {
            preHandler: authMiddleware
        },
        async (request, reply) => {
            return promptController.getUserPrompts(request, reply);
        }
    );

    // Get all prompts for a specific app
    fastify.get<{ Params: AppParams }>(
        '/apps/:appId/prompts',
        {
            schema: appParamsSchema,
            preHandler: authMiddleware
        },
        async (request, reply) => {
            return promptController.getAppPrompts(request, reply);
        }
    );
} 