const express = require("express");
const router = express.Router();
const pool = require("../models/db");

// Add new account
router.post("/add", async (req, res) => {
    const { name, email, token, refresh_token, proxy } = req.body;
    try {
        await pool.query(
            "INSERT INTO accounts (name, email, token, refresh_token, proxy) VALUES (?, ?, ?, ?, ?)",
            [name, email, token, refresh_token, proxy]
        );
        res.status(201).json({ message: "Account added successfully!" });
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// Get all accounts
router.get("/", async (req, res) => {
    try {
        const [accounts] = await pool.query("SELECT * FROM accounts");
        res.json(accounts);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

module.exports = router;