import * as Pulumi from "@pulumi/pulumi"
import * as Constants from "../constants"
import { ProcessProvider } from "./provider"

export interface ProcessInputProps {
  code?: Pulumi.Input<string>
  codeId?: Pulumi.Input<string>
  name?: Pulumi.Input<string>
  customTags?: Pulumi.Input<Record<string, string>>
  gatewayUrl?: Pulumi.Input<string>
  walletPath?: Pulumi.Input<string>
  environment?: Pulumi.Input<Record<string, string | Pulumi.Output<string>>>
  moduleId?: Pulumi.Input<string>
  schedulerId?: Pulumi.Input<string>
  authorityId?: Pulumi.Input<string>
}

/**
 * Represents a process on AO
 * Executes Lua code on spawn and sets global Environment variables.
 */
export class Process extends Pulumi.dynamic.Resource {
  public readonly owner!: Pulumi.Output<string>
  public readonly name!: Pulumi.Output<string>
  public readonly code!: Pulumi.Output<string>
  public readonly codeId!: Pulumi.Output<string>
  public readonly gatewayUrl!: Pulumi.Output<string>
  public readonly walletPath!: Pulumi.Output<string>
  public readonly moduleId!: Pulumi.Output<string>
  public readonly schedulerId!: Pulumi.Output<string>
  public readonly authority!: Pulumi.Output<string>
  public readonly customTags!: Pulumi.Output<Record<string, string>>
  public readonly environment!: Pulumi.Output<Record<string, string>>
  public readonly tags!: Pulumi.Output<Record<string, string>>

  constructor(
    name: string,
    inputProps: ProcessInputProps,
    options?: Pulumi.CustomResourceOptions
  ) {
    inputProps.name = inputProps.name ?? name
    inputProps.moduleId = inputProps.moduleId ?? Constants.AOS_2_0_1_CODE_ID
    inputProps.authorityId =
      inputProps.authorityId ?? Constants.TESTNET_AUTHORITY_ID
    inputProps.customTags = inputProps.customTags ?? {}
    inputProps.environment = inputProps.environment ?? {}

    const config = new Pulumi.Config("ao")

    inputProps.schedulerId =
      config.get("schedulerId") ?? Constants.DEFAULT_SCHEDULER_ID

    inputProps.gatewayUrl =
      inputProps.gatewayUrl ??
      config.get("gatewayUrl") ??
      Constants.DEFAULT_GATEWAY_URL

    inputProps.walletPath =
      inputProps.walletPath ?? config.require("walletPath")

    // props generated by the provider, can't be directly changed by the user
    const outputProps = { owner: "", tags: {} }

    const allProps = { ...inputProps, ...outputProps }

    super(new ProcessProvider(), name, allProps, options, "ao", "Process")
  }
}
