import type { FastifyInstance } from "fastify";
import { getActivationState, schema } from "@app/database";
import { errors } from "../lib/errors.js";
import { getActiveHouseholdId } from "../lib/helpers.js";

/**
 * Hushållsaktiveringsstatus (spec §4.2):
 *   - ACTIVATED_HOUSEHOLD
 *   - VALUE_CONFIRMED_HOUSEHOLD
 */
export async function activationRoutes(app: FastifyInstance) {
  const auth = { preHandler: [app.authenticate] };

  app.get("/v1/households/me/activation", auth, async (req) => {
    const householdId = await getActiveHouseholdId(app.db, req.userId);
    if (!householdId) throw errors.notFound("Aktivt hushåll saknas.");

    const state = await getActivationState(app.db, householdId);
    return {
      ...state,
      isActivated: state.activatedAt != null,
      isValueConfirmed: state.valueConfirmedAt != null,
    };
  });
}
