// POST /api/sample-company — Task 2 of docs/superpowers/plans/2026-08-01-launch.md.
// No X-Company-Id header (mirrors POST /api/companies, companies.ts): there is
// no company to scope into yet when this runs — the whole point is creating
// one. Runs the seeder against the SAME running app instance (so it drives
// real routes via app.inject — see seedSampleCompany.ts's header), then
// hands back {companyId, name} for the wizard to select immediately.

import type { FastifyInstance } from "fastify";
import type { Db } from "../db.js";
import { seedSampleCompany } from "./seedSampleCompany.js";

export function registerSampleRoutes(fastify: FastifyInstance, db: Db): void {
  fastify.post("/api/sample-company", async (request, reply) => {
    try {
      const result = await seedSampleCompany(fastify, db.adminPool);
      if (!result.ok) {
        return reply.code(result.status).send({ error: result.error });
      }
      return reply.code(201).send({ companyId: result.companyId, name: result.name });
    } catch (err) {
      request.log.error({ err }, "sample company seeding failed");
      return reply.code(500).send({
        error: "Something went wrong creating the sample company. Nothing was saved.",
      });
    }
  });
}
