File size: 7,231 Bytes
9e81be2
 
fb451d2
 
9e81be2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb451d2
 
9e81be2
 
 
 
 
 
 
 
fb451d2
9e81be2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb451d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* ======================= SHORTCUT ======================= */
const el = (id) => document.getElementById(id);
let lastAnalysisId = null;


/* ======================= FIREBASE CONFIG ======================= */
const firebaseConfig = {
  apiKey: "AIzaSyDVwJCmDIEV4cIPDEEzxCLOnDF1f3m3YbA",
  authDomain: "success-predictor-fire.firebaseapp.com",
  projectId: "success-predictor-fire",
};
firebase.initializeApp(firebaseConfig);

const auth = firebase.auth();

/* ======================= LOGIN FUNCTION ======================= */
async function handleGoogleLogin() {
  const provider = new firebase.auth.GoogleAuthProvider();

  try {
    const result = await auth.signInWithPopup(provider);
    const idToken = await result.user.getIdToken();

    await fetch("/api/login", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      credentials: "include",   // πŸ”₯ MUST
      body: JSON.stringify({ token: idToken }) // βœ… FIXED
    });

    updateAuthUI();
  } catch (err) {
    console.error("Login error:", err);
    alert("Login failed");
  }
}

/* ======================= LOGOUT FUNCTION ======================= */
async function handleLogout() {
  await fetch("/api/logout", {
    method: "POST",
    credentials: "include"
  });
  await auth.signOut();
  updateAuthUI();
}


/* ======================= UPDATE LOGIN UI ======================= */
async function updateAuthUI() {
  const openGoogle = el("openGoogle");
  const userSection = el("userSection");
  const userPhoto = el("userPhoto");
  const userDropdown = el("userDropdown");
  const userName = el("userName");
  const userEmail = el("userEmail");

  try {
    const res = await fetch("/api/me", {
  credentials: "include"   // πŸ”₯ MOST IMPORTANT
});

    const j = await res.json();

    if (j.authenticated) {
      // (A) HIDE LOGIN BUTTON
      openGoogle.classList.add("hidden");

      // (B) SHOW PROFILE SECTION
      userSection.classList.remove("hidden");
      userName.textContent = j.name || "User";
      userEmail.textContent = j.email || "";
      userPhoto.src = j.picture || "https://i.imgur.com/4ZQZ4ZQ.png";

      el("openDeep").disabled = false;
    } else {
      // (A) SHOW LOGIN
      openGoogle.classList.remove("hidden");

      // (B) HIDE PROFILE
      userSection.classList.add("hidden");

      openGoogle.onclick = handleGoogleLogin;
      el("openDeep").disabled = true;
    }
  } catch (e) {
    console.warn("Auth check failed:", e);
  }
}
updateAuthUI();

/* ======================= DROPDOWN TOGGLE ======================= */
el("userSection").addEventListener("click", () => {
  el("userDropdown").classList.toggle("hidden");
});

document.addEventListener("click", (e) => {
  if (!el("userSection").contains(e.target)) {
    el("userDropdown").classList.add("hidden");
  }
});

/* ======================= SMOOTH SCROLL TO CARDS ======================= */
document.querySelectorAll("#openFast, #openDeep").forEach((btn) => {
  btn.addEventListener("click", () => {
    document
      .getElementById("analysisSection")
      .scrollIntoView({ behavior: "smooth" });
  });
});

/* ======================= OPEN MODAL ======================= */
function openModal(mode) {
  el("inputModal").classList.remove("hidden");
  el("modalTitle").textContent =
    mode === "deep" ? "Start Deep Analysis" : "Start Fast Analysis";
  el("analyzeBtn").dataset.mode = mode;
  el("formStatus").textContent = "";
}

/* Fast Analysis Card Button */
document.querySelectorAll(".start").forEach((b) => {
  b.addEventListener("click", () => {
    openModal(b.dataset.mode);
  });
});

/* Deep Button Auth Guard */
el("openDeep").addEventListener("click", async () => {
  const me = await fetch("/api/me", { credentials: "include" })
                  .then(r => r.json());

  if (!me.authenticated) {
    alert("Please login first.");
    return;
  }
});


/* Close Modal */
el("closeModal").onclick = () => el("inputModal").classList.add("hidden");
el("cancelBtn").onclick = () => el("inputModal").classList.add("hidden");

/* ======================= ANALYZE ======================= */
el("analyzeBtn").addEventListener("click", async function () {
  const mode = this.dataset.mode;

  const name = el("name").value.trim();
  const pitch = el("pitch").value.trim();
  const description = el("description").value.trim();
  const industry = el("industry").value.trim();

  if (!name && !pitch && !description) {
    el("formStatus").textContent = "Please fill at least one field.";
    return;
  }

  el("formStatus").textContent = "Running AI analysis...";

  try {
    const resp = await fetch("/api/analyze", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        name,
        pitch,
        description,
        industry,
        mode,
      }),
    });

    const j = await resp.json();

    if (!resp.ok) {
      el("formStatus").textContent = j.error || "Error!";
      return;
    }

    const id = j.id;
    lastAnalysisId = id;


    el("formStatus").innerHTML = `
      Analysis Ready πŸŽ‰ <br>
      <a href="/api/doc/${id}" target="_blank">View JSON</a> β€’
      <a href="/api/pdf/${id}" target="_blank">Download TXT</a>
    `;

    fetchHistory();
    el("feedback").style.display = "block";
  } catch (err) {
    el("formStatus").textContent = "Network error.";
  }
});

/* ======================= HISTORY LOADER ======================= */
async function fetchHistory() {
  const historyList = el("historyList");

  try {
    historyList.innerHTML = "Loading...";

    const r = await fetch("/api/history");
    const j = await r.json();

    historyList.innerHTML = "";

    if (!j.items || j.items.length === 0) {
      historyList.innerHTML = '<div class="muted">No recent analyses</div>';
      return;
    }

    j.items.forEach((it) => {
      const div = document.createElement("div");
      div.className = "history-item";

      div.innerHTML = `
        <strong>${it.name || it.pitch || "Unnamed"}</strong>
        <span class="muted">(${it.industry || "General"})</span>
        <div class="small muted">
          Mode: ${it.mode} β€’ 
          <a href="/api/doc/${it.id}" target="_blank">json</a> β€’
          <a href="/api/pdf/${it.id}" target="_blank">txt</a>
        </div>
      `;

      historyList.appendChild(div);
    });
  } catch (e) {
    console.error(e);
  }
}

fetchHistory();

async function submitFeedback() {
  const rating = el("rating").value;
  const helpful = el("helpful").value;
  const comment = el("comment").value;

  if (!lastAnalysisId) {
    el("feedbackStatus").innerText = "Run an analysis first.";
    return;
  }

  try {
    const res = await fetch("/api/feedback", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      credentials: "include",
      body: JSON.stringify({
        analysis_id: lastAnalysisId,
        rating,
        helpful,
        comment,
      }),
    });

    const j = await res.json();

    el("feedbackStatus").innerText =
      j.message || "βœ… Feedback submitted. Thank you!";

    // Optional UX improvement
    el("comment").value = "";
  } catch (e) {
    el("feedbackStatus").innerText = "❌ Failed to submit feedback.";
  }
}