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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<script lang="ts">

  import { questions, nextStep, currentStep } from '../stores/appStore'

  import { parseDocument } from '../utils/documentParser'

  import { onMount, onDestroy } from 'svelte'

  

  let dragActive = false

  let questionText = ''

  let isProcessing = false

  let processingStatus = ''

  let uploadedFiles: File[] = []

  let currentMethod: 'paste' | 'upload' = 'upload'

  let extractedContent = ''

  let previousStep = $currentStep

  

  // Auto-clear functionality

  onMount(() => {

    // Clear questions when component mounts (page refresh)

    if ($questions.length > 0) {

      console.log('Clearing questions on component mount (page refresh)')

      clearAllQuestions()

    }

  })

  

  // Watch for step changes

  $: if ($currentStep !== previousStep && previousStep !== undefined) {

    if ($currentStep !== 2) { // If we're leaving step 2 (Add Questions)

      console.log('Clearing questions due to step change')

      clearAllQuestions()

    }

    previousStep = $currentStep

  }

  

  async function handleFileUpload(event: Event) {

    const target = event.target as HTMLInputElement

    if (target.files) {

      await processFiles(Array.from(target.files))

    }

  }

  

  async function processFiles(files: File[]) {

    isProcessing = true

    processingStatus = 'Processing RFP document...'

    uploadedFiles = [...uploadedFiles, ...files]

    

    try {

      for (const file of files) {

        processingStatus = `Extracting content from ${file.name}...`

        const content = await parseDocument(file)

        extractedContent = content

        

        processingStatus = 'Identifying questions in document...'

        // Enhanced question extraction

        const extractedQuestions = extractQuestions(content)

        

        questions.update(current => [...current, ...extractedQuestions])

        processingStatus = `Found ${extractedQuestions.length} questions in RFP document`

      }

    } catch (error) {

      console.error('Error processing files:', error)

      processingStatus = 'Error processing document. Please check format and try again.'

    } finally {

      setTimeout(() => {

        isProcessing = false

        processingStatus = ''

      }, 2000)

    }

  }

  

  function extractQuestions(content: string): any[] {

    const questions = []

    

    console.log('Original content:', content)

    

    // SIMPLE APPROACH: Just split by "number. " pattern

    // Remove any leading/trailing whitespace and normalize

    const cleanContent = content.trim()

    

    // Split using a simple approach - find all "number. " patterns

    const parts = cleanContent.split(/(?=\d+\.\s)/)

    

    console.log('Split parts:', parts)

    

    for (let i = 0; i < parts.length; i++) {

      const part = parts[i].trim()

      

      if (!part) continue

      

      // Extract the question number and text

      const match = part.match(/^(\d+)\.\s*(.+)$/)

      

      if (match) {

        const questionNumber = match[1]

        let questionText = match[2].trim()

        

        // Remove any trailing numbers that might be part of the next question

        questionText = questionText.replace(/\s+\d+\.\s*.*$/, '')

        

        console.log(`Extracted Q${questionNumber}: "${questionText}"`)

        

        if (questionText.length > 3) {

          questions.push({

            id: `q-split-${Date.now()}-${i}`,

            text: questionText,

            selected: true,

            priority: 'high' as const,

            source: 'split'

          })

        }

      }

    }

    

    // FALLBACK: If the above didn't work, try manual character-by-character parsing

    if (questions.length <= 1 && cleanContent.includes('1.') && cleanContent.includes('2.')) {

      console.log('Using fallback manual parsing...')

      questions.length = 0 // Clear any previous attempts

      

      const questionParts = []

      let currentQuestion = ''

      let i = 0

      

      while (i < cleanContent.length) {

        const char = cleanContent[i]

        const nextFew = cleanContent.substring(i, i + 10)

        

        // Check if we're at the start of a new question (digit followed by period and space)

        const questionStart = nextFew.match(/^(\d+)\.\s/)

        

        if (questionStart && currentQuestion.trim()) {

          // Save the previous question

          questionParts.push(currentQuestion.trim())

          currentQuestion = ''

        }

        

        currentQuestion += char

        i++

      }

      

      // Don't forget the last question

      if (currentQuestion.trim()) {

        questionParts.push(currentQuestion.trim())

      }

      

      console.log('Manual parsing result:', questionParts)

      

      // Process each part

      questionParts.forEach((part, index) => {

        const match = part.match(/^(\d+)\.\s*(.+)$/)

        if (match) {

          const questionText = match[2].trim()

          console.log(`Manual Q${match[1]}: "${questionText}"`)

          

          questions.push({

            id: `q-manual-${Date.now()}-${index}`,

            text: questionText,

            selected: true,

            priority: 'high' as const,

            source: 'manual'

          })

        }

      })

    }

    

    console.log(`Final result: ${questions.length} questions extracted`)

    return questions

  }

  

  function handleDrop(event: DragEvent) {

    event.preventDefault()

    dragActive = false

    

    if (event.dataTransfer?.files) {

      processFiles(Array.from(event.dataTransfer.files))

    }

  }

  

  function handleDragOver(event: DragEvent) {

    event.preventDefault()

    dragActive = true

  }

  

  function handleDragLeave() {

    dragActive = false

  }

  

  function addManualQuestion() {

    if (questionText.trim()) {

      // Use the extraction function to split multiple questions

      const extractedQuestions = extractQuestions(questionText.trim())

      

      if (extractedQuestions.length > 0) {

        // Add all extracted questions

        questions.update(current => [...current, ...extractedQuestions])

      } else {

        // Fallback: add as single question if extraction fails

        questions.update(current => [...current, {

          id: `manual-${Date.now()}`,

          text: questionText.trim(),

          selected: true,

          priority: 'high' as const,

          source: 'manual'

        }])

      }

      

      questionText = ''

    }

  }

  

  function removeFile(index: number) {

    uploadedFiles = uploadedFiles.filter((_, i) => i !== index)

  }

  

  function removeQuestion(id: string) {

    questions.update(current => current.filter(q => q.id !== id))

  }

  

  function clearAllQuestions() {

    questions.set([])

    uploadedFiles = []

    extractedContent = ''

    questionText = ''

  }

  

  function proceedToAnswers() {

    if ($questions.length > 0) {

      // Skip selection step and go directly to AI Processing (step 3)

      nextStep() // This will go to step 3 (AI Processing)

    }

  }

</script>

<div class="max-w-4xl mx-auto space-y-6">
  <!-- Method Selection -->
  <div class="bg-gray-800 border border-gray-600 rounded-lg p-6">
    <h2 class="text-xl font-semibold text-white mb-4">Add RFP Questions</h2>
    
    <div class="flex gap-4 mb-6">
      <button 

        class="px-4 py-2 rounded border {currentMethod === 'upload' ? 'bg-blue-500/20 border-blue-500 text-blue-300' : 'bg-gray-700 border-gray-600 text-gray-300'}"

        on:click={() => currentMethod = 'upload'}
      >
        Upload Document
      </button>
      <button 

        class="px-4 py-2 rounded border {currentMethod === 'paste' ? 'bg-blue-500/20 border-blue-500 text-blue-300' : 'bg-gray-700 border-gray-600 text-gray-300'}"

        on:click={() => currentMethod = 'paste'}
      >
        Manual Entry
      </button>
    </div>

    {#if currentMethod === 'upload'}
      <!-- File Upload Section -->
      <div class="space-y-4">
        <div

          class="border-2 border-dashed border-gray-600 rounded-lg p-8 text-center transition-colors bg-gray-800

          {dragActive ? 'border-blue-500 bg-blue-500/10' : 'hover:border-blue-500/50'}

          {isProcessing ? 'opacity-60' : ''}"

          on:drop={handleDrop}

          on:dragover={handleDragOver}

          on:dragleave={handleDragLeave}

        >
          {#if isProcessing}
            <div class="space-y-3">
              <div class="inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500"></div>
              <p class="text-blue-400 font-medium">{processingStatus}</p>
              {#if processingStatus.includes('Found')}
                <div class="text-green-400 text-sm">✓ Processing complete</div>
              {/if}
            </div>
          {:else}
            <div class="space-y-3">
              <svg class="mx-auto h-12 w-12 text-gray-400" stroke="currentColor" fill="none" viewBox="0 0 48 48">
                <path d="M28 8H12a4 4 0 00-4 4v20m32-12v8m0 0v8a4 4 0 01-4 4H12a4 4 0 01-4-4v-4m32-4l-3.172-3.172a4 4 0 00-5.656 0L28 28M8 32l9.172-9.172a4 4 0 015.656 0L28 28m0 0l4 4m4-24h8m-4-4v8m-12 4h.02" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
              </svg>
              <div>
                <p class="text-gray-200 font-medium">Drop RFP documents here</p>
                <p class="text-gray-400 text-sm">or</p>
                <label class="inline-block px-4 py-2 bg-blue-600 text-white rounded cursor-pointer hover:bg-blue-700">
                  Browse Files
                  <input type="file" class="hidden" accept=".pdf,.docx,.doc" multiple on:change={handleFileUpload}>
                </label>
              </div>
              <p class="text-xs text-gray-500">Supports PDF, DOCX, DOC files</p>
            </div>
          {/if}
        </div>

        <!-- Uploaded Files List -->
        {#if uploadedFiles.length > 0}
          <div class="bg-gray-700/50 rounded-lg p-4">
            <h3 class="font-medium text-gray-200 mb-3">Uploaded Documents</h3>
            <div class="space-y-2">
              {#each uploadedFiles as file, index}
                <div class="flex items-center justify-between bg-gray-800 p-3 rounded border border-gray-600">
                  <div class="flex items-center space-x-3">
                    <svg class="h-5 w-5 text-blue-400" fill="currentColor" viewBox="0 0 20 20">
                      <path fill-rule="evenodd" d="M4 4a2 2 0 012-2h4.586A2 2 0 0112 2.586L15.414 6A2 2 0 0116 7.414V16a2 2 0 01-2 2H6a2 2 0 01-2-2V4z" clip-rule="evenodd" />
                    </svg>
                    <div>
                      <p class="font-medium text-gray-200">{file.name}</p>
                      <p class="text-sm text-gray-400">{(file.size / 1024 / 1024).toFixed(2)} MB</p>
                    </div>
                  </div>
                  <button 

                    class="text-red-400 hover:text-red-300"

                    on:click={() => removeFile(index)}
                  >
                    Remove
                  </button>
                </div>
              {/each}
            </div>
          </div>
        {/if}
      </div>
    {:else}
      <!-- Manual Entry Section -->
      <div class="space-y-4">
        <div>
          <label class="block text-sm font-medium text-gray-200 mb-2">
            Enter Questions (one per line or numbered)
          </label>
          <textarea

            bind:value={questionText}

            placeholder="Example:

1. What is your company's experience with similar projects?

2. How do you ensure data security and compliance?

3. What is your implementation timeline?"

            class="w-full h-32 p-3 border border-gray-600 bg-gray-800 text-gray-100 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 resize-none placeholder-gray-500"

          />
        </div>
        
        <button

          on:click={addManualQuestion}

          disabled={!questionText.trim()}

          class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed"

        >
          Add Question
        </button>
      </div>
    {/if}
  </div>

  <!-- Questions Preview -->
  {#if $questions.length > 0}
    <div class="bg-gray-800 border border-gray-600 rounded-lg p-6">
      <div class="flex items-center justify-between mb-4">
        <div>
          <h3 class="text-lg font-semibold text-white">Extracted Questions</h3>
          <p class="text-sm text-gray-400">{$questions.length} questions found</p>
        </div>
        
        <div class="flex items-center space-x-2">
          <button

            on:click={clearAllQuestions}

            class="px-3 py-1 bg-red-600 text-white text-sm rounded hover:bg-red-700 transition-colors"

            title="Clear all questions"

          >
            Clear All
          </button>
          <button

            on:click={proceedToAnswers}

            class="px-6 py-2 bg-green-600 text-white rounded hover:bg-green-700"

          >
            Generate AI Answers
          </button>
        </div>
      </div>
      
      <div class="space-y-3 max-h-96 overflow-y-auto">
        {#each $questions as question, index}
          <div class="border border-gray-600 bg-gray-700/50 rounded-lg p-4">
            <div class="flex items-start justify-between">
              <div class="flex-1">
                <div class="flex items-center space-x-2 mb-2">
                  <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-blue-500/20 text-blue-300">
                    Q{index + 1}
                  </span>
                  {#if question.source}
                    <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-gray-600 text-gray-300">
                      {question.source}
                    </span>
                  {/if}
                  <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium

                    {question.priority === 'high' ? 'bg-red-500/20 text-red-300' : 

                      question.priority === 'medium' ? 'bg-blue-500/20 text-blue-300' : 

                      'bg-green-500/20 text-green-300'}">
                    {question.priority}
                  </span>
                </div>
                <p class="text-gray-200">{question.text}</p>
              </div>
              <button

                on:click={() => removeQuestion(question.id)}
                class="ml-4 text-red-400 hover:text-red-300"
                title="Remove question"
              >
                ×
              </button>
            </div>
          </div>
        {/each}
      </div>
    </div>
  {/if}
</div>