const PASSWORD = "edge10@private";
const STOP_LOSS = 0.7;

/* MOCK SIGNALS */
const universe = [
  { symbol: "NETWEB", score: 82 },
  { symbol: "TATAELXSI", score: 71 },
  { symbol: "SAILIFE", score: 68 },
  { symbol: "MOSCHIP", score: 44 }
];

let portfolio = [
  { symbol: "NETWEB", qty: 5, buy: 3200, current: 3450 },
  { symbol: "MOSCHIP", qty: 50, buy: 205, current: 140 }
];

/* AUTH */
function doLogin() {
  if (passwordInput.value === PASSWORD) {
    loginOverlay.style.display = "none";
    app.classList.remove("hidden");
    renderDashboard();
    renderPortfolio();
  } else {
    loginError.innerText = "Incorrect password";
  }
}

/* NAV */
function showDashboard() {
  dashboard.classList.remove("hidden");
  portfolioPage().classList.add("hidden");
  navDash.classList.add("active");
  navPort.classList.remove("active");
}

function showPortfolio() {
  portfolioPage().classList.remove("hidden");
  dashboard.classList.add("hidden");
  navPort.classList.add("active");
  navDash.classList.remove("active");
}

function portfolioPage() {
  return document.getElementById("portfolio");
}

/* RECOMMENDATION */
function getRecommendation(score) {
  if (score >= 78) return "STRONG BUY";
  if (score >= 68) return "BUY";
  if (score >= 55) return "HOLD";
  if (score >= 45) return "REDUCE";
  return "SELL";
}

function actionClass(action, ignored) {
  if (ignored) return "ignore";
  if (action.includes("BUY")) return "buy";
  if (action === "SELL") return "sell";
  return "neutral";
}

/* DASHBOARD */
function renderDashboard() {
  const grid = document.getElementById("signalGrid");
  grid.innerHTML = "";

  universe.forEach(stock => {
    const rec = getRecommendation(stock.score);
    const holding = portfolio.find(p => p.symbol === stock.symbol);

    let ignored = false;
    let note = "No risk conflict";

    if (holding) {
      const stopHit = holding.current <= holding.buy * STOP_LOSS;
      if (stopHit && (rec === "BUY" || rec === "STRONG BUY")) {
        ignored = true;
        note = "Signal ignored due to stop-loss";
      }
    }

    const card = document.createElement("div");
    card.className = "signal-card";

    card.innerHTML = `
      <h3>${stock.symbol}</h3>
      <div class="score">${stock.score}</div>
      <div class="action ${actionClass(rec, ignored)}">${rec}</div>
      <p class="muted">${note}</p>
    `;

    grid.appendChild(card);
  });
}

/* PORTFOLIO */
function renderPortfolio() {
  portfolioTable.innerHTML = "";

  portfolio.forEach(p => {
    const pl = (p.current - p.buy) * p.qty;
    const dd = ((p.current - p.buy) / p.buy) * 100;
    const stopHit = p.current <= p.buy * STOP_LOSS;

    const row = document.createElement("tr");

    row.innerHTML = `
      <td>${p.symbol}</td>
      <td>${p.qty}</td>
      <td>${p.buy}</td>
      <td>${p.current}</td>
      <td class="${pl >= 0 ? "pos" : "neg"}">${pl.toFixed(0)}</td>
      <td class="${dd <= -30 ? "neg" : "pos"}">${dd.toFixed(1)}%</td>
      <td>${stopHit ? "EXIT REQUIRED" : "OK"}</td>
    `;

    portfolioTable.appendChild(row);
  });
}

/* ADD TRADE */
function addTrade() {
  const sym = symInput.value.trim();
  const qty = Number(qtyInput.value);
  const price = Number(priceInput.value);

  if (!sym || qty <= 0 || price <= 0) return;

  portfolio.push({ symbol: sym, qty, buy: price, current: price });
  renderPortfolio();
  renderDashboard();

  symInput.value = qtyInput.value = priceInput.value = "";
}
