import { BaseSchema } from '@adonisjs/lucid/schema';

/**
 * Migration: Seed new control settings for ScammerGuard
 * - auto_reply_group_only: Only reply in groups
 * - auto_reply_private_only: Only reply in private chats
 * - auto_reply_cooldown_sec: Cooldown between replies per chat (seconds)
 * - greeting_leave_enabled: Send farewell message when member leaves
 * - greeting_leave_prompt: Custom farewell prompt
 * - scammer_notify_jid: JID to forward scammer detection alerts
 */
export default class extends BaseSchema {
    tableName = 'scammer_settings';

    async up() {
        const now = new Date().toISOString();
        const defaults = [
            { key: 'auto_reply_group_only',    value: '0' },
            { key: 'auto_reply_private_only',  value: '0' },
            { key: 'auto_reply_cooldown_sec',  value: '0' },
            { key: 'auto_reply_group_mode',    value: 'all' },
            { key: 'auto_reply_groups',        value: '[]' },
            { key: 'greeting_leave_enabled',   value: '0' },
            { key: 'greeting_leave_prompt',    value: '' },
            { key: 'scammer_notify_jid',       value: '' },
        ];
        for (const setting of defaults) {
            const existing = await this.db.from('scammer_settings').where('key', setting.key).first();
            if (!existing) {
                await this.db.table('scammer_settings').insert({ key: setting.key, value: setting.value, created_at: now, updated_at: now });
            }
        }
    }

    async down() {
        await this.db.from('scammer_settings').whereIn('key', [
            'auto_reply_group_only', 'auto_reply_private_only', 'auto_reply_cooldown_sec',
            'greeting_leave_enabled', 'greeting_leave_prompt', 'scammer_notify_jid',
        ]).delete();
    }
}
