/**
 * مدیریت file_id استیکرها
 */
const fs = require('fs');
const { STICKERS_FILE } = require('./config');

function loadStickers() {
    if (fs.existsSync(STICKERS_FILE)) {
        try {
            const data = fs.readFileSync(STICKERS_FILE, 'utf8');
            return JSON.parse(data);
        } catch (error) {
            console.error('خطا در خواندن فایل استیکرها:', error);
            return {
                success: [],
                lazy: null,
                scenario1: null,
                scenario2_yes: null,
                scenario2_no: null
            };
        }
    }
    return {
        success: [],
        lazy: null,
        scenario1: null,
        scenario2_yes: null,
        scenario2_no: null
    };
}

function saveStickers(stickers) {
    try {
        fs.writeFileSync(STICKERS_FILE, JSON.stringify(stickers, null, 4), 'utf8');
    } catch (error) {
        console.error('خطا در ذخیره فایل استیکرها:', error);
    }
}

function addSuccessSticker(fileId) {
    const stickers = loadStickers();
    if (!stickers.success.includes(fileId)) {
        stickers.success.push(fileId);
        saveStickers(stickers);
    }
}

function setLazySticker(fileId) {
    const stickers = loadStickers();
    stickers.lazy = fileId;
    saveStickers(stickers);
}

function setScenario1Sticker(fileId) {
    const stickers = loadStickers();
    stickers.scenario1 = fileId;
    saveStickers(stickers);
}

function setScenario2YesSticker(fileId) {
    const stickers = loadStickers();
    stickers.scenario2_yes = fileId;
    saveStickers(stickers);
}

function setScenario2NoSticker(fileId) {
    const stickers = loadStickers();
    stickers.scenario2_no = fileId;
    saveStickers(stickers);
}

function getRandomSuccessSticker() {
    const stickers = loadStickers();
    if (stickers.success.length > 0) {
        return stickers.success[Math.floor(Math.random() * stickers.success.length)];
    }
    // استیکرهای پیشفرض اگر هیچ استیکری تنظیم نشده
    return 'stik/3.webp';
}

function getLazySticker() {
    const stickers = loadStickers();
    return stickers.lazy || 'stik/6.webp';
}

function getScenario1Sticker() {
    const stickers = loadStickers();
    return stickers.scenario1 || 'stik/1.webp';
}

function getScenario2YesSticker() {
    const stickers = loadStickers();
    return stickers.scenario2_yes || 'stik/2.webp';
}

function getScenario2NoSticker() {
    const stickers = loadStickers();
    return stickers.scenario2_no || 'stik/5.webp';
}

module.exports = {
    addSuccessSticker,
    setLazySticker,
    setScenario1Sticker,
    setScenario2YesSticker,
    setScenario2NoSticker,
    getRandomSuccessSticker,
    getLazySticker,
    getScenario1Sticker,
    getScenario2YesSticker,
    getScenario2NoSticker,
    loadStickers
};

