import express = require('express');
const statusR = express.Router();
const memberStatusR = express.Router();

import pool from '../db';
import { requireLogin } from '../middleware/auth';

statusR.use(requireLogin);
memberStatusR.use(requireLogin);

//insert a new latest rank for a user
memberStatusR.post('/', async (req, res) => {
    // try {
    //     const App = req.body?.App || {};

    //     // TODO: replace with current user ID
    //     const memberId = 1;

    //     const sql = `INSERT INTO applications (member_id, app_version, app_data) VALUES (?, ?, ?);`;
    //     const appVersion = 1;

    //     const params = [memberId, appVersion, JSON.stringify(App)]

    //     console.log(params)

    //     await pool.query(sql, params);

    //     res.sendStatus(201);
    // } catch (err) {
    //     console.error('Insert failed:', err);
    //     res.status(500).json({ error: 'Failed to save application' });
    // }
    res.status(501).json({ error: 'Not implemented' });
});

//get all statuses
statusR.get('/', async (req, res) => {
    try {
        const result = await pool.query('SELECT * FROM statuses;');
        res.json(result);
    } catch (err) {
        console.error(err);
        res.status(500).json({ error: 'Internal server error' });
    }
});

export const status = statusR;
export const memberStatus = memberStatusR;


// TODO, implement get all ranks route with SQL stirng SELECT id, name, short_name, category, sort_id FROM ranks;