File size: 11,719 Bytes
d2b7a80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// ── State ──────────────────────────────────────────────────────────────────
const state = { resume: null, jd: null };
//const API = 'http://localhost:8000'; // for localhost
const API = ''; //for nginx
let resumeMode = 'pdf'; // 'pdf' | 'text'
let jdMode = 'pdf';     // 'pdf' | 'text'

// Set pdf.js worker
pdfjsLib.GlobalWorkerOptions.workerSrc =
  'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js';

// ── PDF Extraction ─────────────────────────────────────────────────────────
async function extractTextFromPDF(file) {
  const arrayBuffer = await file.arrayBuffer();
  const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise;
  let fullText = '';
  for (let i = 1; i <= pdf.numPages; i++) {
    const page = await pdf.getPage(i);
    const content = await page.getTextContent();
    const pageText = content.items.map(item => item.str).join(' ');
    fullText += pageText + '\n';
  }
  return fullText.trim();
}

// ── File Handling ──────────────────────────────────────────────────────────
async function handleFile(type, file) {
  if (!file || file.type !== 'application/pdf') {
    setStatus(type, 'Only PDF files are supported.', true);
    return;
  }
  if (file.size > 10 * 1024 * 1024) {
    setStatus(type, 'File exceeds 10MB limit.', true);
    return;
  }

  const chipName = document.getElementById(type + 'ChipName');
  const chip = document.getElementById(type + 'Chip');
  chipName.textContent = file.name;
  chip.style.display = 'flex';

  document.getElementById(type + 'DropZone').classList.add('has-file');

  setStatus(type, '⏳ Extracting text from PDF...');
  try {
    const text = await extractTextFromPDF(file);
    if (!text || text.length < 30) {
      setStatus(type, '⚠️ Could not extract enough text. Is this a scanned PDF?', true);
      return;
    }
    state[type] = text;
    setStatus(type, `βœ“ Extracted ${text.length.toLocaleString()} characters from ${file.name}`);

    const previewBtn = document.getElementById(type + 'PreviewBtn');
    const previewDiv = document.getElementById(type + 'Preview');
    previewDiv.textContent = text.slice(0, 1200) + (text.length > 1200 ? '\n\n… (truncated)' : '');
    previewBtn.style.display = 'inline-block';
  } catch (err) {
    setStatus(type, 'βœ• Failed to read PDF: ' + err.message, true);
  }
}

function setStatus(type, msg, isError = false) {
  const el = document.getElementById(type + 'Status');
  el.textContent = msg;
  el.className = 'extract-status' + (isError ? ' error' : '');
}

function clearFile(type) {
  state[type] = null;
  document.getElementById(type + 'File').value = '';
  document.getElementById(type + 'Chip').style.display = 'none';
  document.getElementById(type + 'DropZone').classList.remove('has-file');
  document.getElementById(type + 'Status').textContent = '';
  document.getElementById(type + 'PreviewBtn').style.display = 'none';
  document.getElementById(type + 'Preview').style.display = 'none';
}

function togglePreview(type) {
  const div = document.getElementById(type + 'Preview');
  const btn = document.getElementById(type + 'PreviewBtn');
  const visible = div.style.display !== 'none';
  div.style.display = visible ? 'none' : 'block';
  btn.textContent = visible
    ? (type === 'resume' ? 'πŸ‘ Preview resume text' : 'πŸ‘ Preview JD text')
    : 'πŸ™ˆ Hide preview';
}

// ── Drop Zone Setup ────────────────────────────────────────────────────────
function setupDropZone(type) {
  const zone = document.getElementById(type + 'DropZone');
  const input = document.getElementById(type + 'File');

  zone.addEventListener('click', e => {
    if (e.target.closest('.file-chip') || e.target.classList.contains('drop-link')) return;
    input.click();
  });

  input.addEventListener('change', () => {
    if (input.files[0]) handleFile(type, input.files[0]);
  });

  zone.addEventListener('dragover', e => { e.preventDefault(); zone.classList.add('drag-over'); });
  zone.addEventListener('dragleave', () => zone.classList.remove('drag-over'));
  zone.addEventListener('drop', e => {
    e.preventDefault();
    zone.classList.remove('drag-over');
    const file = e.dataTransfer.files[0];
    if (file) handleFile(type, file);
  });
}

setupDropZone('resume');
setupDropZone('jd');

// ── Mode Toggle (shared for both resume and jd) ────────────────────────────
function switchMode(type, mode) {
  if (type === 'resume') resumeMode = mode;
  else jdMode = mode;

  const cap = type.charAt(0).toUpperCase() + type.slice(1);

  document.getElementById(type + 'PdfMode').style.display  = mode === 'pdf'  ? 'block' : 'none';
  document.getElementById(type + 'TextMode').style.display = mode === 'text' ? 'block' : 'none';
  document.getElementById(type + 'TogglePdf').classList.toggle('active',  mode === 'pdf');
  document.getElementById(type + 'ToggleText').classList.toggle('active', mode === 'text');

  if (mode === 'pdf') {
    document.getElementById(type + 'Textarea').value = '';
    updateCharCount(type);
  } else {
    clearFile(type);
  }
}

// ── Get text from whichever mode is active ─────────────────────────────────
function getText(type) {
  const mode = type === 'resume' ? resumeMode : jdMode;
  if (mode === 'text') {
    return document.getElementById(type + 'Textarea').value.trim();
  }
  return state[type];
}

// ── Character Counters ─────────────────────────────────────────────────────
function updateCharCount(type) {
  const textarea = document.getElementById(type + 'Textarea');
  const len = textarea ? textarea.value.length : 0;
  const counter = document.getElementById(type + 'CharCount');
  if (counter) counter.textContent = `${len.toLocaleString()} character${len !== 1 ? 's' : ''}`;
}

document.getElementById('resumeTextarea').addEventListener('input', () => updateCharCount('resume'));
document.getElementById('jdTextarea').addEventListener('input', () => updateCharCount('jd'));

// ── Analyze ────────────────────────────────────────────────────────────────
async function analyze() {
  const btn        = document.getElementById('analyzeBtn');
  const spinner    = document.getElementById('spinner');
  const btnLabel   = document.getElementById('btnLabel');
  const statusText = document.getElementById('statusText');
  const errorBox   = document.getElementById('errorBox');
  const results    = document.getElementById('results');

  errorBox.classList.remove('show');
  results.classList.remove('show');

  const resumeText = getText('resume');
  if (!resumeText || resumeText.length < 30) {
    showError(
      resumeMode === 'pdf'
        ? 'Please upload and process your resume PDF first.'
        : 'Please paste at least a few lines of resume text.'
    );
    return;
  }

  const jdText = getText('jd');
  if (!jdText || jdText.length < 30) {
    showError(
      jdMode === 'pdf'
        ? 'Please upload and process the job description PDF first.'
        : 'Please paste at least a few lines of job description text.'
    );
    return;
  }

  btn.disabled = true;
  spinner.classList.add('active');
  btnLabel.textContent = 'Analyzing...';
  statusText.textContent = 'Sending to backend...';

  try {
    const resumeFile = document.getElementById('resumeFile').files[0];
    const jdFile     = document.getElementById('jdFile').files[0];
    const useUpload  = (resumeMode === 'pdf' && resumeFile) || (jdMode === 'pdf' && jdFile);

    let res;
    if (useUpload) {
      // At least one PDF β€” send as multipart/form-data
      const form = new FormData();
      if (resumeMode === 'pdf' && resumeFile) form.append('resume_pdf', resumeFile);
      else form.append('resume_text', resumeText);
      if (jdMode === 'pdf' && jdFile) form.append('jd_pdf', jdFile);
      else form.append('job_description', jdText);
      res = await fetch(`${API}/predict/ats/upload`, { method: 'POST', body: form });
    } else {
      // Both plain text β€” send as JSON
      res = await fetch(`${API}/predict/ats`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ resume_text: resumeText, job_description: jdText })
      });
    }

    if (!res.ok) {
      const err = await res.json().catch(() => ({}));
      throw new Error(err.detail || `HTTP ${res.status}`);
    }

    const data = await res.json();
    render(data);
    statusText.textContent = 'Done βœ“';
  } catch (e) {
    const msg = (e.message || '').toLowerCase().includes('fetch')
      ? `Cannot reach backend at ${API}. Is your FastAPI server running?`
      : e.message;
    showError(msg);
    statusText.textContent = '';
  } finally {
    btn.disabled = false;
    spinner.classList.remove('active');
    btnLabel.textContent = 'Run Analysis';
  }
}

// ── Render Results ─────────────────────────────────────────────────────────
function render(data) {
  const s = +(data.semantic_score  || 0).toFixed(1);
  const k = +(data.keyword_score   || 0).toFixed(1);
  const f = +(data.final_ats_score || 0).toFixed(1);

  document.getElementById('finalScore').innerHTML    = `${f}<span class="score-unit">/100</span>`;
  document.getElementById('semanticScore').innerHTML = `${s}<span class="score-unit">/100</span>`;
  document.getElementById('keywordScore').innerHTML  = `${k}<span class="score-unit">/100</span>`;
  document.getElementById('feedbackBody').textContent = data.summary || 'No feedback returned.';

  document.getElementById('results').classList.add('show');

  setTimeout(() => {
    setBar('finalBar',    'finalPct',    'finalMiniBar',    f);
    setBar('semanticBar', 'semanticPct', 'semanticMiniBar', s);
    setBar('keywordBar',  'keywordPct',  'keywordMiniBar',  k);
  }, 60);
}

function setBar(barId, pctId, miniId, val) {
  const pct = Math.min(val, 100);
  document.getElementById(barId).style.width  = pct + '%';
  document.getElementById(miniId).style.width = pct + '%';
  document.getElementById(pctId).textContent  = val + '%';
}

// ── Utilities ──────────────────────────────────────────────────────────────
function showError(msg) {
  const box = document.getElementById('errorBox');
  box.textContent = msg;
  box.classList.add('show');
}

function clearAll() {
  clearFile('resume');
  clearFile('jd');
  document.getElementById('resumeTextarea').value = '';
  document.getElementById('jdTextarea').value = '';
  updateCharCount('resume');
  updateCharCount('jd');
  switchMode('resume', 'pdf');
  switchMode('jd', 'pdf');
  document.getElementById('results').classList.remove('show');
  document.getElementById('errorBox').classList.remove('show');
  document.getElementById('statusText').textContent = '';
}