'use strict';

const express = require('express');
const path = require('path');
const { pool } = require('../lib/db');

const router = express.Router();
const SERVER_START = Date.now();

// Serve page
router.get('/', (req, res) => {
  res.sendFile(path.resolve(__dirname, '../public/status.html'));
});

// Status API — neutral, uptime-focused (like status.claude.com)
router.get('/api', async (req, res) => {
  const services = [];

  // API
  services.push({ name: 'API', status: 'operational' });

  // Database
  try {
    await pool.execute('SELECT 1');
    services.push({ name: 'Database', status: 'operational' });
  } catch {
    services.push({ name: 'Database', status: 'outage' });
  }

  // Image Processing
  services.push({ name: 'Image Processing', status: 'operational' });

  // Video Processing
  services.push({ name: 'Video Processing', status: 'operational' });

  // Authentication
  services.push({ name: 'Authentication', status: 'operational' });

  const overall = services.some(s => s.status === 'outage') ? 'outage'
    : services.some(s => s.status === 'degraded') ? 'degraded'
    : 'operational';

  res.json({
    overall,
    uptime: Math.floor((Date.now() - SERVER_START) / 1000),
    services,
    timestamp: new Date().toISOString(),
  });
});

module.exports = router;
