/**
 * A2P AutoPilot MCP Resource Registration
 * Serves bundled HTML for each UI app
 */

import { readFileSync, existsSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// ============================================================
// RESOURCE DEFINITIONS
// ============================================================

export interface AppResource {
  uri: string;
  name: string;
  description: string;
  mimeType: string;
  htmlPath: string;
}

export const appResources: AppResource[] = [
  {
    uri: 'ui://a2p/registration-wizard',
    name: 'A2P Registration Wizard',
    description: 'Multi-step wizard for A2P brand and campaign registration',
    mimeType: 'text/html',
    htmlPath: 'dist/app-ui/registration-wizard.html'
  },
  {
    uri: 'ui://a2p/dashboard',
    name: 'A2P Dashboard',
    description: 'View all A2P submissions with filtering and statistics',
    mimeType: 'text/html',
    htmlPath: 'dist/app-ui/dashboard.html'
  },
  {
    uri: 'ui://a2p/submission-detail',
    name: 'A2P Submission Detail',
    description: 'Detailed view of a single A2P submission with SID chain and history',
    mimeType: 'text/html',
    htmlPath: 'dist/app-ui/submission-detail.html'
  },
  {
    uri: 'ui://a2p/landing-preview',
    name: 'A2P Landing Page Preview',
    description: 'Preview generated landing pages for opt-in, terms, and privacy',
    mimeType: 'text/html',
    htmlPath: 'dist/app-ui/landing-preview.html'
  }
];

// ============================================================
// RESOURCE LOADER
// ============================================================

export function loadResourceContent(resource: AppResource): string {
  const fullPath = join(__dirname, '..', resource.htmlPath);
  
  if (existsSync(fullPath)) {
    return readFileSync(fullPath, 'utf-8');
  }
  
  // Fallback HTML if bundle doesn't exist yet
  return generateFallbackHTML(resource);
}

function generateFallbackHTML(resource: AppResource): string {
  return `<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>${resource.name}</title>
  <style>
    * { margin: 0; padding: 0; box-sizing: border-box; }
    body {
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      min-height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 20px;
    }
    .container {
      background: white;
      border-radius: 12px;
      padding: 40px;
      max-width: 600px;
      box-shadow: 0 10px 40px rgba(0,0,0,0.2);
      text-align: center;
    }
    h1 {
      color: #1a202c;
      margin-bottom: 16px;
      font-size: 28px;
    }
    p {
      color: #4a5568;
      line-height: 1.6;
      margin-bottom: 24px;
    }
    .badge {
      display: inline-block;
      background: #f7fafc;
      color: #2d3748;
      padding: 8px 16px;
      border-radius: 6px;
      font-size: 14px;
      font-weight: 500;
      margin-bottom: 24px;
    }
    .instructions {
      background: #edf2f7;
      border-radius: 8px;
      padding: 20px;
      text-align: left;
    }
    .instructions h2 {
      color: #2d3748;
      font-size: 16px;
      margin-bottom: 12px;
    }
    .instructions code {
      background: #2d3748;
      color: #68d391;
      padding: 2px 8px;
      border-radius: 4px;
      font-size: 14px;
    }
    .instructions ol {
      margin-left: 20px;
      color: #4a5568;
    }
    .instructions li {
      margin-bottom: 8px;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>${resource.name}</h1>
    <div class="badge">${resource.uri}</div>
    <p>${resource.description}</p>
    
    <div class="instructions">
      <h2>⚠️ UI Not Built Yet</h2>
      <p style="margin-bottom: 16px;">The production UI bundle hasn't been created. To build:</p>
      <ol>
        <li>Navigate to <code>a2p-autopilot/mcp-app/</code></li>
        <li>Run <code>npm install</code></li>
        <li>Run <code>npm run build:ui</code></li>
        <li>Restart the MCP server</li>
      </ol>
    </div>
  </div>
</body>
</html>`;
}

// ============================================================
// RESOURCE LIST FOR MCP
// ============================================================

export function getResourceList() {
  return appResources.map(resource => ({
    uri: resource.uri,
    name: resource.name,
    description: resource.description,
    mimeType: resource.mimeType
  }));
}
