import { pool } from '../config/database';
import { RowDataPacket } from 'mysql2';

export interface Role extends RowDataPacket {
  id: number;
  name: string;
  description: string;
}

export const findRoleByName = async (name: string, connection?: any): Promise<Role | null> => {
  const db = connection || pool;
  const [rows] = await (db as any).query('SELECT * FROM roles WHERE name = ?', [name]);
  return rows[0] || null;
};

export const findRoleById = async (id: number): Promise<Role | null> => {
  const [rows] = await pool.query<Role[]>('SELECT * FROM roles WHERE id = ?', [id]);
  return rows[0] || null;
};
