import { CommandRiskLevel } from '../value-objects/command-risk-level.vo';

const FORBIDDEN_PATTERNS = [
  /rm\s+-rf\s+\/(?!tmp)/i,
  /mkfs/i,
  /dd\s+if=.*of=\/dev/i,
  /:(){ :\|:& };:/,
  /chmod\s+-R\s+777\s+\//i,
  />\s*\/dev\/sd[a-z]/i,
];

const HIGH_RISK_PATTERNS = [
  /systemctl\s+(stop|restart|disable)/i,
  /service\s+\w+\s+(stop|restart)/i,
  /docker\s+(rm|stop|kill)/i,
  /kubectl\s+delete/i,
  /DROP\s+(TABLE|DATABASE)/i,
  /rm\s+-rf/i,
  /reboot/i,
  /shutdown/i,
];

const LOW_RISK_WRITE_PATTERNS = [
  /echo\s+.*>/i,
  /tee\s/i,
  /sed\s+-i/i,
  /mv\s/i,
  /cp\s/i,
  /mkdir/i,
  /touch/i,
  /docker\s+(run|exec|build)/i,
  /kubectl\s+(apply|create|scale)/i,
];

export class CommandRiskClassifier {
  classify(command: string): CommandRiskLevel {
    if (FORBIDDEN_PATTERNS.some(p => p.test(command))) {
      return CommandRiskLevel.FORBIDDEN;
    }
    if (HIGH_RISK_PATTERNS.some(p => p.test(command))) {
      return CommandRiskLevel.HIGH_RISK;
    }
    if (LOW_RISK_WRITE_PATTERNS.some(p => p.test(command))) {
      return CommandRiskLevel.LOW_RISK_WRITE;
    }
    return CommandRiskLevel.READ_ONLY;
  }
}
