"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = characterRoutes;
const reference_pipeline_js_1 = require("../modules/character-engine/reference-pipeline.js");
const file_ops_js_1 = require("../modules/data-store/file-ops.js");
async function characterRoutes(app, opts) {
    // Use injected modules or fall back to singleton instances
    const { characterEngine: ceFromModule } = await import('../modules/character-engine/index.js');
    const { dataStore: dsFromModule } = await import('../modules/data-store/index.js');
    const engine = opts.characterEngine ?? ceFromModule;
    const store = opts.dataStore ?? dsFromModule;
    // GET /characters  List all characters
    app.get('/characters', async (_request, reply) => {
        try {
            const characters = await engine.listCharacters();
            return reply.status(200).send({ success: true, data: characters });
        }
        catch (err) {
            return reply.status(500).send({ success: false, error: err.message });
        }
    });
    // POST /characters  Create new character
    app.post('/characters', async (request, reply) => {
        try {
            const body = request.body;
            if (!body || !body.dna) {
                return reply.status(400).send({ success: false, error: 'Request body must include "dna" object' });
            }
            const character = await engine.createCharacter(body.dna, body.profile);
            return reply.status(201).send({ success: true, data: character });
        }
        catch (err) {
            return reply.status(400).send({ success: false, error: err.message });
        }
    });
    // GET /characters/:id  Get character detail
    app.get('/characters/:id', async (request, reply) => {
        try {
            const { id } = request.params;
            const character = await engine.getCharacter(id);
            if (!character) {
                return reply.status(404).send({ success: false, error: 'Character not found' });
            }
            return reply.status(200).send({ success: true, data: character });
        }
        catch (err) {
            return reply.status(500).send({ success: false, error: err.message });
        }
    });
    // PUT /characters/:id  Update character DNA
    app.put('/characters/:id', async (request, reply) => {
        try {
            const { id } = request.params;
            const updates = request.body;
            if (!updates) {
                return reply.status(400).send({ success: false, error: 'Request body is required' });
            }
            const character = await engine.updateCharacter(id, updates);
            return reply.status(200).send({ success: true, data: character });
        }
        catch (err) {
            if (err.message?.includes('not found')) {
                return reply.status(404).send({ success: false, error: err.message });
            }
            return reply.status(400).send({ success: false, error: err.message });
        }
    });
    // DELETE /characters/:id  Delete character
    app.delete('/characters/:id', async (request, reply) => {
        try {
            const { id } = request.params;
            const deleted = await engine.deleteCharacter(id);
            if (!deleted) {
                return reply.status(404).send({ success: false, error: 'Character not found' });
            }
            return reply.status(200).send({ success: true, data: { deleted: true } });
        }
        catch (err) {
            return reply.status(500).send({ success: false, error: err.message });
        }
    });
    // POST /characters/:id/clone  Clone character
    app.post('/characters/:id/clone', async (request, reply) => {
        try {
            const { id } = request.params;
            const overrides = request.body || {};
            const cloned = await engine.cloneCharacter(id, overrides);
            return reply.status(201).send({ success: true, data: cloned });
        }
        catch (err) {
            if (err.message?.includes('not found')) {
                return reply.status(404).send({ success: false, error: err.message });
            }
            return reply.status(400).send({ success: false, error: err.message });
        }
    });
    // POST /characters/:id/reference-images  Regenerate reference images
    app.post('/characters/:id/reference-images', async (request, reply) => {
        try {
            const { id } = request.params;
            const character = await engine.getCharacter(id);
            if (!character) {
                return reply.status(404).send({ success: false, error: 'Character not found' });
            }
            const taskIds = await (0, reference_pipeline_js_1.generateReferenceImages)(id, character);
            return reply.status(201).send({ success: true, data: { taskIds } });
        }
        catch (err) {
            return reply.status(500).send({ success: false, error: err.message });
        }
    });
    // POST /characters/:id/reference-images/regenerate  Regenerate reference images with backup
    app.post('/characters/:id/reference-images/regenerate', async (request, reply) => {
        try {
            const { id } = request.params;
            const dna = await engine.getCharacter(id);
            if (!dna) {
                return reply.status(404).send({ success: false, error: 'Character not found' });
            }
            const result = await (0, reference_pipeline_js_1.regenerateReferenceImages)(id, dna);
            return reply.status(200).send({ success: true, data: result });
        }
        catch (err) {
            return reply.status(500).send({ success: false, error: err.message });
        }
    });
    // GET /characters/:id/reference-images  Get reference images
    app.get('/characters/:id/reference-images', async (request, reply) => {
        try {
            const { id } = request.params;
            const character = await engine.getCharacter(id);
            if (!character) {
                return reply.status(404).send({ success: false, error: 'Character not found' });
            }
            const dir = store.getReferenceImagesDir(id);
            const files = await (0, file_ops_js_1.listFiles)(dir);
            return reply.status(200).send({ success: true, data: { characterId: id, images: files } });
        }
        catch (err) {
            return reply.status(500).send({ success: false, error: err.message });
        }
    });
    // PUT /characters/:id/conversation-profile  Update conversation profile
    app.put('/characters/:id/conversation-profile', async (request, reply) => {
        try {
            const { id } = request.params;
            const character = await engine.getCharacter(id);
            if (!character) {
                return reply.status(404).send({ success: false, error: 'Character not found' });
            }
            const profileData = request.body;
            if (!profileData) {
                return reply.status(400).send({ success: false, error: 'Request body is required' });
            }
            const profile = { characterId: id, ...profileData };
            await store.saveConversationProfile(profile);
            return reply.status(200).send({ success: true, data: profile });
        }
        catch (err) {
            return reply.status(500).send({ success: false, error: err.message });
        }
    });
    // GET /characters/:id/conversation-profile  Get conversation profile
    app.get('/characters/:id/conversation-profile', async (request, reply) => {
        try {
            const { id } = request.params;
            const character = await engine.getCharacter(id);
            if (!character) {
                return reply.status(404).send({ success: false, error: 'Character not found' });
            }
            const profile = await store.getConversationProfile(id);
            if (!profile) {
                return reply.status(404).send({ success: false, error: 'Conversation profile not found' });
            }
            return reply.status(200).send({ success: true, data: profile });
        }
        catch (err) {
            return reply.status(500).send({ success: false, error: err.message });
        }
    });
    // POST /characters/:id/activate  Activate character
    app.post('/characters/:id/activate', async (request, reply) => {
        try {
            const { id } = request.params;
            const character = await engine.activateCharacter(id);
            return reply.status(200).send({ success: true, data: character });
        }
        catch (err) {
            if (err.message?.includes('not found')) {
                return reply.status(404).send({ success: false, error: err.message });
            }
            return reply.status(500).send({ success: false, error: err.message });
        }
    });
    // POST /characters/:id/deactivate  Deactivate character
    app.post('/characters/:id/deactivate', async (request, reply) => {
        try {
            const { id } = request.params;
            const character = await engine.deactivateCharacter(id);
            return reply.status(200).send({ success: true, data: character });
        }
        catch (err) {
            if (err.message?.includes('not found')) {
                return reply.status(404).send({ success: false, error: err.message });
            }
            return reply.status(500).send({ success: false, error: err.message });
        }
    });
    // GET /characters/:id/api-credentials  Get character API credentials
    app.get('/characters/:id/api-credentials', async (request, reply) => {
        try {
            const { id } = request.params;
            const character = await store.getCharacter(id);
            if (!character) {
                return reply.status(404).send({ success: false, error: 'Character not found' });
            }
            return reply.status(200).send({
                success: true,
                data: character.apiCredentials ?? null,
            });
        }
        catch (err) {
            return reply.status(500).send({ success: false, error: err.message });
        }
    });
    // PUT /characters/:id/api-credentials  Update character API credentials
    app.put('/characters/:id/api-credentials', async (request, reply) => {
        try {
            const { id } = request.params;
            const body = request.body;
            if (!body ||
                typeof body.telegramApiId !== 'number' ||
                typeof body.telegramApiHash !== 'string' ||
                !body.telegramApiHash.trim()) {
                return reply.status(400).send({
                    success: false,
                    error: 'Request body must include "telegramApiId" (number) and "telegramApiHash" (non-empty string)',
                });
            }
            const character = await store.getCharacter(id);
            if (!character) {
                return reply.status(404).send({ success: false, error: 'Character not found' });
            }
            character.apiCredentials = {
                telegramApiId: body.telegramApiId,
                telegramApiHash: body.telegramApiHash.trim(),
            };
            character.updatedAt = new Date().toISOString();
            await store.saveCharacter(character);
            return reply.status(200).send({ success: true, data: character.apiCredentials });
        }
        catch (err) {
            return reply.status(500).send({ success: false, error: err.message });
        }
    });
}
//# sourceMappingURL=characters.js.map