import pool from '../db';
import { Role } from '@app/shared/types/roles'

export async function assignUserGroup(userID: number, roleID: number) {

    const sql = `INSERT INTO members_roles (member_id, role_id) VALUES (?, ?);`;
    const params = [userID, roleID];

    return await pool.query(sql, params);
}

export async function createGroup(name: string, color: string, description: string) {
    const sql = `INSERT INTO roles (name, color, description) VALUES (?, ?, ?)`;
    const params = [name, color, description];

    const result = await pool.query(sql, params);
    return { id: result.insertId, name, color, description };
}

export async function getUserRoles(userID: number): Promise<Role[]> {
    const sql = `SELECT r.id, r.name
        FROM members_roles mr
        INNER JOIN roles r ON mr.role_id = r.id
        WHERE mr.member_id = 190;`;

    return await pool.query(sql, [userID]);
}