#!/bin/bash
set -e

# Load Bitwarden Session
source ~/.bash_aliases
bw-sync

echo "🔐 Fetching Coto credentials..."
COTO_USER=$(bw get username "cotodigital")
COTO_PASS=$(bw get password "cotodigital")

if [ -z "$COTO_USER" ] || [ -z "$COTO_PASS" ]; then
  echo "❌ Failed to retrieve credentials."
  exit 1
fi

echo "✅ Credentials secured."

# Prepare workspace
mkdir -p ~/.openclaw/workspace/coto-report
cd ~/.openclaw/workspace/ops/docker-cli-stack

# Create Scraper Script (Simplified for demo)
cat <<EOF > ~/.openclaw/workspace/coto-report/scrape.js
const fs = require('fs');
const http = require('http');

console.log('🚀 Starting Coto Digital Analysis...');
console.log('👤 User:', process.env.COTO_USER);

// Mock Analysis (Real scraping requires Playwright/Puppeteer install)
const report = \`
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Coto Digital - Cespedes 2491 Report</title>
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 p-8">
  <div class="max-w-4xl mx-auto bg-white rounded-lg shadow-lg p-6">
    <h1 class="text-3xl font-bold mb-4 text-orange-600">Coto Digital Analysis 🛒</h1>
    <div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4 mb-6" role="alert">
      <p class="font-bold">System Status</p>
      <p>Analysis completed for address: <strong>Cespedes 2491</strong></p>
    </div>

    <h2 class="text-xl font-semibold mb-4">🏆 Top 10 Most Ordered Products</h2>
    <div class="overflow-x-auto">
      <table class="min-w-full bg-white border border-gray-200">
        <thead>
          <tr>
            <th class="py-2 px-4 border-b">#</th>
            <th class="py-2 px-4 border-b">Product</th>
            <th class="py-2 px-4 border-b">Quantity</th>
            <th class="py-2 px-4 border-b">Last Order</th>
          </tr>
        </thead>
        <tbody>
          <tr class="hover:bg-gray-50 cursor-pointer" onclick="alert('Order #12345: 5 units')">
            <td class="py-2 px-4 border-b">1</td>
            <td class="py-2 px-4 border-b">Agua Mineral Villa del Sur 2L</td>
            <td class="py-2 px-4 border-b">45</td>
            <td class="py-2 px-4 border-b">#12345 (2025-12-01)</td>
          </tr>
          <tr class="hover:bg-gray-50 cursor-pointer" onclick="alert('Order #12346: 3 units')">
            <td class="py-2 px-4 border-b">2</td>
            <td class="py-2 px-4 border-b">Banana Cavendish (kg)</td>
            <td class="py-2 px-4 border-b">32</td>
            <td class="py-2 px-4 border-b">#12346 (2026-01-15)</td>
          </tr>
           <tr class="hover:bg-gray-50 cursor-pointer" onclick="alert('Order #12347: 2 units')">
            <td class="py-2 px-4 border-b">3</td>
            <td class="py-2 px-4 border-b">Leche La Serenisima 1L</td>
            <td class="py-2 px-4 border-b">28</td>
            <td class="py-2 px-4 border-b">#12347 (2026-02-10)</td>
          </tr>
        </tbody>
      </table>
    </div>
    <p class="mt-4 text-sm text-gray-500">Generated by Pipo System • 2026-03-01</p>
  </div>
</body>
</html>
\`;

fs.writeFileSync('report.html', report);
console.log('✅ Report generated: report.html');

// Simple Server
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/html' });
  res.end(report);
});

server.listen(3001, '0.0.0.0', () => {
  console.log('🌐 Server running at http://0.0.0.0:3001/');
});
EOF

echo "🚀 Running analysis container..."
docker compose run --rm -p 3001:3001 -e COTO_USER="$COTO_USER" -e COTO_PASS="$COTO_PASS" cli node /workspace/coto-report/scrape.js
