<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Request — Dealspace</title>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
  <script src="https://cdn.tailwindcss.com"></script>
  <style>* { font-family: 'Inter', sans-serif; } body { background: #0a1628; }
    .sidebar-link.active { background: rgba(201,168,76,0.1); color: #c9a84c; border-left: 3px solid #c9a84c; }
    .sidebar-link:hover:not(.active) { background: rgba(255,255,255,0.04); }
  </style>
  <style id="ds-themes">
    :root,html[data-theme="midnight"]{--ds-bg:#0a1628;--ds-sf:#0d1f3c;--ds-tx:#fff;--ds-tx2:#94a3b8;--ds-tx3:#475569;--ds-ac:#c9a84c;--ds-ac2:#b8973f;--ds-act:#0a1628;--ds-bd:rgba(255,255,255,.08);--ds-hv:rgba(255,255,255,.04);--ds-inp:#0a1628;--ds-hf:'Inter',sans-serif}
    html[data-theme="light"]{--ds-bg:#f0f2f5;--ds-sf:#fff;--ds-tx:#1a202c;--ds-tx2:#64748b;--ds-tx3:#94a3b8;--ds-ac:#2563eb;--ds-ac2:#1d4ed8;--ds-act:#fff;--ds-bd:rgba(0,0,0,.08);--ds-hv:rgba(0,0,0,.05);--ds-inp:#f8fafc;--ds-hf:'Inter',sans-serif}
    html[data-theme="slate"]{--ds-bg:#1e293b;--ds-sf:#334155;--ds-tx:#f1f5f9;--ds-tx2:#94a3b8;--ds-tx3:#64748b;--ds-ac:#14b8a6;--ds-ac2:#0d9488;--ds-act:#0f172a;--ds-bd:rgba(148,163,184,.12);--ds-hv:rgba(255,255,255,.05);--ds-inp:#1e293b;--ds-hf:'Inter',sans-serif}
    html[data-theme="compact"]{font-size:13.5px;--ds-bg:#0a1628;--ds-sf:#0d1f3c;--ds-tx:#fff;--ds-tx2:#94a3b8;--ds-tx3:#475569;--ds-ac:#c9a84c;--ds-ac2:#b8973f;--ds-act:#0a1628;--ds-bd:rgba(255,255,255,.08);--ds-hv:rgba(255,255,255,.04);--ds-inp:#0a1628;--ds-hf:'Inter',sans-serif}
    html[data-theme="executive"]{font-size:17px;--ds-bg:#1a1410;--ds-sf:#2a2218;--ds-tx:#f5f0e8;--ds-tx2:#c4b8a4;--ds-tx3:#8a7e6e;--ds-ac:#d4a843;--ds-ac2:#c49835;--ds-act:#1a1410;--ds-bd:rgba(196,184,164,.12);--ds-hv:rgba(212,168,67,.06);--ds-inp:#1a1410;--ds-hf:'Georgia','Times New Roman',serif}
    body{background:var(--ds-bg)!important}
    h1,h2,h3{font-family:var(--ds-hf)!important}
    html[data-theme="executive"] *{font-family:'Georgia','Times New Roman',serif!important}
    header{background:var(--ds-sf)!important;border-color:var(--ds-bd)!important}
    nav{background:var(--ds-sf)!important;border-color:var(--ds-bd)!important}
    .text-white{color:var(--ds-tx)!important}
    [class*="text-white/"]{color:var(--ds-tx3)!important}
    .sidebar-link.active{background:color-mix(in srgb,var(--ds-ac) 10%,transparent)!important;color:var(--ds-ac)!important;border-left-color:var(--ds-ac)!important}
    .sidebar-link:hover:not(.active){background:var(--ds-hv)!important}
    [class*="bg-[#0d1f3c]"]{background-color:var(--ds-sf)!important}
    [class*="bg-[#0a1628]"]{background-color:var(--ds-bg)!important}
    [class*="text-[#c9a84c]"]{color:var(--ds-ac)!important}
    [class*="text-[#94a3b8]"]{color:var(--ds-tx2)!important}
    [class*="text-[#475569]"]{color:var(--ds-tx3)!important}
    [class*="text-[#0a1628]"]{color:var(--ds-act)!important}
    [class*="bg-[#c9a84c]"]{background-color:var(--ds-ac)!important}
    [class*="hover:bg-[#b8973f]"]:hover{background-color:var(--ds-ac2)!important}
    [class*="border-white"]{border-color:var(--ds-bd)!important}
    [class*="bg-white/"]{background-color:var(--ds-hv)!important}
    [class*="hover:bg-white/"]:hover{background-color:var(--ds-hv)!important}
    input,textarea,select{background-color:var(--ds-inp)!important;color:var(--ds-tx)!important;border-color:var(--ds-bd)!important}
    input:focus,textarea:focus,select:focus{border-color:var(--ds-ac)!important}
    [class*="bg-black/"]{background-color:rgba(0,0,0,.6)!important}
    #ds-theme-bar{position:fixed;bottom:20px;right:20px;z-index:9999;display:flex;gap:3px;padding:4px;background:var(--ds-sf);border:1px solid var(--ds-bd);border-radius:12px;box-shadow:0 4px 20px rgba(0,0,0,.3)}
    #ds-theme-bar button{padding:5px 10px;border-radius:8px;border:none;cursor:pointer;background:transparent;color:var(--ds-tx2);white-space:nowrap;transition:all .15s;font-size:12px}
    #ds-theme-bar button:hover{background:var(--ds-hv)}
    #ds-theme-bar button.active{background:var(--ds-ac);color:var(--ds-act);font-weight:600}
    #ds-theme-bar,#ds-theme-bar *{font-family:'Inter',sans-serif!important}
  </style>
  <script>document.documentElement.setAttribute('data-theme',localStorage.getItem('ds_theme')||'midnight')</script>
</head>
<body>
  <header class="bg-[#0d1f3c] border-b border-white/[0.08] px-6 py-3 flex items-center justify-between sticky top-0 z-50">
    <div class="flex items-center gap-3">
      <a href="/app/projects" class="text-xl font-bold text-white tracking-tight"><span class="text-[#c9a84c]">Deal</span>space</a>
      <span class="text-white/20">/</span>
      <a href="/app/projects" class="text-sm text-[#94a3b8] hover:text-white transition">Projects</a>
      <span class="text-white/20">/</span>
      <a id="backToProject" href="/app/projects" class="text-sm text-[#94a3b8] hover:text-white transition">Project</a>
      <span class="text-white/20">/</span>
      <span id="reqRef" class="text-sm text-white font-medium">Request</span>
    </div>
    <div class="flex items-center gap-4">
      <span id="userName" class="text-sm text-[#94a3b8]"></span>
      <button onclick="logout()" class="text-sm text-[#94a3b8] hover:text-white transition">Logout</button>
    </div>
  </header>
  <div class="flex">
    <nav class="w-56 bg-[#0d1f3c] border-r border-white/[0.08] min-h-[calc(100vh-49px)] sticky top-[49px] shrink-0">
      <div class="p-3 space-y-0.5">
        <a href="/app/tasks" class="sidebar-link flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm font-medium text-[#94a3b8] transition">
          <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"/></svg>
          My Tasks</a>
        <a href="/app/projects" class="sidebar-link active flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm font-medium text-white transition">
          <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"/></svg>
          Projects</a>
        <div id="adminLinks" class="hidden"><div class="border-t border-white/[0.08] my-3"></div>
          <a href="/admin" class="sidebar-link flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm font-medium text-[#94a3b8] transition">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.066 2.573c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.573 1.066c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.066-2.573c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/></svg>
            Admin</a></div>
      </div>
    </nav>
    <main class="flex-1 p-8 max-w-6xl">

      <!-- Two-column layout: Request + Response -->
      <div class="grid grid-cols-1 lg:grid-cols-12 gap-6 mb-6">

        <!-- LEFT: Request (read-only) -->
        <div class="lg:col-span-7 bg-[#0d1f3c] border border-white/[0.08] rounded-xl p-6">
          <div class="flex items-center gap-2 mb-5">
            <span class="text-lg">📋</span>
            <h2 class="text-sm font-semibold text-[#94a3b8] uppercase tracking-wider">Request</h2>
          </div>
          <div class="flex items-start gap-3 mb-4">
            <h1 id="reqTitle" class="text-xl font-bold text-white leading-tight flex-1">Loading...</h1>
            <span id="reqItemBadge" class="hidden shrink-0 px-2.5 py-1 rounded-md bg-[#c9a84c]/15 text-[#c9a84c] text-xs font-semibold"></span>
          </div>
          <p id="reqDesc" class="text-[#94a3b8] text-sm leading-relaxed mb-5"></p>
          <div class="border-t border-white/[0.06] pt-4 flex flex-wrap items-center gap-3">
            <span id="reqPriority" class="hidden px-2.5 py-1 rounded-full text-xs font-medium"></span>
            <span id="reqDue" class="hidden text-xs text-[#475569]"></span>
            <span id="reqStatus" class="px-2.5 py-1 rounded-full text-xs font-medium capitalize"></span>
          </div>
        </div>

        <!-- RIGHT: Response -->
        <div class="lg:col-span-5 bg-[#0d1f3c] border border-white/[0.08] rounded-xl p-6 flex flex-col">
          <div class="flex items-center gap-2 mb-5">
            <span class="text-lg">📁</span>
            <h2 class="text-sm font-semibold text-[#94a3b8] uppercase tracking-wider">Response</h2>
          </div>
          <div id="answeredBanner" class="hidden mb-4 px-4 py-2.5 rounded-lg bg-green-500/10 border border-green-500/20 text-green-300 text-sm font-medium flex items-center gap-2">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg>
            Answered
          </div>
          <div id="answers" class="space-y-2 mb-4 flex-1"></div>
          <div id="uploadArea" class="border-2 border-dashed border-white/[0.08] rounded-xl p-6 text-center hover:border-[#c9a84c]/40 transition cursor-pointer" onclick="document.getElementById('fileInput').click()">
            <div class="text-2xl mb-1">📎</div>
            <p class="text-[#94a3b8] text-sm">Drop files to submit response</p>
            <p class="text-[#475569] text-xs mt-1">PDF, DOCX, XLSX, images</p>
            <input id="fileInput" type="file" multiple class="hidden" onchange="uploadFiles(this.files)">
          </div>
          <div id="uploadStatus" class="mt-2 text-sm text-[#94a3b8]"></div>
          <button id="markAnsweredBtn" onclick="markAnswered()" class="hidden mt-4 w-full px-4 py-2.5 bg-green-600 hover:bg-green-700 text-white font-semibold rounded-lg text-sm transition flex items-center justify-center gap-2">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg>
            Mark as Answered
          </button>
        </div>
      </div>

      <!-- BOTTOM: Discussion (full width) -->
      <div class="bg-[#0d1f3c] border border-white/[0.08] rounded-xl p-6">
        <div class="flex items-center gap-2 mb-5">
          <span class="text-lg">💬</span>
          <h2 class="text-sm font-semibold text-[#94a3b8] uppercase tracking-wider">Discussion</h2>
        </div>
        <div id="comments" class="space-y-3 mb-4"></div>
        <div class="flex gap-3">
          <textarea id="commentText" rows="2" placeholder="Add a comment..." class="flex-1 px-4 py-2.5 bg-[#0a1628] border border-white/[0.08] rounded-lg text-white placeholder-[#475569] focus:outline-none focus:border-[#c9a84c] focus:ring-1 focus:ring-[#c9a84c] text-sm resize-none"></textarea>
          <button onclick="postComment()" class="px-4 py-2 bg-[#c9a84c] hover:bg-[#b8973f] text-[#0a1628] font-semibold rounded-lg text-sm transition self-end">Post</button>
        </div>
      </div>
    </main>
  </div>

  <script>
    const token = localStorage.getItem('ds_token');
    if (!token) window.location.href = '/app/login';
    const user = JSON.parse(localStorage.getItem('ds_user') || '{}');
    const reqID = location.pathname.split('/').pop();
    document.getElementById('userName').textContent = user.name || user.email || '';
    if (user.is_super_admin) document.getElementById('adminLinks').classList.remove('hidden');

    function fetchAPI(path, opts = {}) {
      opts.headers = { ...opts.headers, 'Authorization': 'Bearer ' + token };
      if (opts.body && typeof opts.body === 'string') opts.headers['Content-Type'] = 'application/json';
      return fetch(path, opts).then(r => { if (r.status === 401) { localStorage.removeItem('ds_token'); window.location.href = '/app/login'; } return r; });
    }
    function logout() { fetchAPI('/api/auth/logout', { method: 'POST' }).finally(() => { localStorage.clear(); window.location.href = '/app/login'; }); }
    function parseData(t) { try { return JSON.parse(t); } catch { return {}; } }
    function escHtml(s) { const d = document.createElement('div'); d.textContent = s; return d.innerHTML; }

    const statusColors = { open: 'bg-yellow-500/20 text-yellow-300', answered: 'bg-green-500/20 text-green-300', closed: 'bg-gray-500/20 text-gray-300', 'under-review': 'bg-blue-500/20 text-blue-300' };
    const priorityColors = { high: 'bg-red-500/20 text-red-300', medium: 'bg-yellow-500/20 text-yellow-300', low: 'bg-blue-500/20 text-blue-300' };

    let currentRequest = null; // stores the full request entry
    let currentData = null;    // stores parsed data_text

    async function loadAll() {
      try {
        const res = await fetchAPI('/api/requests/' + reqID);
        if (!res.ok) { document.getElementById('reqTitle').textContent = 'Not found'; return; }
        const json = await res.json();
        currentRequest = json.request;
        currentData = parseData(currentRequest.data_text);
        const children = json.children || [];

        renderRequest(currentRequest, currentData);
        renderAnswers(children.filter(c => c.type === 'answer' || c.type === 'document'));
        renderComments(children.filter(c => c.type === 'comment'));
      } catch(e) {
        document.getElementById('reqTitle').textContent = 'Error loading request';
      }
    }

    function renderRequest(req, d) {
      const title = d.title || req.summary || 'Untitled';
      document.title = title + ' — Dealspace';
      document.getElementById('reqRef').textContent = d.item_number || title;
      document.getElementById('reqTitle').textContent = title;
      document.getElementById('reqDesc').textContent = d.description || '';

      if (req.project_id) document.getElementById('backToProject').href = '/app/projects/' + req.project_id;

      // Item number / section badge
      const badge = document.getElementById('reqItemBadge');
      const badgeParts = [d.section, d.item_number].filter(Boolean);
      if (badgeParts.length) { badge.textContent = badgeParts.join(' · '); badge.classList.remove('hidden'); }

      // Priority
      const prioEl = document.getElementById('reqPriority');
      if (d.priority) {
        const pc = priorityColors[d.priority] || 'bg-gray-500/20 text-gray-300';
        prioEl.className = 'px-2.5 py-1 rounded-full text-xs font-medium capitalize ' + pc;
        prioEl.textContent = d.priority + ' priority';
        prioEl.classList.remove('hidden');
      }

      // Due date
      if (d.due_date) {
        const dueEl = document.getElementById('reqDue');
        dueEl.textContent = '📅 Due: ' + d.due_date;
        dueEl.classList.remove('hidden');
      }

      // Status
      const status = d.status || 'open';
      const sc = statusColors[status] || 'bg-gray-500/20 text-gray-300';
      const statusEl = document.getElementById('reqStatus');
      statusEl.className = 'px-2.5 py-1 rounded-full text-xs font-medium capitalize ' + sc;
      statusEl.textContent = status;

      // Answered banner + mark-answered button
      if (status === 'answered') {
        document.getElementById('answeredBanner').classList.remove('hidden');
        document.getElementById('markAnsweredBtn').classList.add('hidden');
      } else {
        document.getElementById('answeredBanner').classList.add('hidden');
        document.getElementById('markAnsweredBtn').classList.remove('hidden');
      }
    }

    function renderAnswers(docs) {
      const el = document.getElementById('answers');
      if (!docs || docs.length === 0) {
        el.innerHTML = '<p class="text-[#475569] text-sm">No response submitted yet.</p>';
        return;
      }
      const projectID = currentRequest.project_id;
      el.innerHTML = docs.map(a => {
        const d = parseData(a.data_text);
        const name = d.filename || d.name || a.summary || 'Document';
        const icon = name.endsWith('.pdf') ? '📄' : name.match(/\.(jpg|jpeg|png|gif)$/i) ? '🖼️' : name.match(/\.(xlsx?|csv)$/i) ? '📊' : '📎';
        const objectID = d.object_id || '';
        const downloadUrl = objectID ? '/api/projects/' + projectID + '/objects/' + objectID + '?filename=' + encodeURIComponent(name) : '#';
        return `<div class="flex items-center gap-3 px-3 py-2.5 rounded-lg bg-[#0a1628] border border-white/[0.05]">
          <span class="text-xl">${icon}</span>
          <div class="flex-1 min-w-0">
            <div class="text-white text-sm font-medium truncate">${escHtml(name)}</div>
            <div class="text-[#475569] text-xs">${a.created_at ? new Date(a.created_at * 1000).toLocaleString() : ''}</div>
          </div>
          ${objectID ? `<a href="${downloadUrl}" class="text-[#c9a84c] text-sm hover:underline shrink-0">Download</a>` : ''}
        </div>`;
      }).join('');
    }

    function renderComments(comments) {
      const el = document.getElementById('comments');
      if (!comments || comments.length === 0) {
        el.innerHTML = '<p class="text-[#475569] text-sm">No comments yet.</p>';
        return;
      }
      el.innerHTML = comments.map(c => {
        const d = parseData(c.data_text);
        return `<div class="flex gap-3">
          <div class="w-7 h-7 rounded-full bg-[#c9a84c]/20 flex items-center justify-center text-[#c9a84c] text-xs font-semibold shrink-0 mt-0.5">${(d.author||'?')[0].toUpperCase()}</div>
          <div>
            <div class="flex items-baseline gap-2 mb-1">
              <span class="text-white text-sm font-medium">${escHtml(d.author||'Unknown')}</span>
              <span class="text-[#475569] text-xs">${c.created_at ? new Date(c.created_at * 1000).toLocaleString() : ''}</span>
            </div>
            <p class="text-[#94a3b8] text-sm">${escHtml(d.text||'')}</p>
          </div>
        </div>`;
      }).join('');
    }

    async function uploadFiles(files) {
      if (!currentRequest) return;
      const projectID = currentRequest.project_id;
      const status = document.getElementById('uploadStatus');
      for (const file of files) {
        status.textContent = 'Uploading ' + file.name + '...';
        const fd = new FormData();
        fd.append('file', file);
        try {
          // 1. Upload file to object store
          const uploadRes = await fetchAPI('/api/projects/' + projectID + '/objects', { method: 'POST', body: fd });
          if (!uploadRes.ok) { status.textContent = 'Upload failed for ' + file.name; continue; }
          const uploadData = await uploadRes.json();

          // 2. Create document entry linked to request
          const entryBody = {
            project_id: projectID,
            parent_id: reqID,
            type: 'document',
            data: JSON.stringify({ filename: file.name, name: file.name, content_type: file.type, object_id: uploadData.object_id })
          };
          const entryRes = await fetchAPI('/api/projects/' + projectID + '/entries', { method: 'POST', body: JSON.stringify(entryBody) });
          if (entryRes.ok) { status.textContent = file.name + ' uploaded.'; }
          else { status.textContent = 'Failed to save ' + file.name; }
        } catch(e) { status.textContent = 'Error: ' + e.message; }
      }
      setTimeout(() => status.textContent = '', 3000);
      loadAll(); // refresh
    }

    async function postComment() {
      if (!currentRequest) return;
      const text = document.getElementById('commentText').value.trim();
      if (!text) return;
      const projectID = currentRequest.project_id;
      const body = {
        project_id: projectID,
        parent_id: reqID,
        type: 'comment',
        data: JSON.stringify({ text, author: user.name || user.email })
      };
      try {
        const res = await fetchAPI('/api/projects/' + projectID + '/entries', { method: 'POST', body: JSON.stringify(body) });
        if (res.ok) { document.getElementById('commentText').value = ''; loadAll(); }
      } catch(e) {}
    }

    async function markAnswered() {
      if (!currentRequest || !currentData) return;
      const projectID = currentRequest.project_id;
      const updatedData = { ...currentData, status: 'answered' };
      const body = {
        data: JSON.stringify(updatedData),
        version: currentRequest.version
      };
      try {
        const res = await fetchAPI('/api/projects/' + projectID + '/entries/' + reqID, { method: 'PUT', body: JSON.stringify(body) });
        if (res.ok) loadAll();
      } catch(e) {}
    }

    // Drag and drop
    const ua = document.getElementById('uploadArea');
    ua.addEventListener('dragover', e => { e.preventDefault(); ua.classList.add('border-[#c9a84c]/60'); });
    ua.addEventListener('dragleave', () => ua.classList.remove('border-[#c9a84c]/60'));
    ua.addEventListener('drop', e => { e.preventDefault(); ua.classList.remove('border-[#c9a84c]/60'); uploadFiles(e.dataTransfer.files); });

    loadAll();
  </script>
  <div id="ds-theme-bar">
    <button data-t="midnight" onclick="setTheme('midnight')">Midnight</button>
    <button data-t="light" onclick="setTheme('light')">Light</button>
    <button data-t="slate" onclick="setTheme('slate')">Slate</button>
    <button data-t="compact" onclick="setTheme('compact')">Compact</button>
    <button data-t="executive" onclick="setTheme('executive')">Executive</button>
  </div>
  <script>
  function setTheme(t){document.documentElement.setAttribute('data-theme',t);localStorage.setItem('ds_theme',t);document.querySelectorAll('#ds-theme-bar button').forEach(b=>b.classList.toggle('active',b.getAttribute('data-t')===t))}
  setTheme(localStorage.getItem('ds_theme')||'midnight');
  </script>
</body>
</html>
