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

interface PromptMetadata {
    endpoints?: Array<{
        path: string;
        method: string;
        statusCodes: number[];
    }>;
    [key: string]: any;
}

interface ProcessedResult {
    endpoints?: Array<{
        path: string;
        method: string;
        statusCodes: number[];
        description?: string;
    }>;
    [key: string]: any;
}

export class PromptService {
    constructor(private readonly em: EntityManager) {}

    async createPrompt(
        content: string,
        type: PromptType,
        user: User,
        app: App,
        metadata: PromptMetadata = {}
    ): Promise<Prompt> {
        const prompt = new Prompt();
        prompt.content = content;
        prompt.type = type;
        prompt.user = user;
        prompt.app = app;
        prompt.metadata = metadata;
        prompt.status = PromptStatus.PENDING;

        await this.em.persistAndFlush(prompt);
        return prompt;
    }

    async processPrompt(id: number): Promise<Prompt> {
        const prompt = await this.getPromptById(id);
        if (!prompt) {
            throw new Error('Prompt not found');
        }

        try {
            prompt.status = PromptStatus.PROCESSING;
            await this.em.flush();

            const result = await this.processPromptByType(prompt);

            prompt.processedResult = result;
            prompt.status = PromptStatus.COMPLETED;
            await this.em.flush();

            return prompt;
        } catch (error) {
            prompt.status = PromptStatus.FAILED;
            prompt.errorMessage = error instanceof Error ? error.message : 'Unknown error';
            await this.em.flush();
            throw error;
        }
    }

    private async processPromptByType(prompt: Prompt): Promise<ProcessedResult> {
        switch (prompt.type) {
            case PromptType.API_DOCUMENTATION:
                return this.processApiDocumentation(prompt);
            case PromptType.ENDPOINT_ANALYSIS:
                return this.processEndpointAnalysis(prompt);
            case PromptType.SCHEMA_GENERATION:
                return this.processSchemaGeneration(prompt);
            case PromptType.CUSTOM:
                return this.processCustomPrompt(prompt);
            default:
                throw new Error('Invalid prompt type');
        }
    }

    private async processApiDocumentation(prompt: Prompt): Promise<ProcessedResult> {
        // TODO: Implement API documentation processing
        return {};
    }

    private async processEndpointAnalysis(prompt: Prompt): Promise<ProcessedResult> {
        // TODO: Implement endpoint analysis processing
        return {};
    }

    private async processSchemaGeneration(prompt: Prompt): Promise<ProcessedResult> {
        // TODO: Implement schema generation processing
        return {};
    }

    private async processCustomPrompt(prompt: Prompt): Promise<ProcessedResult> {
        // TODO: Implement custom prompt processing
        return {};
    }

    async getPromptById(id: number): Promise<Prompt | null> {
        return this.em.findOne(Prompt, { id });
    }

    async getPromptsByUser(userId: number): Promise<Prompt[]> {
        return this.em.find(Prompt, { user: { id: userId } });
    }

    async getPromptsByApp(appId: number): Promise<Prompt[]> {
        return this.em.find(Prompt, { app: { id: appId } });
    }

    async getPromptsByStatus(status: PromptStatus): Promise<Prompt[]> {
        return this.em.find(Prompt, { status });
    }
} 