import { PlagiarismReport } from "../types";

export async function detectPlagiarism(
  newCode: string,
  existingSubmissions: { studentName: string; rollNumber: string; code: string }[],
  problemStatement: string,
  language: string
): Promise<PlagiarismReport> {
  
  if (existingSubmissions.length === 0) {
    return {
      score: 0,
      status: 'Safe',
      explanation: "No other submissions found for comparison.",
      similarSubmissions: []
    };
  }

  // 🔥 SAFE MODE (NO AI)
  // Basic dummy comparison logic

  let highestScore = 0;
  let similarSubmissions: any[] = [];

  existingSubmissions.forEach(sub => {
    // simple similarity check (length + includes)
    let similarity = 0;

    if (newCode.trim() === sub.code.trim()) {
      similarity = 90;
    } else if (newCode.includes(sub.code.substring(0, 20))) {
      similarity = 50;
    } else {
      similarity = 10;
    }

    if (similarity > highestScore) {
      highestScore = similarity;
    }

    if (similarity > 30) {
      similarSubmissions.push({
        studentName: sub.studentName,
        rollNumber: sub.rollNumber,
        similarityScore: similarity,
        reason: "Basic similarity detected (AI disabled mode)"
      });
    }
  });

  let status: 'Safe' | 'Needs Review' | 'High Similarity' = 'Safe';

  if (highestScore >= 60) status = 'High Similarity';
  else if (highestScore >= 30) status = 'Needs Review';

  return {
    score: highestScore,
    status,
    explanation: "Basic plagiarism check (AI disabled mode)",
    similarSubmissions
  };
}