"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dataStore = exports.DataStore = void 0;
const path_1 = require("path");
const fs_1 = require("fs");
const config_js_1 = require("../../utils/config.js");
const logger_js_1 = require("../../utils/logger.js");
const file_ops_js_1 = require("./file-ops.js");
const log = (0, logger_js_1.createModuleLogger)('data-store');
class DataStore {
    dataDir;
    constructor(dataDir) {
        this.dataDir = dataDir || config_js_1.config.dataDir;
    }
    async init() {
        const dirs = ['config', 'characters', 'templates', 'queue', 'analytics', 'stars', 'translations', 'logs', 'backups'];
        for (const dir of dirs) {
            await (0, file_ops_js_1.ensureDir)((0, path_1.resolve)(this.dataDir, dir));
        }
        log.info('Data store initialized');
    }
    // --- Character Operations ---
    characterDir(id) {
        return (0, path_1.resolve)(this.dataDir, 'characters', id);
    }
    async saveCharacter(dna) {
        const dir = this.characterDir(dna.id);
        await (0, file_ops_js_1.ensureDir)(dir);
        await (0, file_ops_js_1.ensureDir)((0, path_1.resolve)(dir, 'reference-images'));
        await (0, file_ops_js_1.ensureDir)((0, path_1.resolve)(dir, 'generated-images'));
        await (0, file_ops_js_1.ensureDir)((0, path_1.resolve)(dir, 'quality-reports'));
        await (0, file_ops_js_1.ensureDir)((0, path_1.resolve)(dir, 'conversations'));
        await (0, file_ops_js_1.atomicWriteJSON)((0, path_1.resolve)(dir, 'dna.json'), dna);
    }
    async getCharacter(id) {
        return (0, file_ops_js_1.readJSON)((0, path_1.resolve)(this.characterDir(id), 'dna.json'));
    }
    async listCharacters() {
        const dirs = await (0, file_ops_js_1.listDirectories)((0, path_1.resolve)(this.dataDir, 'characters'));
        const characters = [];
        for (const dir of dirs) {
            const dna = await this.getCharacter(dir);
            if (dna)
                characters.push(dna);
        }
        return characters;
    }
    async deleteCharacter(id) {
        const dir = this.characterDir(id);
        if (!(0, file_ops_js_1.fileExists)(dir))
            return false;
        (0, fs_1.rmSync)(dir, { recursive: true, force: true });
        return true;
    }
    // --- Master Prompt ---
    async saveMasterPrompt(prompt) {
        await (0, file_ops_js_1.atomicWriteJSON)((0, path_1.resolve)(this.characterDir(prompt.characterId), 'master-prompt.json'), prompt);
    }
    async getMasterPrompt(characterId) {
        return (0, file_ops_js_1.readJSON)((0, path_1.resolve)(this.characterDir(characterId), 'master-prompt.json'));
    }
    // --- Conversation Profile ---
    async saveConversationProfile(profile) {
        await (0, file_ops_js_1.atomicWriteJSON)((0, path_1.resolve)(this.characterDir(profile.characterId), 'conversation-profile.json'), profile);
    }
    async getConversationProfile(characterId) {
        return (0, file_ops_js_1.readJSON)((0, path_1.resolve)(this.characterDir(characterId), 'conversation-profile.json'));
    }
    // --- Conversation Persona ---
    async saveConversationPersona(characterId, persona) {
        await (0, file_ops_js_1.atomicWriteJSON)((0, path_1.resolve)(this.characterDir(characterId), 'conversation-persona.json'), persona);
    }
    async getConversationPersona(characterId) {
        return (0, file_ops_js_1.readJSON)((0, path_1.resolve)(this.characterDir(characterId), 'conversation-persona.json'));
    }
    // --- Conversation History ---
    async saveConversationHistory(history) {
        const dir = (0, path_1.resolve)(this.characterDir(history.characterId), 'conversations');
        await (0, file_ops_js_1.ensureDir)(dir);
        await (0, file_ops_js_1.atomicWriteJSON)((0, path_1.resolve)(dir, `${history.telegramUserId}.json`), history);
    }
    async getConversationHistory(characterId, userId) {
        return (0, file_ops_js_1.readJSON)((0, path_1.resolve)(this.characterDir(characterId), 'conversations', `${userId}.json`));
    }
    async listConversations(characterId) {
        const dir = (0, path_1.resolve)(this.characterDir(characterId), 'conversations');
        const files = await (0, file_ops_js_1.listFiles)(dir, '.json');
        const histories = [];
        for (const file of files) {
            const h = await (0, file_ops_js_1.readJSON)((0, path_1.resolve)(dir, file));
            if (h)
                histories.push(h);
        }
        return histories;
    }
    async deleteConversation(characterId, userId) {
        return (0, file_ops_js_1.deleteFile)((0, path_1.resolve)(this.characterDir(characterId), 'conversations', `${userId}.json`));
    }
    // --- Stars Transactions ---
    async saveStarsTransaction(tx) {
        const dir = (0, path_1.resolve)(this.dataDir, 'stars', tx.characterId);
        await (0, file_ops_js_1.ensureDir)(dir);
        const txFile = (0, path_1.resolve)(dir, 'transactions.json');
        const existing = await (0, file_ops_js_1.readJSON)(txFile) || [];
        existing.push(tx);
        await (0, file_ops_js_1.atomicWriteJSON)(txFile, existing);
    }
    async getStarsTransactions(characterId) {
        return await (0, file_ops_js_1.readJSON)((0, path_1.resolve)(this.dataDir, 'stars', characterId, 'transactions.json')) || [];
    }
    // --- Scene Templates ---
    async saveTemplate(template) {
        await (0, file_ops_js_1.atomicWriteJSON)((0, path_1.resolve)(this.dataDir, 'templates', `${template.id}.json`), template);
    }
    async getTemplate(id) {
        return (0, file_ops_js_1.readJSON)((0, path_1.resolve)(this.dataDir, 'templates', `${id}.json`));
    }
    async listTemplates() {
        const files = await (0, file_ops_js_1.listFiles)((0, path_1.resolve)(this.dataDir, 'templates'), '.json');
        const templates = [];
        for (const file of files) {
            const t = await (0, file_ops_js_1.readJSON)((0, path_1.resolve)(this.dataDir, 'templates', file));
            if (t)
                templates.push(t);
        }
        return templates;
    }
    async deleteTemplate(id) {
        return (0, file_ops_js_1.deleteFile)((0, path_1.resolve)(this.dataDir, 'templates', `${id}.json`));
    }
    // --- Reference / Generated Images Paths ---
    getReferenceImagesDir(characterId) {
        return (0, path_1.resolve)(this.characterDir(characterId), 'reference-images');
    }
    getGeneratedImagesDir(characterId) {
        return (0, path_1.resolve)(this.characterDir(characterId), 'generated-images');
    }
    getQualityReportsDir(characterId) {
        return (0, path_1.resolve)(this.characterDir(characterId), 'quality-reports');
    }
    // --- Reference Image URLs ---
    async saveReferenceImageUrls(characterId, urls) {
        const dir = this.characterDir(characterId);
        await (0, file_ops_js_1.ensureDir)(dir);
        await (0, file_ops_js_1.atomicWriteJSON)((0, path_1.resolve)(dir, 'reference-urls.json'), urls);
    }
    async getReferenceImageUrls(characterId) {
        return (0, file_ops_js_1.readJSON)((0, path_1.resolve)(this.characterDir(characterId), 'reference-urls.json'));
    }
    async addReferenceImageUrl(characterId, filename, url) {
        const existing = await this.getReferenceImageUrls(characterId) ?? {};
        existing[filename] = url;
        await this.saveReferenceImageUrls(characterId, existing);
    }
    // --- Channel Config ---
    async saveCharacterChannelConfig(characterId, config) {
        const dir = this.characterDir(characterId);
        await (0, file_ops_js_1.ensureDir)(dir);
        await (0, file_ops_js_1.atomicWriteJSON)((0, path_1.resolve)(dir, 'channel-config.json'), config);
    }
    async getCharacterChannelConfig(characterId) {
        return (0, file_ops_js_1.readJSON)((0, path_1.resolve)(this.characterDir(characterId), 'channel-config.json'));
    }
    // --- Telegram Entities ---
    async getTelegramEntities(characterId) {
        return await (0, file_ops_js_1.readJSON)((0, path_1.resolve)(this.characterDir(characterId), 'telegram-entities.json')) || [];
    }
}
exports.DataStore = DataStore;
exports.dataStore = new DataStore();
//# sourceMappingURL=index.js.map