import { Hono } from '@hono/hono';
import { logger } from '@hono/hono/logger';
import { serveStatic } from '@hono/hono/deno';
import { Layout } from './layouts/layout.tsx';
import { getLatestVideo } from './lucinda.ts';
import { HuggingFace } from './huggingface.ts';

const HF_COOKIE = Deno.env.get('HF_COOKIE');
if (!HF_COOKIE) {
  throw new Error('Missing HF_COOKIE');
} else {
  const hf = new HuggingFace(HF_COOKIE);
  const users = await hf.getOrganizationFollowers('nvidia');
  if (!Object.hasOwn(users[0], 'isFollowing')) throw new Error('Invalid cookie');
}

const app = new Hono();

let counter = 0;

app.use(logger());

app.use('/static/*', serveStatic({ root: './' }));

app.get('/', async (c) => {
  counter++;
  console.log('Counter:', counter);

  let video: string;
  try {
    video = await getLatestVideo();
  } catch (e) {
    console.error(e);
    video = 'srs1dXyAoTQ';
  }
  return c.html(
    <Layout>
      <div class='matrix-bg'></div>
      <div class='bg-animation'>
        <div class='particle'></div>
        <div class='particle'></div>
        <div class='particle'></div>
        <div class='particle'></div>
        <div class='particle'></div>
      </div>
      <header>
        <div class='visitor-counter'>Visitors: {counter}</div>
        <h1>Artificial General Intelligence</h1>
        <div class='subtitle'>Understanding the Future of AI</div>
      </header>
      <div class='container'>
        <div class='video-container'>
          <iframe src={`https://www.youtube.com/embed/${video}?autoplay=1&cc_load_policy=1`} frameborder='0' allow='accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share' referrerpolicy='strict-origin-when-cross-origin' allowfullscreen></iframe>
        </div>
        <section class='intro-text'>
          <h2>What is Artificial General Intelligence?</h2>
          <p>Artificial General Intelligence (AGI) refers to highly autonomous systems that outperform humans at most economically valuable work. Unlike narrow AI systems designed for specific tasks, AGI aims to possess the ability to understand, learn, and apply knowledge across a wide range of domains—similar to human intelligence.</p>
          <p>The development of AGI represents one of the most profound technological challenges and opportunities of our time, with potential impacts across virtually every aspect of human society.</p>
        </section>
        <section class='key-points'>
          <div class='point-card'>
            <h3>Key Characteristics</h3>
            <p>AGI systems would demonstrate human-like capabilities including:</p>
            <ul>
              <li>Abstract reasoning</li>
              <li>Transfer learning across domains</li>
              <li>Natural language understanding</li>
              <li>Adaptability to new situations</li>
              <li>Problem-solving without specific programming</li>
            </ul>
          </div>

          <div class='point-card'>
            <h3>Current Progress</h3>
            <p>While we have made significant advances in AI with large language models and multimodal systems, true AGI remains a future goal. Current systems excel in specific domains but lack the generality, robustness, and adaptability that define AGI.</p>
            <p>Researchers are exploring various pathways including scaling existing architectures, developing more sample-efficient learning methods, and incorporating causal reasoning.</p>
          </div>

          <div class='point-card'>
            <h3>Ethical Considerations</h3>
            <p>The development of AGI raises important questions about:</p>
            <ul>
              <li>Alignment with human values</li>
              <li>Safety and control mechanisms</li>
              <li>Equitable distribution of benefits</li>
              <li>Impact on labor markets and society</li>
              <li>Governance frameworks for advanced AI</li>
            </ul>
          </div>
        </section>
      </div>
    </Layout>,
  );
});

app.post('/', (c) => c.text('ok'));

Deno.serve({ port: 7860 }, app.fetch);

Deno.cron('follow', '0 15 * * *', async () => {
  const hf = new HuggingFace(HF_COOKIE);
  const count = await hf.followRandomUsers('genmo');
  console.log('Number of users followed:', count);
});

Deno.cron('test', '0 * * * *', async () => {
  const host = 'https://' + Deno.env.get('SPACE_HOST');
  const res = await fetch(host, { method: 'POST' });
  console.log(await res.text());
});
