require('dotenv').config();
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const path = require('path');
const cron = require('node-cron');
const { runScrapers } = require('./scraper');

const app = express();
app.use(cors());
app.use(express.json());

const SECRETS_DIR = path.join(__dirname, '.secrets');
if (!fs.existsSync(SECRETS_DIR)) {
    fs.mkdirSync(SECRETS_DIR, { recursive: true });
}
const CREDENTIALS_FILE = path.join(SECRETS_DIR, 'utility_creds.json');

function getCredentials() {
    if (fs.existsSync(CREDENTIALS_FILE)) {
        return JSON.parse(fs.readFileSync(CREDENTIALS_FILE, 'utf8'));
    }
    return {};
}

function saveCredentials(creds) {
    fs.writeFileSync(CREDENTIALS_FILE, JSON.stringify(creds, null, 2));
}

app.get('/api/credentials', (req, res) => {
    const creds = getCredentials();
    // Don't send passwords back to the client for security
    res.json({
        success: true,
        firstEnergyUser: creds.firstEnergy ? creds.firstEnergy.username : '',
        peoplesGasUser: creds.peoplesGas ? creds.peoplesGas.username : ''
    });
});

app.post('/api/credentials', (req, res) => {
    const { firstEnergyUser, firstEnergyPass, peoplesGasUser, peoplesGasPass } = req.body;
    const creds = getCredentials();
    
    if (firstEnergyUser) {
        creds.firstEnergy = { username: firstEnergyUser, password: firstEnergyPass || creds.firstEnergy?.password };
    }
    if (peoplesGasUser) {
        creds.peoplesGas = { username: peoplesGasUser, password: peoplesGasPass || creds.peoplesGas?.password };
    }
    
    saveCredentials(creds);
    res.json({ success: true, message: 'Credentials saved securely.' });
});

app.post('/api/run', async (req, res) => {
    res.json({ success: true, message: 'Scraping started in background.' });
    runScrapers(getCredentials());
});

const PORT = process.env.PORT || 5005;
app.listen(PORT, '0.0.0.0', () => {
    console.log(`[Utility Agent] Server running on 0.0.0.0:${PORT}`);
    
    // Run daily at 11:30 PM (23:30)
    cron.schedule('30 23 * * *', () => {
        console.log('[Utility Agent] Running daily scheduled scrape...');
        runScrapers(getCredentials());
    });
});
