
import { storage } from '../server/storage';
import { getDatabase } from '../server/db';
import { systemSettings, users } from '../shared/schema';
import { eq } from 'drizzle-orm';

async function triggerMorningRoutine() {
    console.log("🔧 Configuring System for Morning Routine Trigger...");

    // 1. Enable Morning Routine and set time to 00:00 (so it's definitely 'past' start time)
    await storage.setSystemSettings('morning_routine_enabled', 'true');
    await storage.setSystemSettings('morning_routine_time', '00:00');
    // Disable evening to avoid conflict
    await storage.setSystemSettings('evening_routine_enabled', 'false');

    console.log("✅ System Settings Updated: Morning Enabled @ 00:00");

    // 2. Reset User's lastMorningRoutine
    const db = getDatabase();
    const allUsers = await db.select().from(users).limit(1);

    if (allUsers.length > 0) {
        const user = allUsers[0];
        console.log(`Resetting routine for user: ${user.username} (${user.id})`);

        // Update directly via DB to ensure it's null or old
        // Set to yesterday
        const yesterday = new Date();
        yesterday.setDate(yesterday.getDate() - 1);

        await storage.updateUser(user.id, { lastMorningRoutine: yesterday });
        console.log("✅ User Updated: lastMorningRoutine set to yesterday.");
        console.log("🚀 The app should now block navigation and show the Morning Routine wizard.");
    } else {
        console.error("❌ No users found to update.");
    }

    process.exit(0);
}

triggerMorningRoutine();
