import { Schema } from "effect";

// ---------------------------------------------------------------------------
// Auth — how to talk to 1Password
// ---------------------------------------------------------------------------

export const DesktopAppAuth = Schema.Struct({
  kind: Schema.Literal("desktop-app"),
  /** 1Password account domain, e.g. "my.1password.com" */
  accountName: Schema.String,
});
export type DesktopAppAuth = typeof DesktopAppAuth.Type;

export const ServiceAccountAuth = Schema.Struct({
  kind: Schema.Literal("service-account"),
  /** The service account token (stored as a secret) */
  tokenSecretId: Schema.String,
});
export type ServiceAccountAuth = typeof ServiceAccountAuth.Type;

export const OnePasswordAuth = Schema.Union([DesktopAppAuth, ServiceAccountAuth]);
export type OnePasswordAuth = typeof OnePasswordAuth.Type;

// ---------------------------------------------------------------------------
// Stored config — persisted via KV
// ---------------------------------------------------------------------------

export const OnePasswordConfig = Schema.Struct({
  auth: OnePasswordAuth,
  /** Vault to scope operations to */
  vaultId: Schema.String,
  /** Human label */
  name: Schema.String,
});
export type OnePasswordConfig = typeof OnePasswordConfig.Type;

// ---------------------------------------------------------------------------
// Vault
// ---------------------------------------------------------------------------

export const Vault = Schema.Struct({
  id: Schema.String,
  name: Schema.String,
});
export type Vault = typeof Vault.Type;

// ---------------------------------------------------------------------------
// Connection status
// ---------------------------------------------------------------------------

export const ConnectionStatus = Schema.Struct({
  connected: Schema.Boolean,
  vaultName: Schema.optional(Schema.String),
  error: Schema.optional(Schema.String),
});
export type ConnectionStatus = typeof ConnectionStatus.Type;
