/**
 * {{titleCase name}} — Temporal Workflow Application
 *
 * Infrastructure: AppEnvironment + API Service + TemporalWorker
 *
 * Dev mode:  pnpm mesh dev
 * Deploy:    pnpm mesh deploy up
 */

import * as pulumi from "@pulumi/pulumi";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import { mesh } from "@mesh-tech/app-kit/infra";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const { tenant, platform, stack, deploy } = mesh.apps.getConfig(pulumi);

const TASK_QUEUE = "{{name}}";

// --- Environment ---

const env = new mesh.apps.AppEnvironment("env", {
  tenant,
  platform,
  stack,
  deploy,
  appName: "{{name}}",
  namespace: true,
  rootDir: __dirname,
  temporal: true,
});

// --- Services ---

const api = new mesh.apps.Service("api", {
  env,
  runtime: "node",
  src: "./api",
  port: 3000,
  link: [
    env.temporal!.link({
      environmentPrefix: "TEMPORAL",
      taskQueue: TASK_QUEUE,
    }),
  ],
  ingress: env.buildIngress({
    subdomain: "{{name}}-api",
    healthCheckPath: "/health",
  }),
  replicas: 1,
  resources: { cpu: "100m", memory: "128Mi" },
});

const worker = new mesh.apps.TemporalWorker("worker", {
  env,
  runtime: "node",
  src: "./worker",
  taskQueue: TASK_QUEUE,
  replicas: 1,
  resources: { cpu: "200m", memory: "256Mi" },
});

// --- Outputs ---

export const app = env.register({
  description: "{{titleCase name}} — Temporal workflow app",
});
