// POST /components/validate-contract
// Requirements: 1.1, 1.2, 11.5

import { Hono } from 'hono';
import type { Bindings } from '../types';
import { validateContract } from '../actions/validateContract';

const app = new Hono<{ Bindings: Bindings }>();

app.post('/', async c => {
  let body: unknown;
  try {
    body = await c.req.json();
  } catch {
    return c.json({ valid: false, missing_fields: [], errors: ['request body 必須為合法 JSON'] }, 400);
  }

  const result = validateContract(body);

  if (result.valid) {
    return c.json({ valid: true, missing_fields: [], errors: [] }, 200);
  }

  return c.json({
    valid: false,
    missing_fields: result.missing_fields,
    errors: result.errors,
  }, 422);
});

export default app;
