Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
189 KB
Raw Blame
import { z } from "zod";
import { normalizeIssueStatusCategory } from "../issues/config/status";
import type {
  BatchIssueOutcome,
  BatchIssueResult,
  BatchUpdateIssuesResponse,
  AppCatalogResponse,
  AppsOverview,
  TeamAppImpact,
  WorkflowHandoffPreview,
  IssueWorkflow,
  IssueWorkflowDryRunResponse,
  IssueWorkflowHandlerType,
  ListIssueWorkflowsResponse,
  AgentBuilderRuntimeSwitch,
  AgentBuilderSession,
  AgentBuilderSessionSummary,
  Attachment,
  AutopilotRun,
  BillingBalance,
  BillingBatchesPage,
  BillingCheckoutSessionStatus,
  BillingPriceTier,
  BillingTopupsPage,
  BillingTransactionsPage,
  CancelTaskResponse,
  ChatMessage,
  ChatDraftRestoresResponse,
  ChatPendingTask,
  ChatSession,
  ChatWakeup,
  PrioritizeQueuedChatTaskResponse,
  SendChatMessageResponse,
  StartMikaOnboardingResponse,
  Comment,
  CreateBillingCheckoutSessionResponse,
  CreateBillingPortalSessionResponse,
  WorkspaceSubscriptionEntitlements,
  WorkspaceFeatureState,
  WorkspaceFeatures,
  MemberLimit,
  StorageUsage,
  AppCallUsage,
  WorkspaceSubscriptionSummary,
  WorkspaceSubscriptionPrice,
  WorkspaceSubscriptionPlanPrices,
  WorkspaceSubscriptionPrices,
  WorkspacePlanLimits,
  CreateWorkspaceAutopilotAddonCheckoutResponse,
  WorkspaceAutopilotAddonCheckout,
  WorkspaceAutopilotAddonPrice,
  CreateWorkspaceSubscriptionCheckoutResponse,
  WorkspacePlanChangePreview,
  ChangeWorkspacePlanResponse,
  WorkspaceSubscriptionSeatReconcileResult,
  WorkspaceSeatPurchasePreview,
  PurchaseWorkspaceSeatsResponse,
  CreateWorkspaceSubscriptionPortalResponse,
  CronPreviewResponse,
  DingTalkInstallation,
  ListDingTalkInstallationsResponse,
  ListDingTalkGroupsResponse,
  RedeemDingTalkBindingTokenResponse,
  WecomInstallation,
  ListLarkInstallationsResponse,
  ListWecomInstallationsResponse,
  BeginWecomInstallResponse,
  WecomInstallStatusResponse,
  RedeemWecomBindingTokenResponse,
  TelegramInstallation,
  ListTelegramInstallationsResponse,
  RedeemTelegramBindingTokenResponse,
  GitHubConnectResponse,
  IssuePullRequestsResponse,
  IssueEnvironmentsResponse,
  IssuePullRequestSummariesResponse,
  InboxItem,
  SnoozedInboxIssue,
  InboxWorkspaceUnread,
  IssueUnread,
  Label,
  Project,
  ProjectProgressDetail,
  ProjectActivityPage,
  ListProjectsResponse,
  MemberWithUser,
  IssueProperty,
  ListPropertiesResponse,
  QuickAction,
  ListQuickActionsResponse,
  IssuePropertiesResponse,
  IssueTableGroupDescriptor,
  IssueTableFacetsResponse,
  IssueTableGroupsResponse,
  IssueTableRowsResponse,
  ListIssuesResponse,
  ListGitHubInstallationsResponse,
  ListGitHubRepositoriesResponse,
  ListLabelsResponse,
  ListWebhookDeliveriesResponse,
  IssueStatusEntry,
  ListIssueStatusesResponse,
  NotificationPreferenceResponse,
  PluginInstallation,
  PluginInstallationListResponse,
  PluginPackage,
  PluginPackageListResponse,
  PluginPreview,
  PluginSurfaceLaunch,
  ResourceLabelsResponse,
  RuntimeModelListRequest,
  SearchIssuesResponse,
  SearchProjectsResponse,
  ShareLink,
  ShareLinkInfo,
  Skill,
  SkillImportResult,
  SkillSummary,
  SkillVersion,
  SkillVersionSummary,
  Squad,
  TimelineEntry,
  User,
  WebhookDelivery,
  WorkspaceMcpServer,
} from "../types";
import type { CloudRuntimeNode } from "../runtimes/cloud-runtime";
import type { CreateFeedbackResponse } from "../feedback/types";

export const PluginConfigFieldSchema = z.object({
  key: z.string(),
  type: z.string().default("string"),
  label: z.string().default(""),
  description: z.string().optional(),
  required: z.boolean().default(false),
  options: z.array(z.string()).default([]),
  placeholder: z.string().optional(),
  multiline: z.boolean().default(false),
}).loose();

export const PluginSurfaceSchema = z.object({
  key: z.string(),
  type: z.string().default(""),
  name: z.string().default(""),
  entry: z.string().default(""),
  platforms: z.array(z.string()).default([]),
}).loose();

export const PluginHookSchema = z.object({
  key: z.string(),
  name: z.string().default(""),
  description: z.string().default(""),
  triggers: z.array(z.string()).default([]),
  events: z.array(z.string()).default([]),
  schedule: z.object({
    cron: z.string().default(""),
    timezone: z.string().default(""),
    next_run_at: z.string().optional(),
  }).loose().optional(),
  transport: z.string().default(""),
}).loose();

export const PluginResourceSchema = z.object({
  type: z.string().default(""),
  key: z.string(),
  entry: z.string().default(""),
}).loose();

export const PluginInstallationSchema = z.object({
  id: z.string(),
  plugin_key: z.string().default(""),
  name: z.string().default(""),
  description: z.string().optional(),
  version: z.string().default(""),
  package_version_id: z.string().default(""),
  enabled: z.boolean().default(false),
  granted_scopes: z.array(z.string()).default([]),
  config_schema: z.array(PluginConfigFieldSchema).default([]),
  config: z.record(z.string(), z.unknown()).default({}),
  configured_secrets: z.array(z.string()).default([]),
  surfaces: z.array(PluginSurfaceSchema).default([]),
  hooks: z.array(PluginHookSchema).default([]),
  resources: z.array(PluginResourceSchema).default([]),
  created_at: z.string().default(""),
  updated_at: z.string().default(""),
}).loose();

export const EMPTY_PLUGIN_INSTALLATION: PluginInstallation = {
  id: "",
  plugin_key: "",
  name: "",
  version: "",
  package_version_id: "",
  enabled: false,
  granted_scopes: [],
  config_schema: [],
  config: {},
  configured_secrets: [],
  surfaces: [],
  hooks: [],
  resources: [],
  created_at: "",
  updated_at: "",
};

export const PluginInstallationListResponseSchema = z.object({
  plugins: z.array(PluginInstallationSchema).default([]),
}).loose();

export const EMPTY_PLUGIN_INSTALLATION_LIST: PluginInstallationListResponse = {
  plugins: [],
};

/**
 * One completed hook call. `status` is the host's classification, not the
 * endpoint's: "refused" means we declined to make the call at all, which is a
 * different problem for the reader than an endpoint that answered badly.
 */
export const PluginHookResultSchema = z.object({
  status: z.string().default("ok"),
  output: z.unknown().optional(),
  error: z.string().optional(),
  latency_ms: z.number().default(0),
  hook_key: z.string().default(""),
  trigger: z.string().default(""),
  attempts: z.number().default(1),
}).loose();

export const PluginInvocationSchema = z.object({
  id: z.string().default(""),
  hook_key: z.string().default(""),
  trigger: z.string().default(""),
  status: z.string().default(""),
  event_type: z.string().optional(),
  attempt: z.number().default(1),
  latency_ms: z.number().default(0),
  error: z.string().optional(),
  delivery_id: z.string().optional(),
  planned_at: z.string().optional(),
  created_at: z.string().default(""),
}).loose();

export const PluginInvocationListSchema = z.object({
  invocations: z.array(PluginInvocationSchema).default([]),
}).loose();

/**
 * Returned once, by the request that minted it. There is no read endpoint for
 * either value, so a client that discards this cannot recover it.
 */
export const PluginTokenIssueSchema = z.object({
  token: z.string().default(""),
  signing_secret: z.string().default(""),
}).loose();

/**
 * Discovered tools for one `mcp`-transport hook.
 *
 * Defaults matter here in the usual direction: an unparseable response yields
 * an EMPTY list and nothing approved, so a drifted backend cannot make the UI
 * render a tool as already-approved.
 */
export const PluginMCPToolSchema = z.object({
  name: z.string().default(""),
  description: z.string().default(""),
  schema_digest: z.string().default(""),
  approved: z.boolean().default(false),
  drifted: z.boolean().default(false),
}).loose();

export const PluginMCPToolListSchema = z.object({
  tools: z.array(PluginMCPToolSchema).default([]),
}).loose();

export const PluginManifestSummarySchema = z.object({
  key: z.string().default(""),
  name: z.string().default(""),
  description: z.string().optional(),
  version: z.string().default(""),
  author: z.object({
    name: z.string().default(""),
    url: z.string().optional(),
  }).loose().default({ name: "" }),
  contributes: z.object({
    hooks: z.array(z.object({
      key: z.string().default(""),
      name: z.string().default(""),
      triggers: z.array(z.string()).default([]),
      schedule: z.object({
        cron: z.string().default(""),
        timezone: z.string().default(""),
      }).loose().optional(),
    }).loose()).default([]),
  }).loose().optional(),
}).loose();

export const PluginPreviewSchema = z.object({
  manifest: PluginManifestSummarySchema,
  scopes: z.array(z.string()).default([]),
  config_schema: z.array(PluginConfigFieldSchema).default([]),
  version_id: z.string().default(""),
  version: z.string().default(""),
  digest: z.string().default(""),
  installed: z.boolean().default(false),
  installed_version: z.string().optional(),
  added_scopes: z.array(z.string()).default([]),
}).loose();

export const EMPTY_PLUGIN_PREVIEW: PluginPreview = {
  manifest: { key: "", name: "", version: "", author: { name: "" } },
  scopes: [],
  config_schema: [],
  version_id: "",
  version: "",
  digest: "",
  installed: false,
  added_scopes: [],
};

/**
 * A published version. `installed` is the marker the settings page reads to
 * answer "which one am I on"; it defaults to false so a malformed response can
 * never claim a version is running that is not.
 */
export const PluginPackageVersionSchema = z.object({
  id: z.string().default(""),
  version: z.string().default(""),
  digest: z.string().default(""),
  size_bytes: z.number().default(0),
  published_at: z.string().default(""),
  installed: z.boolean().default(false),
}).loose();

export const PluginPackageSchema = z.object({
  id: z.string().default(""),
  plugin_key: z.string().default(""),
  name: z.string().default(""),
  versions: z.array(PluginPackageVersionSchema).default([]),
  created_at: z.string().default(""),
}).loose();

export const PluginPackageListResponseSchema = z.object({
  packages: z.array(PluginPackageSchema).default([]),
}).loose();

export const EMPTY_PLUGIN_PACKAGE_LIST: PluginPackageListResponse = {
  packages: [],
};

export const EMPTY_PLUGIN_PACKAGE: PluginPackage = {
  id: "",
  plugin_key: "",
  name: "",
  versions: [],
  created_at: "",
};

/** A malformed launch becomes unavailable, never a partly trusted frame. */
export const PluginSurfaceLaunchSchema = z.object({
  url: z.string().default(""),
  bridge_token: z.string().default(""),
  version: z.string().default(""),
  digest: z.string().default(""),
}).loose();

export const EMPTY_PLUGIN_SURFACE_LAUNCH: PluginSurfaceLaunch = {
  url: "",
  bridge_token: "",
  version: "",
  digest: "",
};

export const GitHubInstallationSchema = z.object({
  id: z.string(),
  workspace_id: z.string(),
  installation_id: z.number().optional(),
  account_login: z.string(),
  account_type: z.string(),
  account_avatar_url: z.string().nullable(),
  created_at: z.string(),
  connected_by: z.string().optional(),
}).loose();

export const ListGitHubInstallationsResponseSchema = z.object({
  installations: z.array(GitHubInstallationSchema).default([]),
  configured: z.boolean().optional().default(false),
  repository_browse_configured: z.boolean().optional().default(false),
  can_manage: z.boolean().optional().default(false),
}).loose();

export const EMPTY_LIST_GITHUB_INSTALLATIONS_RESPONSE: ListGitHubInstallationsResponse = {
  installations: [],
  configured: false,
  repository_browse_configured: false,
  can_manage: false,
};

export const GitHubConnectResponseSchema = z.object({
  connection_id: z.string().optional().catch(undefined),
  url: z.string().optional(),
  configured: z.boolean().optional().default(false),
}).loose();

export const EMPTY_GITHUB_CONNECT_RESPONSE: GitHubConnectResponse = {
  configured: false,
};

export const GitHubRepositorySchema = z.object({
  id: z.number(),
  full_name: z.string(),
  html_url: z.string(),
  clone_url: z.string(),
  description: z.string().nullable(),
  private: z.boolean(),
  archived: z.boolean(),
  default_branch: z.string(),
}).loose();

export const ListGitHubRepositoriesResponseSchema = z.object({
  repository_selection: z.enum(["all", "selected", "unknown"]).catch("unknown").optional(),
  repositories: z.array(GitHubRepositorySchema).default([]),
  total_count: z.number().optional().default(0),
  next_page: z.number().nullable().optional().default(null),
}).loose();

export const EMPTY_LIST_GITHUB_REPOSITORIES_RESPONSE: ListGitHubRepositoriesResponse = {
  repositories: [],
  total_count: 0,
  next_page: null,
};

export const GitHubPullRequestSchema = z.object({
  id: z.string(),
  provider: z.string().optional().default("github"),
  workspace_id: z.string(),
  repo_owner: z.string(),
  repo_name: z.string(),
  number: z.number(),
  title: z.string(),
  state: z.string(),
  html_url: z.string(),
  branch: z.string().nullable(),
  author_login: z.string().nullable(),
  author_avatar_url: z.string().nullable(),
  merged_at: z.string().nullable(),
  closed_at: z.string().nullable(),
  pr_created_at: z.string(),
  pr_updated_at: z.string(),
  link_source: z.enum(["manual", "title", "branch", "auto"]).optional().catch(undefined),
  mergeable: z.string().nullable().optional(),
  merge_state_status: z.string().nullable().optional(),
  snapshot_available: z.boolean().optional(),
  checks_rollup: z.string().nullable().optional(),
  checks_conclusion: z.string().nullable().optional(),
  checks_total: z.number().optional().default(0),
  checks_passed: z.number().optional().default(0),
  checks_failed: z.number().optional().default(0),
  checks_running: z.number().optional().default(0),
  checks_pending: z.number().optional().default(0),
  failed_check_names: z.array(z.string()).optional().default([]),
  snapshot_stale: z.boolean().optional().default(false),
  snapshot_fetched_at: z.string().nullable().optional(),
  mergeable_state: z.string().nullable().optional(),
  additions: z.number().optional().default(0),
  deletions: z.number().optional().default(0),
  changed_files: z.number().optional().default(0),
}).loose();

// A malformed auto_complete block degrades to null (the issue page shows no
// automation line) instead of discarding the PR list with it.
export const PRAutoCompleteSchema = z.object({
  state: z.string(),
  pull_request_ids: z.array(z.string()).default([]),
  issue_disabled: z.boolean().default(false),
  workspace_enabled: z.boolean().default(true),
  target_status: z.string().optional().catch(undefined),
}).loose();

export const IssuePullRequestsResponseSchema = z.object({
  pull_requests: z.array(GitHubPullRequestSchema).default([]),
  auto_complete: PRAutoCompleteSchema.nullable().optional().default(null).catch(null),
}).loose();

export const EMPTY_ISSUE_PULL_REQUESTS_RESPONSE: IssuePullRequestsResponse = {
  pull_requests: [],
  auto_complete: null,
};

// An action record from a newer server with an unknown action is dropped;
// an unknown status reads as failed, which offers a retry.
export const IssueEnvironmentActionSchema = z.object({
  id: z.string(),
  action: z.enum(["stop", "restart", "start", "logs"]),
  status: z.enum(["pending", "succeeded", "failed", "expired"]).catch("failed"),
  error: z.string().optional().default(""),
  environment_id: z.string().nullable().optional().default(null),
  runtime_id: z.string().optional().default(""),
  task_id: z.string().nullable().optional().default(null),
  repo: z.string().optional().default(""),
  name: z.string().optional().default(""),
  actor_type: z.string().optional().default("member"),
  actor_id: z.string().nullable().optional().default(null),
  created_at: z.string(),
  completed_at: z.string().nullable().optional().default(null),
}).loose();

const OptionalIssueEnvironmentActionSchema = IssueEnvironmentActionSchema.nullable().optional().default(null).catch(null);

export const IssueEnvironmentCommandSchema = IssueEnvironmentActionSchema.extend({
  output: z.string().nullable().optional().default(null),
}).loose();

// One malformed environment drops out of the list instead of taking the
// whole section with it; an unknown state from a newer server reads as
// unreachable, which offers no Open action. A kind this client does not know,
// or none from an older server, reads as a server: the row then opens its URL
// if it has one and offers nothing otherwise. A missing or malformed
// local_key reads as empty, which leaves an app row without its switch action.
export const IssueEnvironmentSchema = z.object({
  id: z.string(),
  issue_id: z.string(),
  task_id: z.string(),
  runtime_id: z.string().nullable().optional().default(null),
  agent_id: z.string().nullable().optional().default(null),
  local_key: z.string().catch(""),
  source: z.enum(["config", "detected"]).catch("detected"),
  kind: z.enum(["server", "app"]).catch("server"),
  repo: z.string().optional().default(""),
  name: z.string(),
  label: z.string().optional().default(""),
  url: z.string().optional().default(""),
  port: z.number().nullable().optional().default(null),
  state: z.enum(["starting", "running", "exited", "stopped", "unreachable"]).catch("unreachable"),
  detail: z.string().optional().default(""),
  excerpt: z.string().optional().default(""),
  started_at: z.string(),
  expires_at: z.string().nullable().optional().default(null),
  last_seen_at: z.string(),
  ended_at: z.string().nullable().optional().default(null),
  // Older servers predate sidebar control: everything reads as read-only.
  controllable: z.boolean().optional().default(false),
  can_control: z.boolean().optional().default(false),
  pending_action: OptionalIssueEnvironmentActionSchema,
  last_action: OptionalIssueEnvironmentActionSchema,
  origin: OptionalIssueEnvironmentActionSchema,
}).loose();

const DeclaredEnvironmentSchema = z.object({
  repo: z.string(),
  name: z.string(),
  label: z.string().optional().default(""),
  start: z.string().optional().default(""),
}).loose();

const IssueEnvironmentConfigSchema = z.object({
  runtime_id: z.string(),
  task_id: z.string(),
  reported_at: z.string(),
  can_control: z.boolean().optional().default(false),
  environments: z.array(DeclaredEnvironmentSchema).default([]),
}).loose();

function lenientArray<T extends z.ZodType>(schema: T) {
  return z
    .array(z.unknown())
    .optional()
    .default([])
    .transform((items) =>
      items.flatMap((item) => {
        const parsed = schema.safeParse(item);
        return parsed.success ? [parsed.data as z.infer<T>] : [];
      }),
    );
}

export const IssueEnvironmentsResponseSchema = z.object({
  environments: lenientArray(IssueEnvironmentSchema),
  configs: lenientArray(IssueEnvironmentConfigSchema),
  pending_starts: lenientArray(IssueEnvironmentActionSchema),
  failed_starts: lenientArray(IssueEnvironmentActionSchema),
}).loose();

export const EMPTY_ISSUE_ENVIRONMENTS_RESPONSE: IssueEnvironmentsResponse = {
  environments: [],
  configs: [],
  pending_starts: [],
  failed_starts: [],
};

// A malformed row is dropped rather than failing the whole workspace read, so
// one bad link never blanks the PR chips on every card.
const IssuePullRequestSummarySchema = z.object({
  issue_id: z.string(),
  id: z.string(),
  provider: z.string().optional().default("github"),
  repo_owner: z.string(),
  repo_name: z.string(),
  number: z.number(),
  title: z.string().optional().default(""),
  state: z.string(),
  html_url: z.string(),
}).loose();

export const IssuePullRequestSummariesResponseSchema = z.object({
  pull_requests: lenientArray(IssuePullRequestSummarySchema),
}).loose();

export const EMPTY_ISSUE_PULL_REQUEST_SUMMARIES_RESPONSE: IssuePullRequestSummariesResponse = {
  pull_requests: [],
};

// Label responses are consumed by settings tables and resource pickers. Keep
// the resource type lenient so newer server scopes do not break older clients,
// while defaulting fields that predate scoped label catalogs.
export const LabelSchema = z.object({
  id: z.string(),
  workspace_id: z.string(),
  resource_type: z.string().optional().default("issue"),
  name: z.string(),
  description: z.string().optional().default(""),
  color: z.string(),
  usage_count: z.number().optional().default(0),
  created_at: z.string(),
  updated_at: z.string(),
}).loose();

export const EMPTY_LABEL: Label = {
  id: "",
  workspace_id: "",
  resource_type: "issue",
  name: "",
  description: "",
  color: "#6b7280",
  usage_count: 0,
  created_at: "",
  updated_at: "",
};

export const ListLabelsResponseSchema = z.object({
  labels: z.array(LabelSchema).default([]),
  total: z.number().default(0),
}).loose();

export const EMPTY_LIST_LABELS_RESPONSE: ListLabelsResponse = {
  labels: [],
  total: 0,
};

// Issue status catalog (MUL-6243). `category` is parsed as a plain string
// rather than an enum: a newer server could in principle report a category this
// build does not know, and failing the whole catalog parse would leave the UI
// with no statuses at all. Consumers fall back to rendering by `color`/`name`
// when they do not recognize a category.
export const IssueStatusEntrySchema = z.object({
  id: z.string(),
  workspace_id: z.string(),
  key: z.string(),
  name: z.string(),
  description: z.string().optional().default(""),
  category: z.string().transform((value) => normalizeIssueStatusCategory(value) ?? value),
  color: z.string().optional().default("#6b7280"),
  icon: z.string().nullable().optional(),
  is_system: z.boolean().optional().default(false),
  position: z.number().optional().default(0),
  archived_at: z.string().nullable().optional().default(null),
  created_at: z.string(),
  updated_at: z.string(),
}).loose();

export const EMPTY_ISSUE_STATUS_ENTRY: IssueStatusEntry = {
  id: "",
  workspace_id: "",
  key: "",
  name: "",
  description: "",
  category: "unstarted",
  color: "#6b7280",
  is_system: false,
  position: 0,
  archived_at: null,
  created_at: "",
  updated_at: "",
};

export const ListIssueStatusesResponseSchema = z.object({
  statuses: z.array(IssueStatusEntrySchema).default([]),
  categories: z.array(z.string()).default([]).transform((values) => [...new Set(values.map((value) => normalizeIssueStatusCategory(value) ?? value))]),
  total: z.number().default(0),
}).loose();

// The fallback carries the four lifecycle categories. Concrete built-in status
// keys remain available through the status configuration.
export const EMPTY_LIST_ISSUE_STATUSES_RESPONSE: ListIssueStatusesResponse = {
  statuses: [],
  categories: ["unstarted", "started", "done", "closed"],
  total: 0,
};

// Workspace workflows (MUL-7420). Handler types parse as plain strings and an
// unknown one degrades to "none": a newer server may add a handler this build
// cannot render, and the step's status must still show.
const IssueWorkflowHandlerSchema = z.object({
  type: z.string().transform((value): IssueWorkflowHandlerType =>
    ISSUE_WORKFLOW_HANDLER_TYPES.has(value) ? (value as IssueWorkflowHandlerType) : "none",
  ),
  id: z.string().optional(),
}).loose();

const ISSUE_WORKFLOW_HANDLER_TYPES = new Set<string>([
  "none", "agent", "squad", "member", "project_lead", "creator",
]);

const IssueWorkflowStepSchema = z.object({
  status_key: z.string(),
  handler: IssueWorkflowHandlerSchema.default({ type: "none" }),
  instructions: z.string().default(""),
  next_status_key: z.string().optional(),
  back_status_key: z.string().optional(),
}).loose();

export const IssueWorkflowSchema = z.object({
  id: z.string(),
  workspace_id: z.string(),
  name: z.string(),
  description: z.string().default(""),
  is_default: z.boolean().default(false),
  owner_id: z.string().nullable().default(null),
  steps: z.array(IssueWorkflowStepSchema).default([]),
  project_ids: z.array(z.string()).default([]),
  created_at: z.string(),
  updated_at: z.string(),
}).loose();

export const ListIssueWorkflowsResponseSchema = z.object({
  workflows: z.array(IssueWorkflowSchema).default([]),
  total: z.number().default(0),
}).loose();

export const EMPTY_LIST_ISSUE_WORKFLOWS_RESPONSE: ListIssueWorkflowsResponse = {
  workflows: [],
  total: 0,
};

export const EMPTY_ISSUE_WORKFLOW: IssueWorkflow = {
  id: "",
  workspace_id: "",
  name: "",
  description: "",
  is_default: false,
  owner_id: null,
  steps: [],
  project_ids: [],
  created_at: "",
  updated_at: "",
};

export const IssueWorkflowMappingPlanSchema = z.object({
  required: z.array(z.object({
    status_key: z.string(),
    issue_count: z.number().default(0),
    suggested_status_key: z.string().default(""),
  }).loose()).default([]),
  unchanged: z.array(z.object({
    status_key: z.string(),
    issue_count: z.number().default(0),
  }).loose()).default([]),
  total_issues: z.number().default(0),
  affected_issues: z.number().default(0),
}).loose();

export const IssueWorkflowDryRunResponseSchema = z.object({
  dry_run: z.literal(true).default(true),
  plan: IssueWorkflowMappingPlanSchema.default({ required: [], unchanged: [], total_issues: 0, affected_issues: 0 }),
}).loose();

export const EMPTY_ISSUE_WORKFLOW_DRY_RUN: IssueWorkflowDryRunResponse = {
  dry_run: true,
  plan: { required: [], unchanged: [], total_issues: 0, affected_issues: 0 },
};

export const IssueWorkflowBriefPreviewSchema = z.object({
  brief: z.string().default(""),
}).loose();

const HANDOFF_HANDLER_TYPES = new Set(["agent", "squad", "member"]);

export const WorkflowHandoffPreviewSchema = z.object({
  handoff: z.boolean().default(false),
  workflow_name: z.string().default(""),
  from_status: z.string().default(""),
  to_status: z.string().default(""),
  handler_type: z.string().optional().transform((value): WorkflowHandoffPreview["handler_type"] =>
    value && HANDOFF_HANDLER_TYPES.has(value) ? (value as "agent" | "squad" | "member") : null,
  ),
  handler_id: z.string().nullable().optional().transform((value) => value || null),
  previous_assignee_type: z.string().nullable().default(null),
  previous_assignee_id: z.string().nullable().default(null),
  previous_runs: z.array(z.object({
    task_id: z.string(),
    agent_id: z.string(),
    status: z.string().default(""),
    started_at: z.string().nullable().default(null),
  }).loose()).default([]),
  brief: z.string().default(""),
}).loose();

// A preview the client cannot read says "no handoff": the write path still
// applies the workflow, it only loses the confirmation.
export const EMPTY_WORKFLOW_HANDOFF_PREVIEW: WorkflowHandoffPreview = {
  handoff: false,
  workflow_name: "",
  from_status: "",
  to_status: "",
  handler_type: null,
  handler_id: null,
  previous_assignee_type: null,
  previous_assignee_id: null,
  previous_runs: [],
  brief: "",
};

export const ResourceLabelsResponseSchema = z.object({
  labels: z.array(LabelSchema).default([]),
  issue_revision: z.number().int().positive().optional(),
}).loose();

export const EMPTY_RESOURCE_LABELS_RESPONSE: ResourceLabelsResponse = {
  labels: [],
};

// Saved issue views (MUL-4796). `query`/`display` are opaque definition
// blobs interpreted client-side per `definition_version` — keep them as
// loose records so newer servers can add fields freely. `scope_type` /
// `visibility` stay lenient strings; downstream code uses explicit `===`
// comparisons and default branches per the API-compat rules.
export const IssueViewSchema = z.object({
  id: z.string(),
  workspace_id: z.string().default(""),
  owner_id: z.string().default(""),
  name: z.string().default(""),
  scope_type: z.string().default("workspace"),
  scope_id: z.string().nullish(),
  scope_variant: z.string().nullish(),
  visibility: z.string().default("private"),
  definition_version: z.number().default(1),
  query: z.record(z.string(), z.unknown()).default({}),
  display: z.record(z.string(), z.unknown()).default({}),
  revision: z.number().default(1),
  created_at: z.string().default(""),
  updated_at: z.string().default(""),
}).loose();

export type IssueView = z.infer<typeof IssueViewSchema>;

export const IssueViewListSchema = z.array(IssueViewSchema);

export const IssueViewPreferenceSchema = z.object({
  scope_type: z.string().default("workspace"),
  scope_id: z.string().nullish(),
  prefs: z.object({
    hidden: z.array(z.string()).default([]),
    order: z.array(z.string()).default([]),
  }).loose().default({ hidden: [], order: [] }),
  updated_at: z.string().default(""),
}).loose();

export type IssueViewPreference = z.infer<typeof IssueViewPreferenceSchema>;

export const EMPTY_ISSUE_VIEW_PREFERENCE: IssueViewPreference = {
  scope_type: "workspace",
  scope_id: null,
  prefs: { hidden: [], order: [] },
  updated_at: "",
};

/**
 * The `sidebar_projects` preference document. Another client version may have
 * saved values this one does not know; every field that does not parse comes
 * out undefined so the caller's default stands in for it.
 */
export const SidebarProjectPrefsDocSchema = z.object({
  projects: z.object({
    ordering: z.enum(["manual", "name", "updated", "created"]).optional().catch(undefined),
    statuses: z.array(z.string()).optional().catch(undefined),
    onlyMine: z.boolean().optional().catch(undefined),
    showCount: z.boolean().optional().catch(undefined),
    showNoProject: z.boolean().optional().catch(undefined),
    minimalIcons: z.boolean().optional().catch(undefined),
  }).catch({}),
  issues: z.object({
    sortBy: z.enum(["updated_at", "created_at", "priority", "status", "title", "due_date"]).optional().catch(undefined),
    sortDirection: z.enum(["asc", "desc"]).optional().catch(undefined),
    hiddenStatuses: z.array(z.string()).nullable().optional().catch(undefined),
    assignee: z.enum(["anyone", "me", "my_agents", "me_and_my_agents"]).optional().catch(undefined),
    showSubIssues: z.boolean().optional().catch(undefined),
  }).catch({}),
  order: z.array(z.unknown()).catch([]).transform((ids) => ids.filter((id): id is string => typeof id === "string")),
});

export type SidebarProjectPrefsDoc = z.infer<typeof SidebarProjectPrefsDocSchema>;

export interface CreateIssueViewRequest {
  name: string;
  scope_type: "workspace" | "my" | "project";
  scope_id?: string | null;
  scope_variant?: "assigned" | "created" | "involved" | "any" | "members" | "agents" | null;
  visibility: "private" | "workspace";
  definition_version: number;
  query: Record<string, unknown>;
  display: Record<string, unknown>;
}

// Custom property definitions. `type` stays a lenient string so newer server
// types don't break installed clients; UI narrows with isKnownPropertyType.
export const IssuePropertySchema = z.object({
  id: z.string(),
  workspace_id: z.string(),
  name: z.string(),
  type: z.string(),
  description: z.string().optional().default(""),
  icon: z.string().optional().default(""),
  config: z.object({
    options: z.array(z.object({
      id: z.string(),
      name: z.string(),
      color: z.string().optional().default("#6b7280"),
    }).loose()).optional(),
  }).loose().default({}),
  position: z.number().optional().default(0),
  archived: z.boolean().optional().default(false),
  archived_at: z.string().nullable().optional(),
  usage_count: z.number().optional().default(0),
  created_at: z.string(),
  updated_at: z.string(),
}).loose();

export const EMPTY_ISSUE_PROPERTY: IssueProperty = {
  id: "",
  workspace_id: "",
  name: "",
  type: "text",
  description: "",
  icon: "",
  config: {},
  position: 0,
  archived: false,
  usage_count: 0,
  created_at: "",
  updated_at: "",
};

// Quick actions (MUL-5465). `visibility` and `status` stay z.string() rather
// than z.enum: they are server-driven, and a newer server adding a value must
// degrade to the UI's default branch, not blank the whole list.
export const QuickActionSchema = z.object({
  id: z.string(),
  workspace_id: z.string(),
  name: z.string(),
  description: z.string().optional().default(""),
  assignee_type: z.string(),
  assignee_id: z.string(),
  prompt: z.string().optional().default(""),
  visibility: z.string().optional().default("public"),
  status: z.string().optional().default("active"),
  last_used_at: z.string().nullable().optional().default(null),
  use_count: z.number().optional().default(0),
  created_by_id: z.string().optional().default(""),
  created_at: z.string(),
  updated_at: z.string(),
  target_name: z.string().optional(),
  // Both default to the pessimistic reading on an older server: "not known to
  // be public" and "not known to be missing" keep the settings row honest
  // rather than asserting a state the server never sent.
  target_public: z.boolean().optional().default(false),
  target_missing: z.boolean().optional().default(false),
}).loose();

export const EMPTY_QUICK_ACTION: QuickAction = {
  id: "",
  workspace_id: "",
  name: "",
  description: "",
  assignee_type: "agent",
  assignee_id: "",
  prompt: "",
  visibility: "public",
  status: "active",
  last_used_at: null,
  use_count: 0,
  created_by_id: "",
  created_at: "",
  updated_at: "",
  target_public: false,
  target_missing: true,
};

export const ListQuickActionsResponseSchema = z.object({
  quick_actions: z.array(QuickActionSchema).default([]),
}).loose();

export const EMPTY_LIST_QUICK_ACTIONS_RESPONSE: ListQuickActionsResponse = {
  quick_actions: [],
};

export const ListPropertiesResponseSchema = z.object({
  properties: z.array(IssuePropertySchema).default([]),
  total: z.number().default(0),
}).loose();

export const EMPTY_LIST_PROPERTIES_RESPONSE: ListPropertiesResponse = {
  properties: [],
  total: 0,
};

// Value bag: keyed by definition UUID; values are primitives or string
// arrays (multi_select). The preprocess step drops entries with unknown
// shapes BEFORE validation — a newer server shipping an object-shaped value
// (future actor/relation types) must degrade to "that one property missing",
// never fail the whole IssueSchema and blank the list via parseWithFallback.
export const IssuePropertyValuesSchema = z.preprocess(
  (raw) => {
    if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return {};
    const out: Record<string, unknown> = {};
    for (const [key, value] of Object.entries(raw)) {
      const ok =
        typeof value === "string" ||
        typeof value === "number" ||
        typeof value === "boolean" ||
        (Array.isArray(value) && value.every((item) => typeof item === "string"));
      if (ok) out[key] = value;
    }
    return out;
  },
  z.record(z.string(), z.union([z.string(), z.number(), z.boolean(), z.array(z.string())])).default({}),
);

export const IssuePropertiesResponseSchema = z.object({
  properties: IssuePropertyValuesSchema,
  issue_revision: z.number().int().positive().optional(),
}).loose();

export const EMPTY_ISSUE_PROPERTIES_RESPONSE: IssuePropertiesResponse = {
  properties: {},
};

export interface AppConfigResponse {
  cdn_domain: string;
  attachment_public_url?: string;
  // True when the CDN domain serves private content via time-bounded signed
  // URLs (CloudFront signing) — raw storage URLs on that domain are NOT
  // publicly fetchable and must not be used as native media sources
  // (MUL-3254). Older servers omit the field; treat that as false.
  cdn_signed?: boolean;
  allow_signup: boolean;
  google_client_id?: string;
  posthog_key?: string;
  posthog_host?: string;
  analytics_environment?: string;
  daemon_server_url?: string;
  daemon_app_url?: string;
  daemon_setup_target?: "cloud" | "self_host" | "unknown";
  workspace_creation_disabled?: boolean;
  /** Whether this deployment offers the self-hosted Git provider integration
   * (self-host only; off on the managed cloud). Absent/false hides the whole
   * Settings → Integrations "Git providers" section. */
  vcs_integration_available?: boolean;
  feature_flags?: Record<string, boolean>;
  /** Whether this server understands local_directory `execution_mode` and
   * gates worktree mode at save time. Absent on every server that predates this
   * capability signal, which includes the ones that silently DROPPED an unknown
   * `execution_mode` and answered 201 — the resource then ran in place while
   * the user was promised isolation (#7113). Servers between that fix and this
   * signal do validate but cannot say so, and are treated as unable: the client
   * has no way to tell them apart, and only one of the two answers is safe. */
  local_worktree_supported?: boolean;
  /** Whether agent create/update persists `conversation_starters`. Older servers
   * silently ignored the unknown field, so absent must be treated as false. */
  agent_conversation_starters_supported?: boolean;
  webhook_payload_conditions_supported?: boolean;
  /** Whether issue create atomically validates and persists `properties`.
   * Older servers silently ignore the field, so absent means unsupported. */
  issue_create_properties_supported?: boolean;
  /** Whether deleting a comment keeps its replies and the server routes
   * DELETE /api/comments/{id}/keep-replies. Older servers deleted the replies
   * too, so absent must be treated as false (#8296). */
  comment_delete_keep_replies_supported?: boolean;
  /** Whether this server serves the community theme store under
   * /api/theme-store. Off when the operator turned it off, and absent on older
   * servers, so the app shows no Browse entry and makes no store requests. */
  theme_store_enabled?: boolean;
  server_version?: string;
}

// Schemas for the highest-risk API endpoints — those whose responses drive
// the issue detail page (timeline, comments, subscribers) and the issues
// list. These are the surfaces that white-screened in #2143 / #2147 / #2192.
//
// These schemas are intentionally LENIENT:
//   - String enums are stored as `z.string()` rather than `z.enum([...])`.
//     A new server-side enum value should render as a generic fallback in
//     the UI, never crash a `safeParse`.
//   - Optional fields are unioned with `null` and given fallbacks where
//     existing UI code already coerces them.
//   - Arrays default to `[]` so a missing `reactions` / `attachments` /
//     `entries` field doesn't take the page down.
//   - Every object schema ends with `.loose()` so unknown server-side
//     fields pass through unchanged. zod 4's `.object()` defaults to STRIP,
//     which would silently delete fields the schema didn't explicitly list
//     — fine while the TS type doesn't claim them, but the moment a future
//     PR adds a TS field without updating the schema, the cast `as T` lies
//     and the field shows up as `undefined` at runtime. `.loose()` removes
//     that synchronisation hazard.
//
// These schemas are deliberately not typed as `z.ZodType<TimelineEntry>` /
// `z.ZodType<Issue>` etc. — the strict TS types narrow string fields to
// literal unions, which would defeat the leniency above. `parseWithFallback`
// returns the parsed value cast to the caller-supplied `T`, so the strict
// type still flows out at the call site; the schema only guards shape.

const ReactionSchema = z.object({
  id: z.string(),
  comment_id: z.string(),
  actor_type: z.string(),
  actor_id: z.string(),
  emoji: z.string(),
  created_at: z.string(),
});

// Nested attachments embedded in timeline/comment responses stay lenient on
// purpose: a single malformed attachment must not knock the whole timeline
// into the fallback `[]`.
const AttachmentImageFields = {
  width: z.number().int().positive().optional().catch(undefined),
  height: z.number().int().positive().optional().catch(undefined),
  thumbnail_url: z.string().min(1).optional().catch(undefined),
};

const AttachmentSchema = z.object({
  id: z.string(),
  ...AttachmentImageFields,
}).loose();

const ChatQuickActionSchema = z.object({
  label: z.string(),
  prompt: z.string(),
  primary: z.boolean().optional(),
}).loose();

export const ChatMessageSchema = z.object({
  id: z.string(),
  chat_session_id: z.string(),
  role: z.enum(["user", "assistant"]).catch("assistant"),
  content: z.string().default(""),
  task_id: z.string().nullable().default(null),
  created_at: z.string().default(""),
  attachments: z.array(AttachmentSchema).optional(),
  failure_reason: z.string().nullable().optional(),
  elapsed_ms: z.number().nullable().optional(),
  message_kind: z
    .enum(["message", "no_response", "onboarding_kickoff", "onboarding_opening", "wakeup"])
    .catch("message")
    .optional(),
  // Optional additive data degrades independently: a malformed suggestion
  // must not hide the assistant reply that contains it.
  quick_actions: z.array(ChatQuickActionSchema).catch([]).optional().default([]),
  wakeup_id: z.string().nullish().catch(undefined),
}).loose();

export const ChatMessageListSchema = z.array(ChatMessageSchema).default([]);
export const EMPTY_CHAT_MESSAGE_LIST: ChatMessage[] = [];

export const ChatMessagesPageSchema = z.object({
  messages: z.array(ChatMessageSchema).default([]),
  limit: z.number().default(50),
  has_more: z.boolean().default(false),
  next_cursor: z.object({
    created_at: z.string(),
    id: z.string(),
  }).loose().nullable().optional(),
}).loose();

// Standalone attachment lookup (`GET /api/attachments/{id}`) is the source of
// truth for click-time download URLs. The two fields the download flow opens
// in a new tab — `download_url` and `url` — must be strings, otherwise we'd
// happily `window.open(undefined)`. `filename` gates the toast/title and is
// also enforced so a missing value falls back to the empty record below.
//
// `markdown_url` is parsed lenient: a server old enough to predate
// MUL-3192 omits the field, in which case the schema defaults it to "".
// Callers that need to persist a URL into markdown should go through the
// `useFileUpload` helper (which falls back to the legacy
// `attachmentDownloadPath` shape when `markdown_url` is empty), so the
// empty-string default does not silently break any persistence path.
export const AttachmentResponseSchema = z.object({
  id: z.string(),
  url: z.string(),
  download_url: z.string(),
  // Forced-attachment ("download button") URL — credential-free and, unlike
  // `download_url`, always Content-Disposition: attachment across every storage
  // mode. Optional: a server older than this field omits it, and callers fall
  // back to `download_url` / the stable endpoint. Never persisted (short-lived).
  attachment_download_url: z.string().optional(),
  markdown_url: z.string().optional().default(""),
  filename: z.string(),
  chat_session_id: z.string().nullable().optional(),
  chat_message_id: z.string().nullable().optional(),
  // Image metadata is optional and degrades on its own: a malformed value
  // means "unknown dimensions", never a lost attachment.
  ...AttachmentImageFields,
}).loose();

// The issue file inventory uses comment origins for grouping and sorting.
// Malformed optional origins must not discard otherwise usable attachments.
export const AttachmentListSchema = z.array(AttachmentResponseSchema.extend({
  workspace_id: z.string(),
  issue_id: z.string().nullable().default(null),
  comment_id: z.string().nullable().default(null),
  chat_session_id: z.string().nullable().default(null),
  chat_message_id: z.string().nullable().default(null),
  uploader_type: z.string(),
  uploader_id: z.string(),
  content_type: z.string(),
  size_bytes: z.number(),
  created_at: z.string(),
  comment_author_type: z.string().optional().catch(undefined),
  comment_author_id: z.string().optional().catch(undefined),
  comment_created_at: z.iso.datetime({ offset: true }).optional().catch(undefined),
})).default([]);

export const EMPTY_ATTACHMENT: Attachment = {
  id: "",
  workspace_id: "",
  issue_id: null,
  comment_id: null,
  chat_session_id: null,
  chat_message_id: null,
  uploader_type: "",
  uploader_id: "",
  filename: "",
  url: "",
  download_url: "",
  markdown_url: "",
  content_type: "",
  size_bytes: 0,
  created_at: "",
};

// All object schemas use `.loose()` so unknown server-side fields pass
// through unchanged. zod 4's `.object()` defaults to STRIP, which would
// silently drop new fields and surface as a "field neither showed up in
// the UI" mystery the next time the TS type adopted them but the schema
// wasn't updated in lock-step. `.loose()` removes that synchronisation
// hazard — the schema validates the shape it knows about and leaves the
// rest alone.
// One receipt per running turn a comment steered. A malformed receipt is
// dropped on its own rather than failing the whole comment.
const CommentSupplementReceiptSchema = z.object({
  task_id: z.string(),
  agent_id: z.string().optional().catch(undefined),
  status: z.enum(["pending", "delivering", "delivered", "failed"]),
  failure_reason: z.string().optional().catch(undefined),
  delivered_at: z.string().optional().catch(undefined),
});

const CommentSupplementReceiptsSchema = z.array(z.unknown()).optional().catch(undefined)
  .transform((raw) => raw?.flatMap((item) => {
    const parsed = CommentSupplementReceiptSchema.safeParse(item);
    return parsed.success ? [parsed.data] : [];
  }));

const TimelineEntrySchema = z.object({
  type: z.string(),
  id: z.string(),
  actor_type: z.string(),
  actor_id: z.string(),
  created_at: z.string(),
  actor_name: z.string().optional(),
  actor_avatar_url: z.string().optional(),
  action: z.string().optional(),
  details: z.record(z.string(), z.unknown()).optional(),
  content: z.string().optional(),
  parent_id: z.string().nullable().optional(),
  updated_at: z.string().optional(),
  revision: z.number().int().positive().optional(),
  comment_type: z.string().optional(),
  reactions: z.array(ReactionSchema).optional(),
  attachments: z.array(AttachmentSchema).optional(),
  source_task_id: z.string().nullable().optional(),
  supplements: CommentSupplementReceiptsSchema,
  supplement_task_id: z.string().optional().catch(undefined),
  supplement_status: z.enum(["pending", "delivering", "delivered", "failed"]).optional().catch(undefined),
  supplement_failure_reason: z.string().optional().catch(undefined),
  supplement_delivered_at: z.string().optional().catch(undefined),
  // Tombstone marker (#8296). Lenient: a malformed value reads as a live
  // comment instead of failing the whole timeline.
  deleted_at: z.string().nullable().optional().catch(undefined),
  coalesced_count: z.number().optional(),
}).loose();

// /timeline returns a flat array of TimelineEntry, oldest first. The
// previously cursor-paginated wrapper was removed (#1929) — at observed data
// sizes (p99 ~30 entries per issue) paged delivery only created bugs.
export const TimelineEntriesSchema = z.array(TimelineEntrySchema);

export const EMPTY_TIMELINE_ENTRIES: TimelineEntry[] = [];

const OptionalStringSchema = z.preprocess(
  (value) => (typeof value === "string" ? value : undefined),
  z.string().optional(),
);

const BooleanWithDefaultSchema = (fallback: boolean) =>
  z.preprocess(
    (value) => (typeof value === "boolean" ? value : undefined),
    z.boolean().default(fallback),
  );

const FeatureFlagsSchema = z.preprocess(
  (value) =>
    value && typeof value === "object" && !Array.isArray(value)
      ? value
      : undefined,
  z.record(z.string(), BooleanWithDefaultSchema(false)).default({}),
);

/**
 * POST /api/auth/refresh — sliding session renewal (MUL-7436).
 *
 * `token` is present only for clients that carry the session as a string
 * (Desktop, mobile). A cookie-authenticated browser gets the renewed session
 * as a Set-Cookie and must never be handed a readable JWT, so the field is
 * absent there rather than empty.
 *
 * `renewed: false` is the normal answer to asking early, not an error —
 * clients poll on a cadence and most calls land outside the renewal window.
 * `check_again_in_seconds` is that cadence: the server derives it from the
 * deployment's configured TTL, so no client hardcodes one.
 */
export interface RefreshSessionResponse {
  token?: string;
  expires_at: string;
  renewed: boolean;
  check_again_in_seconds: number;
}

export const RefreshSessionResponseSchema = z.object({
  token: OptionalStringSchema,
  expires_at: OptionalStringSchema,
  renewed: BooleanWithDefaultSchema(false),
  check_again_in_seconds: z.number().int().nonnegative().default(0),
}).loose();

// Fail closed: an unreadable response means "nothing was renewed", so the
// client keeps the session it already has and retries later. A fallback that
// claimed renewal would drop a working session on the floor.
export const EMPTY_REFRESH_SESSION_RESPONSE: RefreshSessionResponse = {
  expires_at: "",
  renewed: false,
  check_again_in_seconds: 0,
};

export const AppConfigSchema = z.object({
  cdn_domain: z.string().default(""),
  attachment_public_url: OptionalStringSchema,
  cdn_signed: BooleanWithDefaultSchema(false),
  allow_signup: BooleanWithDefaultSchema(true),
  google_client_id: OptionalStringSchema,
  posthog_key: OptionalStringSchema,
  posthog_host: OptionalStringSchema,
  analytics_environment: OptionalStringSchema,
  daemon_server_url: OptionalStringSchema,
  daemon_app_url: OptionalStringSchema,
  daemon_setup_target: z.enum(["cloud", "self_host", "unknown"]).optional().catch("unknown"),
  workspace_creation_disabled: BooleanWithDefaultSchema(false).optional(),
  vcs_integration_available: BooleanWithDefaultSchema(false).optional(),
  feature_flags: FeatureFlagsSchema,
  local_worktree_supported: BooleanWithDefaultSchema(false),
  agent_conversation_starters_supported: BooleanWithDefaultSchema(false),
  webhook_payload_conditions_supported: BooleanWithDefaultSchema(false),
  issue_create_properties_supported: BooleanWithDefaultSchema(false),
  comment_delete_keep_replies_supported: BooleanWithDefaultSchema(false),
  theme_store_enabled: BooleanWithDefaultSchema(false),
  server_version: OptionalStringSchema,
}).loose();

export const EMPTY_APP_CONFIG: AppConfigResponse = {
  cdn_domain: "",
  attachment_public_url: "",
  cdn_signed: false,
  allow_signup: true,
  google_client_id: "",
  daemon_server_url: "",
  daemon_app_url: "",
  daemon_setup_target: "unknown",
  workspace_creation_disabled: false,
  vcs_integration_available: false,
  // Fail closed: an unreadable config must not look like a server that
  // validates execution_mode.
  local_worktree_supported: false,
  // Fail closed: old servers returned success while dropping the field.
  agent_conversation_starters_supported: false,
  webhook_payload_conditions_supported: false,
  // Fail closed: old servers returned success while dropping create properties.
  issue_create_properties_supported: false,
  // Fail closed: old servers delete a comment's replies with it.
  comment_delete_keep_replies_supported: false,
  theme_store_enabled: false,
  feature_flags: {},
};

export type StoreThemeMode = "light" | "dark";

/** A theme of the community registry, as /api/theme-store lists it. */
export interface StoreTheme {
  id: string;
  name: string;
  author: string;
  /** The author's GitHub repository, `owner/name`. */
  repo: string;
  description?: string;
  modes: StoreThemeMode[];
  /** The release the registry checked; installs always get this one. */
  version: string;
  size: number;
  /** Modes the registry rendered a screenshot for. */
  screenshots: StoreThemeMode[];
  /**
   * Each mode's current screenshot. The registry re-renders screenshots
   * without a new release; absent from servers that predate this.
   */
  screenshot_revisions?: Partial<Record<StoreThemeMode, string>>;
  updated_at?: string;
  /** Installs and updates served by this server, all users together. */
  downloads: number;
}

const StoreThemeModesSchema = z.preprocess(
  (value) => (Array.isArray(value) ? value.filter((m) => m === "light" || m === "dark") : []),
  z.array(z.enum(["light", "dark"])),
);

const StoreThemeSchema = z
  .object({
    id: z.string(),
    name: z.string(),
    author: z.string(),
    repo: z.string(),
    description: OptionalStringSchema,
    modes: StoreThemeModesSchema,
    version: z.string(),
    size: z.number(),
    screenshots: StoreThemeModesSchema,
    screenshot_revisions: z
      .object({ light: z.string().optional(), dark: z.string().optional() })
      .optional()
      .catch(undefined),
    updated_at: OptionalStringSchema,
    downloads: z.number().catch(0).default(0),
  })
  .loose();

/** One malformed entry drops that theme, not the whole list. */
export const StoreThemeListSchema = z.object({
  themes: z.preprocess(
    (value) => (Array.isArray(value) ? value.filter((item) => StoreThemeSchema.safeParse(item).success) : []),
    z.array(StoreThemeSchema),
  ),
});

export const EMPTY_STORE_THEME_LIST: { themes: StoreTheme[] } = { themes: [] };

// Preference keys may grow over time, so keep both the key and value spaces
// forward-compatible while still rejecting non-string persisted data.
export const NotificationPreferenceResponseSchema = z.object({
  workspace_id: z.string(),
  preferences: z.record(z.string(), z.string()).default({}),
  auto_archive_prompt: z.boolean().optional(),
}).loose();

export const EMPTY_NOTIFICATION_PREFERENCE_RESPONSE: NotificationPreferenceResponse = {
  workspace_id: "",
  preferences: {},
};

export const CreateFeedbackResponseSchema = z.object({
  id: z.string(),
  created_at: z.string(),
}).loose();

export const EMPTY_CREATE_FEEDBACK_RESPONSE: CreateFeedbackResponse = {
  id: "",
  created_at: "",
};

export const CommentSchema = z.object({
  id: z.string(),
  issue_id: z.string(),
  author_type: z.string(),
  author_id: z.string(),
  content: z.string(),
  type: z.string(),
  parent_id: z.string().nullable(),
  reactions: z.array(ReactionSchema).default([]),
  attachments: z.array(AttachmentSchema).default([]),
  created_at: z.string(),
  updated_at: z.string(),
  revision: z.number().int().positive().optional(),
  source_task_id: z.string().nullable().optional(),
  supplements: CommentSupplementReceiptsSchema,
  supplement_task_id: z.string().optional().catch(undefined),
  supplement_status: z.enum(["pending", "delivering", "delivered", "failed"]).optional().catch(undefined),
  supplement_failure_reason: z.string().optional().catch(undefined),
  supplement_delivered_at: z.string().optional().catch(undefined),
  // Set only on comments a quick action produced (MUL-5465). Server-only.
  quick_action_id: z.string().nullable().optional(),
  deleted_at: z.string().nullable().optional().catch(undefined),
}).loose();

export const CommentsListSchema = z.array(CommentSchema);

// Degraded placeholder for a comment response that failed schema validation.
// The empty id is the caller's signal that nothing usable came back — the run
// UI treats it as "could not read the result" rather than a successful run.
export const EMPTY_COMMENT: Comment = {
  id: "",
  issue_id: "",
  author_type: "member",
  author_id: "",
  content: "",
  type: "comment",
  parent_id: null,
  reactions: [],
  attachments: [],
  created_at: "",
  updated_at: "",
  resolved_at: null,
  resolved_by_type: null,
  resolved_by_id: null,
};

const CommentTriggerPreviewAgentSchema = z.object({
  id: z.string(),
  name: z.string().default(""),
  avatar_url: z.string().optional(),
  source: z.string().default(""),
  reason: z.string().default(""),
}).loose();

// Per-target outcome of an explicit @agent / @squad mention (MUL-4525 §2).
// target_id is required to correlate with the client's rendered mention; a
// malformed entry (missing id) is dropped rather than failing the whole payload.
export const CommentTriggerOutcomeSchema = z.object({
  target_type: z.string().default(""),
  target_id: z.string(),
  status: z.string().default(""),
  reason_code: z.string().default(""),
}).loose();

export const CommentTriggerPreviewSchema = z.object({
  agents: z.array(CommentTriggerPreviewAgentSchema).default([]),
  // Drop malformed blocked entries INDIVIDUALLY (MUL-4525): a single bad item
  // must not discard the whole set of valid blocked mentions. A non-array
  // degrades to []; each valid entry is kept, each malformed one dropped.
  blocked: z
    .array(z.unknown())
    .catch([])
    .default([])
    .transform((items) =>
      items.flatMap((item) => {
        const parsed = CommentTriggerOutcomeSchema.safeParse(item);
        return parsed.success ? [parsed.data] : [];
      }),
    ),
}).loose();

const IssueTriggerPreviewItemSchema = z.object({
  issue_id: z.string(),
  agent_id: z.string().default(""),
  source: z.string().default(""),
}).loose();

export const IssueTriggerPreviewSchema = z.object({
  triggers: z.array(IssueTriggerPreviewItemSchema).default([]),
  total_count: z.number().default(0),
}).loose();

// Metadata is primitive-only by API/DB contract. Stay lenient on shape:
// unknown keys land as `unknown` to a caller, but the field itself defaults
// to {} so consumers never need to nil-guard `issue.metadata`.
const IssueMetadataSchema = z.record(z.string(), z.union([z.string(), z.number(), z.boolean()])).default({});

const SourceContextAttachmentSchema = z.object({
  id: z.string(),
  source_attachment_id: z.string().optional(),
  owner_type: z.string(),
  owner_id: z.string(),
  filename: z.string(),
  content_type: z.string(),
  size_bytes: z.number(),
  created_at: z.string(),
}).loose();

// Early source-context servers encoded an empty Go slice as JSON null. Keep
// installed clients compatible while normalizing consumers onto the canonical
// array shape emitted by current servers.
const SourceContextAttachmentsSchema = z.array(SourceContextAttachmentSchema)
  .nullish()
  .transform((attachments) => attachments ?? []);

const SourceContextAuthorSchema = z.object({
  type: z.string(),
  id: z.string(),
  name: z.string(),
}).loose();

const SourceContextIssueSnapshotSchema = z.object({
  id: z.string(),
  identifier: z.string(),
  number: z.number(),
  title: z.string(),
  description: z.string().nullable(),
  created_at: z.string(),
  updated_at: z.string(),
  revision: z.number(),
  attachments: SourceContextAttachmentsSchema,
}).loose();

const SourceContextCommentSnapshotSchema = z.object({
  id: z.string(),
  parent_id: z.string().nullable(),
  type: z.string(),
  content: z.string(),
  author: SourceContextAuthorSchema,
  created_at: z.string(),
  updated_at: z.string(),
  revision: z.number(),
  attachments: SourceContextAttachmentsSchema,
  deleted: z.boolean().optional().catch(undefined),
}).loose();

export const SourceContextSnapshotSchema = z.object({
  version: z.number().optional(),
  captured_by_user_id: z.string().optional(),
  captured_at: z.string().optional(),
  source_issue: SourceContextIssueSnapshotSchema,
  comment_thread: z.array(SourceContextCommentSnapshotSchema),
  anchor_comment_id: z.string(),
}).loose();

export const SourceContextPreviewSchema = SourceContextSnapshotSchema.extend({
  capture_token: z.string().min(1),
  limits: z.object({
    comment_count: z.number(),
    text_bytes: z.number(),
    attachment_count: z.number(),
    attachment_bytes: z.number(),
  }).loose(),
}).loose();

const IssueSourceContextSchema = z.object({
  id: z.string(),
  version: z.number(),
  usage: z.string(),
  captured_at: z.string(),
  display_state: z.string(),
  source_issue_state: z.string(),
  comment_thread_state: z.string(),
  anchor_comment_state: z.string(),
  can_open_current_source: z.boolean(),
  change_reasons: z.array(z.string()).optional(),
  change_details: z.object({
    changed_comment_ids: z.array(z.string()),
    added_comments: z.array(SourceContextCommentSnapshotSchema).optional(),
    removed_comment_ids: z.array(z.string()).optional(),
    description_attachment_changes: z.array(z.object({
      kind: z.string(),
      attachment_id: z.string(),
      filename: z.string(),
      previous_filename: z.string().optional(),
    }).loose()),
  }).loose().optional(),
  current_source: z.object({
    issue_id: z.string(),
    identifier: z.string(),
    anchor_comment_id: z.string(),
  }).loose().optional(),
  source_author_state: z.array(z.object({
    type: z.string(),
    id: z.string(),
    captured_name: z.string(),
    current_name: z.string().optional(),
    state: z.string(),
  }).loose()).optional(),
  snapshot: SourceContextSnapshotSchema,
}).loose();

export const CommentSubIssueTaskResponseSchema = z.object({
  task_id: z.string().min(1),
}).loose();

const IssueBlockerSchema = z.object({
  id: z.string(), identifier: z.string(), title: z.string(), status: z.string(),
  via: z.string(), closed: z.boolean(),
});

export const IssueSchema = z.object({
  blocked_count: z.number().int().nonnegative().optional().catch(undefined),
  blocked_by: z.array(IssueBlockerSchema).optional().catch(undefined),
  // Detail reads only: the issues waiting on this one.
  blocking: z.array(IssueBlockerSchema).optional().catch(undefined),
  id: z.string(),
  workspace_id: z.string(),
  number: z.number(),
  identifier: z.string(),
  title: z.string(),
  description: z.string().nullable(),
  status: z.string(),
  // The canonical status whose platform behavior `status` carries — equal to
  // `status` for the 7 built-ins, and the inherited category for a custom
  // status. Optional because only endpoints that resolve it emit it, so
  // consumers must fall back to `status` rather than treat "" as a category.
  // (MUL-6243)
  status_category: z.string().transform((value) => normalizeIssueStatusCategory(value) ?? value).optional(),
  // A CUSTOM status's display name; "" for a built-in, which clients localize
  // from the key. Optional so a response from a server that predates the field
  // still validates.
  //
  // .catch(undefined) because this is ADDITIVE display data and the failure it
  // guards against is disproportionate: a parse failure anywhere in IssueSchema
  // takes the whole response through parseWithFallback to EMPTY_LIST_ISSUES_RESPONSE,
  // so one malformed name from a mixed-version deploy would blank an entire
  // issue list. Same treatment as source_context and labels above. Nothing reads
  // this field to make a decision — useStatusLabel resolves the label from the
  // catalog — so dropping it costs a fallback to the key. (MUL-6749)
  status_name: z.string().optional().catch(undefined),
  priority: z.string(),
  assignee_type: z.string().nullable(),
  assignee_id: z.string().nullable(),
  creator_type: z.string(),
  creator_id: z.string(),
  requester_id: z.string().nullable().optional().catch(undefined),
  parent_issue_id: z.string().nullable(),
  // Additive pointer (MUL-7349); same disproportionate-failure reasoning as
  // status_name above, and older backends do not send it at all.
  duplicate_of: z
    .object({ id: z.string(), identifier: z.string(), title: z.string(), status: z.string() })
    .nullable()
    .optional()
    .catch(undefined),
  project_id: z.string().nullable(),
  position: z.number(),
  // Older backends predate `stage`; default to null so a missing field parses
  // cleanly into the non-optional Issue.stage (number | null).
  stage: z.number().nullable().default(null),
  start_date: z.string().nullable(),
  due_date: z.string().nullable(),
  metadata: IssueMetadataSchema,
  // Older backends predate custom properties; default {} so consumers never
  // nil-guard issue.properties.
  properties: IssuePropertyValuesSchema,
  reactions: z.array(z.unknown()).optional(),
  labels: z.array(z.unknown()).optional(),
  created_at: z.string(),
  updated_at: z.string(),
  revision: z.number().int().positive().optional(),
  description_revision: z.number().int().nonnegative().optional(),
  // Optional for compatibility with older self-hosted backends; a current
  // backend emits null until its historical backfill reaches the issue.
  last_activity_at: z.string().nullable().optional(),
  // Detail-only and potentially large. A malformed additive field must not
  // erase an otherwise usable issue returned by a mixed-version server.
  source_context: IssueSourceContextSchema.optional().catch(undefined),
}).loose();

export const ListIssuesResponseSchema = z.object({
  issues: z.array(IssueSchema).default([]),
  total: z.number().default(0),
}).loose();

// Response schema for POST /api/issues. Two tightenings over IssueSchema:
//
//   - `id` must be non-empty. A created issue always carries a real id, so an
//     empty/absent id means the create effectively failed. createIssue turns a
//     schema failure into a rejection (not a fabricated success), so tightening
//     id here routes an id-less body to that same failure path.
//   - `labels` is the backend-compatibility signal the create modal reads to
//     decide whether the backend attached labels in the create transaction
//     (present) or predates that (absent → fall back to per-label attach).
//     Validate it strictly as Label[] and degrade a malformed value to
//     `undefined` — the same as an absent field — so a wrong shape (null,
//     object, a garbage array) can never masquerade as "handled" and suppress
//     the fallback. Unlike the loose IssueSchema.labels (z.array(z.unknown())),
//     the elements are fully validated. See packages/views/modals/create-issue.tsx.
export const CreateIssueResponseSchema = IssueSchema.extend({
  id: z.string().min(1),
  labels: z.array(LabelSchema).optional().catch(undefined),
  pr_auto_complete_disabled: z.boolean().optional().catch(undefined),
}).loose();

export const EMPTY_LIST_ISSUES_RESPONSE: ListIssuesResponse = {
  issues: [],
  total: 0,
};

const SearchIssueResultSchema = IssueSchema.extend({
  match_source: z.string(),
  matched_snippet: z.string().optional(),
  matched_description_snippet: z.string().optional(),
  matched_comment_snippet: z.string().optional(),
}).loose();

export const SearchIssuesResponseSchema = z.object({
  issues: z.array(SearchIssueResultSchema).default([]),
}).loose();

export const EMPTY_SEARCH_ISSUES_RESPONSE: SearchIssuesResponse = {
  issues: [],
};

const ProjectProgressSchema = z.object({
  unit: z.literal("issues").catch("issues"),
  scope: z.number().default(0),
  completed: z.number().default(0),
  started: z.number().default(0),
  unstarted: z.number().default(0),
  cancelled: z.number().default(0),
  percent: z.number().default(0),
}).loose();

export const ProjectSchema = z.object({
  id: z.string(),
  workspace_id: z.string(),
  title: z.string(),
  visibility: z.enum(["workspace", "private"]).default("workspace").catch("private"),
  description: z.string().nullable(),
  icon: z.string().nullable(),
  status: z.string(),
  priority: z.string(),
  lead_type: z.string().nullable(),
  lead_id: z.string().nullable(),
  creator_type: z.string().nullable().default(null).catch(null),
  creator_id: z.string().nullable().default(null).catch(null),
  // .default(null) so a project from an older backend (frontend deploys before
  // backend) that omits these keys parses to null instead of failing the whole
  // object — which would degrade a search/list batch to the empty fallback.
  start_date: z.string().nullable().default(null),
  due_date: z.string().nullable().default(null),
  created_at: z.string(),
  updated_at: z.string(),
  last_activity_at: z.string().optional().catch(undefined),
  issue_count: z.number().default(0),
  done_count: z.number().default(0),
  // Older servers omit it; consumers fall back to issue_count / done_count.
  progress: ProjectProgressSchema.optional().catch(undefined),
  resource_count: z.number().default(0),
  workflow_id: z.string().nullable().default(null),
  archived_at: z.string().nullable().default(null).catch(null),
  archived_by_type: z.enum(["member", "agent"]).nullable().default(null).catch(null),
  archived_by_id: z.string().nullable().default(null).catch(null),
}).loose();

export const ArchiveProjectResponseSchema = ProjectSchema.extend({
  paused_autopilot_ids: z.array(z.string()).default([]).catch([]),
});

export const EMPTY_PROJECT: Project = {
  id: "", workspace_id: "", title: "", description: null, icon: null,
  status: "planned", priority: "none", lead_type: null, lead_id: null,
  creator_type: null, creator_id: null, start_date: null, due_date: null,
  created_at: "", updated_at: "", issue_count: 0, done_count: 0, resource_count: 0,
};

export const ListProjectsResponseSchema = z.object({
  projects: z.array(ProjectSchema).default([]),
  total: z.number().default(0),
}).loose();

export const ProjectProgressDetailSchema = z.object({
  progress: ProjectProgressSchema,
  by_assignee: z.array(z.object({
    assignee_type: z.enum(["member", "agent", "squad"]).nullable().catch(null),
    assignee_id: z.string().nullable().default(null),
    scope: z.number().default(0),
    completed: z.number().default(0),
    started: z.number().default(0),
  }).loose()).default([]),
  // Older servers omit it; the label breakdown then has nothing to show.
  by_label: z.array(z.object({
    label_id: z.string().nullable().default(null),
    scope: z.number().default(0),
    completed: z.number().default(0),
    started: z.number().default(0),
  }).loose()).default([]).catch([]),
  history: z.array(z.object({
    date: z.string(),
    scope: z.number().default(0),
    started: z.number().default(0),
    completed: z.number().default(0),
  }).loose()).default([]),
  forecast: z.object({
    completion_date: z.string(),
    weekly_velocity: z.number(),
  }).loose().nullable().default(null).catch(null),
}).loose();

export const EMPTY_PROJECT_PROGRESS_DETAIL: ProjectProgressDetail = {
  progress: { unit: "issues", scope: 0, completed: 0, started: 0, unstarted: 0, cancelled: 0, percent: 0 },
  by_assignee: [],
  by_label: [],
  history: [],
  forecast: null,
};

export const ProjectActivityPageSchema = z.object({
  entries: z.array(z.object({
    id: z.string(),
    issue_id: z.string(),
    issue_identifier: z.string().default(""),
    issue_title: z.string().default(""),
    issue_status: z.string().default(""),
    actor_type: z.string().default(""),
    actor_id: z.string().default(""),
    action: z.string(),
    details: z.record(z.string(), z.unknown()).default({}).catch({}),
    created_at: z.string(),
  }).loose()).default([]),
  next_cursor: z.string().nullable().default(null),
}).loose();

export const EMPTY_PROJECT_ACTIVITY_PAGE: ProjectActivityPage = { entries: [], next_cursor: null };

export const EMPTY_LIST_PROJECTS_RESPONSE: ListProjectsResponse = { projects: [], total: 0 };

export const ProjectMemberSchema = z.object({
  project_id: z.string(),
  user_id: z.string(),
  name: z.string(),
  email: z.string(),
  avatar_url: z.string().nullable().default(null),
}).loose();

export const ListProjectMembersResponseSchema = z.object({
  members: z.array(ProjectMemberSchema),
  total: z.number(),
  // Absent from a server that predates it; see canManageProjectAccess.
  manager_id: z.string().nullable().default(null).catch(null),
}).loose();

// The workspace owner's overview of private projects (`/api/projects/private`).
// A row whose id is missing is dropped rather than failing the whole list: the
// overview is how an owner gets back into a project nobody else can open.
const PrivateProjectOverviewSchema = z.object({
  id: z.string(),
  title: z.string().catch(""),
  icon: z.string().nullable().default(null).catch(null),
  lead_type: z.enum(["member", "agent"]).nullable().default(null).catch(null),
  lead_id: z.string().nullable().default(null).catch(null),
  created_at: z.string().catch(""),
  archived_at: z.string().nullable().default(null).catch(null),
  is_member: z.boolean().catch(false),
  members: z.array(ProjectMemberSchema.omit({ project_id: true })).default([]).catch([]),
}).loose();

export const ListPrivateProjectsResponseSchema = z.object({
  projects: z.array(z.unknown()).default([]).catch([]).transform((rows) =>
    rows.flatMap((row) => {
      const parsed = PrivateProjectOverviewSchema.safeParse(row);
      return parsed.success ? [parsed.data] : [];
    }),
  ),
  total: z.number().default(0).catch(0),
}).loose();

// The viewer's own project subscription (`/api/projects/:id/subscription`).
// Anything but an explicit `true` reads as not subscribed.
export const ProjectSubscriptionSchema = z.object({
  subscribed: z.boolean().catch(false),
}).loose();

const SearchProjectResultSchema = ProjectSchema.extend({
  match_source: z.string(),
  matched_snippet: z.string().optional(),
}).loose();

export const SearchProjectsResponseSchema = z.object({
  projects: z.array(SearchProjectResultSchema).default([]),
}).loose();

export const EMPTY_SEARCH_PROJECTS_RESPONSE: SearchProjectsResponse = {
  projects: [],
};

// Local search index sync (MUL-7754). Callers reject a response that fails
// these schemas instead of degrading to an empty value: an empty snapshot page
// or change set would be applied to the local copy as truth.
const SearchIndexIssueSchema = IssueSchema.extend({
  search_updated_at: z.string(),
}).loose();

const SearchIndexProjectSchema = ProjectSchema.extend({
  search_updated_at: z.string(),
}).loose();

const SearchIndexCommentSchema = z.object({
  id: z.string().min(1),
  issue_id: z.string().min(1),
  content: z.string(),
  created_at: z.string(),
}).loose();

export const SearchIndexManifestSchema = z.object({
  cursor: z.string().min(1),
  issue_count: z.number(),
  comment_count: z.number(),
  project_count: z.number(),
  text_bytes: z.number(),
}).loose();

export const SearchIndexSnapshotPageSchema = z.object({
  issues: z.array(SearchIndexIssueSchema),
  comments: z.array(SearchIndexCommentSchema),
  projects: z.array(SearchIndexProjectSchema),
  next_after_number: z.number(),
  done: z.boolean(),
}).loose();

export const SearchIndexChangesSchema = z.object({
  issues: z.array(SearchIndexIssueSchema),
  comments: z.array(SearchIndexCommentSchema),
  projects: z.array(SearchIndexProjectSchema),
  deleted: z.object({
    issues: z.array(z.string()),
    comments: z.array(z.string()),
    projects: z.array(z.string()),
  }).loose(),
  cursor: z.string().min(1),
  has_more: z.boolean(),
}).loose();

const IssueTableActorRefSchema = z.object({
  // Server-driven enums stay open so installed desktop clients survive a
  // backend that introduces another actor kind.
  type: z.string(),
  id: z.string(),
}).loose();

const IssueTableParentRefSchema = z.object({
  id: z.string(),
  number: z.number(),
  identifier: z.string(),
  title: z.string(),
  status: z.string(),
}).loose();

const IssueTableGroupValueSchema = z.discriminatedUnion("kind", [
  z.object({
    kind: z.literal("status"),
    // Preserve the requested bucket vocabulary (legacy wire, lifecycle, or
    // concrete status). Normalizing here would disconnect values from keys.
    status: z.string(),
  }).loose(),
  z.object({
    kind: z.literal("assignee"),
    actor: IssueTableActorRefSchema.nullable(),
  }).loose(),
  z.object({
    kind: z.literal("project"),
    project_id: z.string().nullable().optional().default(null),
  }).loose(),
  z.object({
    kind: z.literal("parent"),
    parent_id: z.string().nullable().optional().default(null),
    parent: IssueTableParentRefSchema.nullable().optional().default(null),
    value_state: z.enum(["value", "unavailable", "unset"]),
  }).loose(),
  z.object({
    kind: z.literal("property"),
    property_id: z.string(),
    value: z.union([z.string(), z.boolean(), z.null()]).optional(),
    value_state: z.enum(["value", "unavailable", "unset"]),
  }).loose(),
]);

const IssueTableGroupDescriptorSchema: z.ZodType<IssueTableGroupDescriptor> = z.lazy(() => z.object({
  key: z.string(),
  value: IssueTableGroupValueSchema,
  count: z.number(),
  secondary_groups: z.array(IssueTableGroupDescriptorSchema).optional(),
}).loose());

export const IssueTableGroupsResponseSchema = z.object({
  query_fingerprint: z.string(),
  total: z.number(),
  groups: z.array(IssueTableGroupDescriptorSchema).default([]),
  next_cursor: z.string().nullable().default(null),
}).loose();

export const EMPTY_ISSUE_TABLE_GROUPS_RESPONSE: IssueTableGroupsResponse = {
  query_fingerprint: "",
  total: 0,
  groups: [],
  next_cursor: null,
};

const IssueTableRowSchema = z.object({
  issue: IssueSchema,
  direct_child_count: z.number().default(0),
}).loose();

export const IssueTableRowsResponseSchema = z.object({
  query_fingerprint: z.string(),
  group_key: z.string().nullable().default(null),
  parent_id: z.string().nullable().default(null),
  total: z.number(),
  rows: z.array(IssueTableRowSchema).default([]),
  branch_total: z.number(),
  next_cursor: z.string().nullable().default(null),
}).loose();

export const EMPTY_ISSUE_TABLE_ROWS_RESPONSE: IssueTableRowsResponse = {
  query_fingerprint: "",
  group_key: null,
  parent_id: null,
  total: 0,
  rows: [],
  branch_total: 0,
  next_cursor: null,
};

const IssueTableFacetValueSchema = z.object({
  key: z.string(),
  count: z.number(),
}).loose();

const IssueTableFacetSchema = z.object({
  kind: z.enum(["status", "priority", "assignee", "creator", "project", "label", "property", "working_agents"]),
  property_id: z.string().optional(),
  values: z.array(IssueTableFacetValueSchema).default([]),
}).loose();

export const IssueTableFacetsResponseSchema = z.object({
  query_fingerprint: z.string(),
  total: z.number(),
  facets: z.array(IssueTableFacetSchema).default([]),
}).loose();

export const EMPTY_ISSUE_TABLE_FACETS_RESPONSE: IssueTableFacetsResponse = {
  query_fingerprint: "",
  total: 0,
  facets: [],
};

const SubscriberSchema = z.object({
  issue_id: z.string(),
  user_type: z.string(),
  user_id: z.string(),
  reason: z.string(),
  created_at: z.string(),
  // An unknown or malformed tier shows no badge; it must not drop the row,
  // which would read as "not subscribed".
  delivery: z.enum(["all", "key_updates", "delegated"]).optional().catch(undefined),
}).loose();

export const SubscribersListSchema = z.array(SubscriberSchema);

export const ChildIssuesResponseSchema = z.object({
  issues: z.array(IssueSchema).default([]),
}).loose();

export const IssueDuplicatesResponseSchema = z.object({
  duplicate_of: IssueSchema.nullable().default(null),
  duplicates: z.array(IssueSchema).default([]),
}).loose();

export const ChildIssueProgressResponseSchema = z.object({
  progress: z
    .array(
      z
        .object({
          parent_issue_id: z.string(),
          total: z.number(),
          done: z.number(),
        })
        .loose(),
    )
    .default([]),
}).loose();

export const CloudRuntimeNodeSchema = z.object({
  id: z.string(),
  owner_id: z.string(),
  instance_id: z.string(),
  region: z.string(),
  instance_type: z.string(),
  image_id: z.string(),
  subnet_id: z.string(),
  name: z.string(),
  status: z.string(),
  tags: z.record(z.string(), z.string()).default({}),
  metadata: z.record(z.string(), z.unknown()).default({}),
  created_at: z.string(),
  updated_at: z.string(),
}).loose();

export const CloudRuntimeNodeListSchema = z.array(CloudRuntimeNodeSchema);

export const EMPTY_CLOUD_RUNTIME_NODE_LIST: CloudRuntimeNode[] = [];

export const EMPTY_CLOUD_RUNTIME_NODE: CloudRuntimeNode = {
  id: "",
  owner_id: "",
  instance_id: "",
  region: "",
  instance_type: "",
  image_id: "",
  subnet_id: "",
  name: "",
  status: "",
  tags: {},
  metadata: {},
  created_at: "",
  updated_at: "",
};

// Workspace dashboard schemas
//
// The dashboard hits three independent rollup endpoints. Each returns a flat
// array, and every field is consumed by chart / KPI math — a missing number
// silently degrades to NaN downstream, so we coerce missing numbers to 0.
// String fields default to "" (no enum narrowing) to survive future model /
// agent ID drift, and so a single null from tz-aware SQL bucketing fails
// only that row instead of dropping the whole array to the `[]` fallback.

// Cost split carried by every usage row. `cost_usd_ticks` is what the provider
// itself charged for the rows behind this aggregate (1e-10 USD); the
// `uncosted_*` counts are the tokens from rows the provider did NOT price, and
// so are the only ones the client should run through its rate table.
//
// The `uncosted_*` fields are deliberately `.optional()` rather than
// `.default(0)`: a backend that predates them sends nothing, and defaulting
// those rows to "0 tokens left to estimate" would silently zero their cost.
// `undefined` means "this backend doesn't split", and the consumer falls back
// to the full token counts — i.e. exactly the old behaviour. A real 0 from a
// current backend means "everything here is already priced", which is a
// different thing and must stay distinguishable.
const CostSplitShape = {
  cost_usd_ticks: z.number().optional(),
  uncosted_input_tokens: z.number().optional(),
  uncosted_output_tokens: z.number().optional(),
  uncosted_cache_read_tokens: z.number().optional(),
  uncosted_cache_write_tokens: z.number().optional(),
};

const DashboardUsageDailySchema = z.object({
  date: z.string().default(""),
  provider: z.string().default(""),
  model: z.string().default(""),
  input_tokens: z.number().default(0),
  output_tokens: z.number().default(0),
  cache_read_tokens: z.number().default(0),
  cache_write_tokens: z.number().default(0),
  ...CostSplitShape,
  task_count: z.number().default(0),
}).loose();

export const DashboardUsageDailyListSchema = z.array(DashboardUsageDailySchema);

const DashboardUsageByAgentSchema = z.object({
  agent_id: z.string().default(""),
  provider: z.string().default(""),
  model: z.string().default(""),
  input_tokens: z.number().default(0),
  output_tokens: z.number().default(0),
  cache_read_tokens: z.number().default(0),
  cache_write_tokens: z.number().default(0),
  ...CostSplitShape,
  task_count: z.number().default(0),
}).loose();

export const DashboardUsageByAgentListSchema = z.array(DashboardUsageByAgentSchema);

const DashboardUsageBySquadSchema = DashboardUsageByAgentSchema.extend({
  squad_id: z.string().default(""),
}).loose();

export const DashboardUsageBySquadListSchema = z.array(DashboardUsageBySquadSchema);

// `cancelled_count` defaults to 0 so an installed client pointed at a
// backend that predates it still renders: those rows simply carry no
// cancelled segment, which is exactly what that backend measured.
const DashboardAgentRunTimeSchema = z.object({
  agent_id: z.string().default(""),
  total_seconds: z.number().default(0),
  task_count: z.number().default(0),
  metered_task_count: z.number().optional().catch(undefined),
  failed_count: z.number().default(0),
  cancelled_count: z.number().default(0),
}).loose();

export const DashboardAgentRunTimeListSchema = z.array(DashboardAgentRunTimeSchema);

const DashboardSquadRunTimeSchema = DashboardAgentRunTimeSchema.extend({
  squad_id: z.string().default(""),
}).loose();

export const DashboardSquadRunTimeListSchema = z.array(DashboardSquadRunTimeSchema);

const DashboardRunTimeDailySchema = z.object({
  date: z.string().default(""),
  total_seconds: z.number().default(0),
  task_count: z.number().default(0),
  failed_count: z.number().default(0),
  cancelled_count: z.number().default(0),
}).loose();

export const DashboardRunTimeDailyListSchema = z.array(DashboardRunTimeDailySchema);

// Failure rollups. `failure_reason` is an open string on purpose — it carries
// the backend's canonical taxonomy, which grows as new classifier rules land
// (server/pkg/taskfailure). Pinning it to a z.enum would make an installed
// desktop client drop rows for a reason its build predates; the client folds
// unrecognised reasons into an "other" display class instead. The empty
// string is the succeeded bucket, so `.default("")` is a meaningful default
// only for a row that already lost its reason — such a row lands in the
// denominator rather than inventing a failure that never happened.
const DashboardFailureDailySchema = z.object({
  date: z.string().default(""),
  failure_reason: z.string().default(""),
  task_count: z.number().default(0),
}).loose();

export const DashboardFailureDailyListSchema = z.array(DashboardFailureDailySchema);

const DashboardFailureByAgentSchema = z.object({
  agent_id: z.string().default(""),
  failure_reason: z.string().default(""),
  task_count: z.number().default(0),
}).loose();

export const DashboardFailureByAgentListSchema = z.array(
  DashboardFailureByAgentSchema,
);

const DashboardFailureBySquadSchema = DashboardFailureByAgentSchema.extend({
  squad_id: z.string().default(""),
}).loose();

export const DashboardFailureBySquadListSchema = z.array(
  DashboardFailureBySquadSchema,
);

// Runtime usage schemas — the runtime-detail page's four usage endpoints
// (`/api/runtimes/:id/usage*`). Same leniency rules as the dashboard
// schemas above: numbers default to 0, strings to "", `.loose()` passes
// unknown fields.

const RuntimeUsageSchema = z.object({
  runtime_id: z.string().default(""),
  date: z.string().default(""),
  provider: z.string().default(""),
  model: z.string().default(""),
  input_tokens: z.number().default(0),
  output_tokens: z.number().default(0),
  cache_read_tokens: z.number().default(0),
  cache_write_tokens: z.number().default(0),
  ...CostSplitShape,
}).loose();

export const RuntimeUsageListSchema = z.array(RuntimeUsageSchema);

const RuntimeUsageByAgentSchema = z.object({
  agent_id: z.string().default(""),
  provider: z.string().default(""),
  model: z.string().default(""),
  input_tokens: z.number().default(0),
  output_tokens: z.number().default(0),
  cache_read_tokens: z.number().default(0),
  cache_write_tokens: z.number().default(0),
  ...CostSplitShape,
  task_count: z.number().default(0),
}).loose();

export const RuntimeUsageByAgentListSchema = z.array(RuntimeUsageByAgentSchema);

const RuntimeUsageByHourSchema = z.object({
  hour: z.number().default(0),
  model: z.string().default(""),
  input_tokens: z.number().default(0),
  output_tokens: z.number().default(0),
  cache_read_tokens: z.number().default(0),
  cache_write_tokens: z.number().default(0),
  ...CostSplitShape,
  task_count: z.number().default(0),
}).loose();

export const RuntimeUsageByHourListSchema = z.array(RuntimeUsageByHourSchema);

// Runtime rate limits (`GET /api/runtimes/:id/rate-limits`). Unknown window
// kinds and statuses from a newer server degrade to "other" / "" instead of
// failing the whole response, and a malformed window is dropped on its own.
const RuntimeRateLimitStatusSchema = z
  .enum(["allowed", "warning", "rejected", ""])
  .catch("");

const RuntimeRateLimitWindowSchema = z.object({
  kind: z
    .enum(["session", "weekly", "weekly_opus", "weekly_sonnet", "other"])
    .catch("other"),
  used_percent: z.number().optional().catch(undefined),
  window_minutes: z.number().default(0).catch(0),
  resets_at: z.string().optional().catch(undefined),
  status: RuntimeRateLimitStatusSchema.default(""),
}).loose();

const RuntimeRateLimitsSchema = z.object({
  plan_type: z.string().default("").catch(""),
  status: RuntimeRateLimitStatusSchema.default(""),
  windows: z
    .array(z.unknown())
    .catch([])
    .default([])
    .transform((rows) =>
      rows.flatMap((row) => {
        const parsed = RuntimeRateLimitWindowSchema.safeParse(row);
        return parsed.success ? [parsed.data] : [];
      }),
    ),
  credits: z
    .object({
      has_credits: z.boolean().default(false),
      unlimited: z.boolean().default(false),
      balance: z.string().default(""),
    })
    .loose()
    .optional()
    .catch(undefined),
  observed_at: z.string(),
}).loose();

export const RuntimeRateLimitsResponseSchema = z.object({
  rate_limits: RuntimeRateLimitsSchema.nullable().catch(null),
}).loose();

export const RuntimeUpdateSchema = z.object({
  id: z.string(),
  runtime_id: z.string().default(""),
  // An unknown future status keeps the card neutral and polling.
  status: z
    .enum(["pending", "scheduled", "running", "completed", "failed", "timeout", "cancelled", "expired"])
    .catch("pending"),
  target_version: z.string().default(""),
  output: z.string().optional().catch(undefined),
  error: z.string().optional().catch(undefined),
  active_tasks: z.number().optional().catch(undefined),
  cancellable: z.boolean().default(false).catch(false),
  created_at: z.string().default(""),
  updated_at: z.string().default(""),
}).loose();

export const ActiveRuntimeUpdateResponseSchema = z.object({
  update: RuntimeUpdateSchema.nullable().catch(null),
}).loose();

// Agent task responses. The base object stays loose so daemon/runtime fields
// can drift while task-list consumers still validate the fields they render.

// Human attribution (MUL-4302 §9): who an agent run is accountable to, and how
// that human was resolved. Every field is defensive so a departed member, an
// autopilot run (no originator), or an older backend degrades to a partial
// object instead of a parse failure.
const AttributionUserSchema = z.object({
  id: z.string().default(""),
  name: z.string().optional(),
  email: z.string().optional(),
  avatar_url: z.string().optional(),
}).loose();

const TaskEvidenceSchema = z.object({
  kind: z.string().default(""),
  ref_id: z.string().default(""),
}).loose();

const TaskAttributionSchema = z.object({
  source: z.string().default("unattributed"),
  precise: z.boolean().default(false),
  initiator: AttributionUserSchema.optional(),
  originator: AttributionUserSchema.optional(),
  evidence: TaskEvidenceSchema.optional(),
  rule_version_id: z.string().optional(),
  delegated_from_task_id: z.string().optional(),
  retry_of_task_id: z.string().optional(),
  rerun_of_task_id: z.string().optional(),
}).loose();

const TaskCancellationActorSchema = z.object({
  type: z.string().default(""),
  id: z.string().optional(),
  name: z.string().optional(),
}).loose();

const OptionalStringArraySchema = z.preprocess(
  (value) =>
    Array.isArray(value) && value.every((item) => typeof item === "string")
      ? value
      : undefined,
  z.array(z.string()).optional(),
);

// One (provider, model) slice of a run's token usage. Token counts default to
// 0 rather than failing the row: a slice missing one counter is still worth
// pricing on the counters it does have, and the "we have no usage at all" case
// is carried by the field's absence, not by a zeroed entry.
const TaskUsageSchema = z.object({
  provider: z.string().optional(),
  model: z.string().default(""),
  input_tokens: z.number().default(0),
  output_tokens: z.number().default(0),
  cache_read_tokens: z.number().default(0),
  cache_write_tokens: z.number().default(0),
  cost_usd_ticks: z.number().optional(),
  credits_micros: z.number().optional().catch(undefined),
}).loose();

export const AgentTaskSchema = z.object({
  wakeup_id: z.string().optional().catch(undefined),
  cancelled_by_comment_change: z.boolean().optional().catch(undefined),
  cancelled_by: TaskCancellationActorSchema.optional().catch(undefined),
  id: z.string(),
  agent_id: z.string().default(""),
  runtime_id: z.string().default(""),
  issue_id: z.string().default(""),
  issue_identifier: z.string().optional().catch(undefined),
  issue_title: z.string().optional().catch(undefined),
  status: z.string().default("cancelled"),
  priority: z.number().default(0),
  dispatched_at: z.string().nullable().default(null),
  started_at: z.string().nullable().default(null),
  completed_at: z.string().nullable().default(null),
  transcript_purged_at: z.string().nullable().optional().catch(undefined),
  transcript_purged_by: z.string().optional().catch(undefined),
  result: z.unknown().default(null),
  error: z.string().nullable().default(null),
  failure_reason: z.string().optional(),
  created_at: z.string().default(""),
  chat_session_id: z.string().optional(),
  autopilot_run_id: z.string().optional(),
  squad_id: z.string().optional().catch(undefined),
  is_leader_task: z.boolean().optional().catch(undefined),
  doc_id: z.string().optional().catch(undefined),
  doc_thread_id: z.string().optional().catch(undefined),
  parent_task_id: z.string().optional(),
  attempt: z.number().optional(),
  trigger_comment_id: z.string().optional(),
  // Coverage is additive display metadata. A mixed-version or partially
  // upgraded server must not make one malformed optional field erase the
  // entire execution log, so degrade that field to "absent" independently.
  coalesced_comment_ids: OptionalStringArraySchema,
  delivered_comment_ids: OptionalStringArraySchema,
  supplement_capability: z.string().optional().catch(undefined),
  supplement_comment_ids: OptionalStringArraySchema,
  can_supplement: z.boolean().optional().catch(undefined),
  trigger_summary: z.string().optional(),
  kind: z.string().optional(),
  work_dir: z.string().optional().catch(undefined),
  relative_work_dir: z.string().optional().catch(undefined),
  durable_work_dir: z.string().optional().catch(undefined),
  relative_durable_work_dir: z.string().optional().catch(undefined),
  branch_name: z.string().optional().catch(undefined),
  attribution: TaskAttributionSchema.optional(),
  // Per-run token usage. Same independent-degradation rule as the coverage
  // arrays above: usage is additive display metadata, so one malformed entry
  // must cost the row its usage figure, not erase the whole execution log.
  // `.catch(undefined)` collapses a bad array to "no usage recorded", which
  // the UI already renders as an em dash.
  usage: z.array(TaskUsageSchema).optional().catch(undefined),
}).loose();

// Outcome counts are required: every backend that serves this endpoint
// reports them, so a response missing them is a contract drift, not an
// older peer. `parseWithFallback` then degrades the whole list to `[]`
// rather than letting a partial window masquerade as measured outcomes.
export const AgentActivityBucketListSchema = z.array(z.object({
  agent_id: z.string(),
  bucket_at: z.string(),
  task_count: z.number().int().nonnegative(),
  failed_count: z.number().int().nonnegative(),
  completed_count: z.number().int().nonnegative(),
  cancelled_count: z.number().int().nonnegative(),
  duration_ms: z.number().nonnegative().optional().catch(undefined),
  duration_count: z.number().int().nonnegative().optional().catch(undefined),
}).loose());

export const AgentTaskListSchema = z.array(AgentTaskSchema);

export const AgentTaskPageSchema = z.object({
  tasks: AgentTaskListSchema,
  nextCursor: z.string().min(1).nullable(),
});

// One row of a run transcript. `output_truncated` gates a completeness claim
// the UI makes about a tool's output, so it stays `.optional()` with no
// default: a server that does not send it means "unknown", and defaulting it
// to false would turn a missing field into an assertion that the record is
// complete. `.catch(undefined)` keeps a malformed value that far too — without
// it a single bad boolean fails its row, the array fails with it, and
// `parseWithFallback` hands the viewer an empty transcript. Degrading one
// field to "unknown" is the correct loss; deleting the run is not. Every other
// field keeps a default for the same reason.
export const TaskMessagePayloadSchema = z.object({
  call_id: z.string().optional().catch(undefined),
  task_id: z.string().default(""),
  issue_id: z.string().default(""),
  chat_session_id: z.string().optional(),
  seq: z.number().default(0),
  type: z.enum(["text", "thinking", "tool_use", "tool_result", "error"]).catch("text"),
  tool: z.string().optional(),
  content: z.string().optional(),
  input: z.record(z.string(), z.unknown()).optional(),
  output: z.string().optional(),
  output_truncated: z.boolean().optional().catch(undefined),
  created_at: z.string().optional(),
  details_hidden: z.boolean().optional().catch(undefined),
}).loose();

export const TaskMessageListSchema = z.array(TaskMessagePayloadSchema).default([]);

// Context receipt (`GET /api/tasks/:id/context-receipt`, MUL-9013). Lenient:
// unknown enum values pass through as strings, and a malformed row is dropped
// rather than failing the whole receipt.
const ContextReceiptSourceSchema = z.object({
  kind: z.string(),
  name: z.string().optional(),
  bytes: z.number().catch(0),
  sha256: z.string().optional(),
  delivery: z.string().catch(""),
  path: z.string().optional(),
  load: z.string().catch("unverified"),
  verified_version: z.string().optional(),
  parent: z.string().optional(),
}).loose();

const ContextReceiptSchema = z.object({
  schema_version: z.number().catch(1),
  runtime: z.string().catch(""),
  runtime_version: z.string().optional(),
  model: z.string().optional(),
  prompt_bytes: z.number().catch(0),
  brief_bytes: z.number().catch(0),
  brief_delivery: z.string().catch(""),
  resumed_session: z.boolean().catch(false),
  fresh_retry: z.boolean().optional().catch(undefined),
  mcp_servers: z.array(z.string()).catch([]),
  sources: z
    .array(z.unknown())
    .catch([])
    .transform((rows) =>
      rows.flatMap((row) => {
        const parsed = ContextReceiptSourceSchema.safeParse(row);
        return parsed.success ? [parsed.data] : [];
      }),
    ),
}).loose();

export const TaskContextReceiptResponseSchema = z.object({
  receipt: ContextReceiptSchema.nullable().catch(null),
  recorded_at: z.string().optional(),
}).loose();

// `DELETE /api/tasks/:id/messages` only answers 200 once the log is gone, so a
// malformed body must still read as cleared; the caller fills the gaps.
export const TaskTranscriptPurgeSchema = z.object({
  transcript_purged_at: z.string().nullable().catch(null),
  transcript_purged_by: z.string().catch(""),
  messages_deleted: z.number().catch(0),
}).loose();

export const EMPTY_TASK_TRANSCRIPT_PURGE = {
  transcript_purged_at: null,
  transcript_purged_by: "",
  messages_deleted: 0,
};

// Task cancellation (`POST /api/tasks/:id/cancel`) is consumed directly by
// chat recovery. Its optional message payload must be well-formed before the
// UI deletes a message from cache or restores text into the input.
const CancelledChatMessageSchema = z.object({
  chat_session_id: z.string(),
  message_id: z.string(),
  content: z.string(),
  restore_to_input: z.boolean().default(false),
  // Attachments detached from the deleted message so a restored draft can
  // re-bind them on re-send. Absent on servers that predate the field.
  attachments: z.array(AttachmentSchema).optional(),
}).loose();

export const CancelTaskResponseSchema = AgentTaskSchema.extend({
  cancelled_chat_message: CancelledChatMessageSchema.nullish()
    .transform((value) => value ?? undefined),
}).loose();

const ChatLastMessageSchema = z.object({
  content: z.string().default(""),
  role: z.enum(["user", "assistant"]).catch("assistant"),
  created_at: z.string().default(""),
  failure_reason: z.string().nullable().optional(),
  message_kind: z.enum([
    "message",
    "no_response",
    "onboarding_kickoff",
    "onboarding_opening",
    "wakeup",
  ]).optional().catch(undefined),
}).loose();

const ChatChannelSourceSchema = z.object({
  channel_type: z.string().default(""),
  installation_id: z.string().default(""),
  route_revision: z.number().default(0),
}).loose();

export const ChatSessionSchema: z.ZodType<ChatSession> = z.object({
  id: z.string(),
  workspace_id: z.string().default(""),
  agent_id: z.string().default(""),
  creator_id: z.string().default(""),
  project_id: z.string().nullable().optional(),
  title: z.string().default(""),
  status: z.enum(["active", "archived"]).catch("active"),
  has_unread: z.boolean().default(false),
  unread_count: z.number().optional(),
  last_message: ChatLastMessageSchema.nullable().optional().catch(undefined),
  pinned: z.boolean().optional(),
  channel_source: ChatChannelSourceSchema.optional().catch(undefined),
  is_current_channel_route: z.boolean().optional().catch(undefined),
  created_at: z.string().default(""),
  updated_at: z.string().default(""),
}).loose();

export const EMPTY_CHAT_SESSION: ChatSession = {
  id: "",
  workspace_id: "",
  agent_id: "",
  creator_id: "",
  title: "",
  status: "active",
  has_unread: false,
  created_at: "",
  updated_at: "",
};
export const ChatSessionListSchema = z
  .array(ChatSessionSchema.catch(EMPTY_CHAT_SESSION))
  .transform((sessions) => sessions.filter((session) => session.id !== ""))
  .default([]);
export const EMPTY_CHAT_SESSION_LIST: ChatSession[] = [];

// Deferred-cancellation draft restores
// (`GET /api/chat/sessions/{id}/draft-restores`, #5219) feed the composer
// directly: `content` becomes the draft text, `attachments` re-bind on
// re-send, and `id` is the consume key. A malformed response falls back to
// an empty list — the durable row stays pending server-side, so nothing is
// lost by skipping a fetch.
const ChatDraftRestoreSchema = z.object({
  id: z.string(),
  chat_session_id: z.string(),
  task_id: z.string().optional(),
  content: z.string().default(""),
  attachments: z.array(AttachmentSchema).optional(),
  created_at: z.string().optional(),
}).loose();

export const ChatDraftRestoresResponseSchema = z.object({
  restores: z.array(ChatDraftRestoreSchema).default([]),
}).loose();

const ChatQueuedTaskSchema = z.object({
  task_id: z.string(),
  status: z.string().default("queued"),
  created_at: z.string().default(""),
  message_id: z.string().optional(),
  content: z.string().optional(),
  wakeup_id: z.string().optional().catch(undefined),
}).loose();

const ChatQueuedTasksSchema = z.array(z.unknown()).transform((tasks) =>
  tasks.flatMap((task) => {
    const parsed = ChatQueuedTaskSchema.safeParse(task);
    return parsed.success ? [parsed.data] : [];
  }),
);

// Root fields retain the legacy single-task response shape. Keep additive
// fields optional so callers can distinguish an older server from an empty
// queue. A malformed queue row is ignored without discarding a valid head.
export const ChatPendingTaskSchema: z.ZodType<ChatPendingTask> = z.object({
  task_id: z.string().optional(),
  status: z.string().optional(),
  created_at: z.string().optional(),
  supports_queue: z.boolean().optional(),
  can_supplement: z.boolean().optional().catch(undefined),
  queued_tasks: ChatQueuedTasksSchema.optional(),
}).loose();

export const EMPTY_CHAT_PENDING_TASK: ChatPendingTask = {};

// A malformed wakeup row is dropped rather than failing the list: the strip
// and panel are status surfaces next to the conversation, not its content.
const ChatWakeupSchema: z.ZodType<ChatWakeup> = z.object({
  id: z.string().min(1),
  chat_session_id: z.string().min(1),
  chat_session_title: z.string().optional().catch(undefined),
  agent_id: z.string().default(""),
  title: z.string().default(""),
  instruction: z.string().default(""),
  kind: z.enum(["condition", "at", "check"]),
  condition: z.record(z.string(), z.unknown()).nullish().catch(undefined),
  interval_seconds: z.number().nullable().default(null),
  fire_at: z.string().nullable().default(null),
  next_eval_at: z.string().default(""),
  expires_at: z.string().default(""),
  status: z.enum(["active", "fired", "expired", "cancelled", "failed"]),
  end_reason: z.string().nullable().default(null),
  ended_at: z.string().nullable().default(null),
  check_count: z.number().default(0),
  last_check_note: z.string().nullable().default(null),
  last_checked_at: z.string().nullable().default(null),
  last_task_id: z.string().nullable().default(null),
  last_task_status: z.string().optional().catch(undefined),
  last_fired_at: z.string().nullable().default(null),
  last_error: z.string().nullable().default(null),
  source_message_id: z.string().nullable().default(null),
  source_task_id: z.string().nullable().default(null),
  created_at: z.string().default(""),
  updated_at: z.string().default(""),
}).loose();

export const ChatWakeupListSchema = z.array(z.unknown()).transform((rows) =>
  rows.flatMap((row) => {
    const parsed = ChatWakeupSchema.safeParse(row);
    return parsed.success ? [parsed.data] : [];
  }),
);
export const EMPTY_CHAT_WAKEUP_LIST: ChatWakeup[] = [];

export const SendChatMessageResponseSchema: z.ZodType<SendChatMessageResponse> = z.object({
  steered: z.boolean().optional().catch(undefined),
  message_created_at: z.string().optional().catch(undefined),
  message_task_id: z.string().optional().catch(undefined),
  message_id: z.string().min(1),
  task_id: z.string().min(1),
  supports_queue: z.boolean().optional(),
  queued: z.boolean().optional().catch(undefined),
  created_at: z.string().min(1),
  attachment_ids: z.array(z.string()).nullish().transform((ids) => ids ?? undefined),
}).loose();

// `started` is the only field the flow branches on, and a malformed response
// must not be read as "the opening landed" — parseWithFallback's fallback says
// it did not, which leaves the flow's own retry as the recovery path.
export const StartMikaOnboardingResponseSchema: z.ZodType<StartMikaOnboardingResponse> = z.object({
  started: z.boolean(),
  message_id: z.string().nullish().transform((id) => id ?? undefined),
  created_at: z.string().nullish().transform((at) => at ?? undefined),
}).loose();

export const PrioritizeQueuedChatTaskResponseSchema:
  z.ZodType<PrioritizeQueuedChatTaskResponse> = z.object({
    task_id: z.string(),
    active_task_id: z.string().optional(),
  }).loose();

export const EMPTY_PRIORITIZE_QUEUED_CHAT_TASK_RESPONSE:
  PrioritizeQueuedChatTaskResponse = { task_id: "" };

export const EMPTY_CHAT_DRAFT_RESTORES: ChatDraftRestoresResponse = {
  restores: [],
};

export const EMPTY_CANCEL_TASK_RESPONSE: CancelTaskResponse = {
  id: "",
  agent_id: "",
  runtime_id: "",
  issue_id: "",
  status: "cancelled",
  priority: 0,
  dispatched_at: null,
  started_at: null,
  completed_at: null,
  result: null,
  error: null,
  created_at: "",
};

export const AgentBuilderSessionSchema = z.object({
  session_id: z.string(),
  builder_agent_id: z.string(),
  runtime_id: z.string(),
}).loose();

export const EMPTY_AGENT_BUILDER_SESSION: AgentBuilderSession = {
  session_id: "",
  builder_agent_id: "",
  runtime_id: "",
};

/**
 * The stored configuration of a creation conversation. Every field falls back
 * to empty on its own: a draft written by a newer build (or truncated in
 * transit) must still restore the fields it does understand rather than
 * discarding the user's work wholesale.
 */
export const StoredAgentDraftSchema = z.object({
  name: z.string().catch(""),
  description: z.string().catch(""),
  instructions: z.string().catch(""),
  conversation_starters: z
    .array(
      z.object({
        label: z.string().catch(""),
        prompt: z.string().catch(""),
      }),
    )
    .catch([]),
  avatar_url: z.string().nullable().catch(null),
  model: z.string().catch(""),
  thinking_level: z.string().catch(""),
  service_tier: z.string().catch(""),
  skill_ids: z.array(z.string()).catch([]),
  permission_scope: z
    .enum(["private", "workspace", "members"])
    .catch("private"),
  member_ids: z.array(z.string()).catch([]),
  team_ids: z.array(z.string()).catch([]),
  qoder_agent_id: z.string().catch(""),
  applied_message_id: z.string().nullable().catch(null),
  dismissed_message_id: z.string().nullish().catch(null),
}).loose();

/**
 * One unfinished creation draft. Every field except the id has a safe empty
 * default: an older server that omits `runtime_id` must degrade to "let the
 * user pick" rather than dropping the whole row and losing the conversation.
 */
export const AgentBuilderSessionSummarySchema = z.object({
  session_id: z.string(),
  title: z.string().catch(""),
  runtime_id: z.string().catch(""),
  created_at: z.string().catch(""),
  updated_at: z.string().catch(""),
  last_message_content: z.string().catch(""),
  last_message_role: z.string().catch(""),
  last_message_at: z.string().catch(""),
  // Absent for a conversation the user has never hand-edited; the client then
  // replays the last <agent_draft> block instead of restoring a stored copy.
  draft: StoredAgentDraftSchema.nullish().catch(null),
}).loose();

export const AgentBuilderSessionListSchema = z.object({
  sessions: z.array(AgentBuilderSessionSummarySchema).catch([]),
}).loose();

export const EMPTY_AGENT_BUILDER_SESSION_LIST: {
  sessions: AgentBuilderSessionSummary[];
} = { sessions: [] };

export const AgentBuilderRuntimeSwitchSchema = z.object({
  runtime_id: z.string(),
}).loose();

// This endpoint returns 2xx only after the carrier has been bound to the
// runtime the caller asked for; anything else is a thrown error and no commit.
// So the safe fallback for an unparseable SUCCESS body is the requested id, not
// an empty one: the rebind did happen, and reporting "unknown" would leave the
// picker showing a runtime that is no longer executing — the exact split this
// endpoint exists to close.
export const agentBuilderRuntimeSwitchFallback = (
  requestedRuntimeID: string,
): AgentBuilderRuntimeSwitch => ({ runtime_id: requestedRuntimeID });

// Squad list responses carry lightweight membership previews used by hover
// cards. The preview fields are additive API fields, so older backends default
// cleanly to no preview instead of breaking newer frontends.
const SquadMemberPreviewSchema = z.object({
  member_type: z.string(),
  member_id: z.string(),
  role: z.string().default(""),
}).loose();

export const SquadSchema = z.object({
  id: z.string(),
  workspace_id: z.string(),
  name: z.string(),
  description: z.string().default(""),
  instructions: z.string().default(""),
  system_key: z.string().optional(),
  system_instructions: z.string().optional(),
  avatar_url: z.string().nullable().optional().transform((v) => v ?? null),
  leader_id: z.string(),
  creator_id: z.string(),
  created_at: z.string(),
  updated_at: z.string(),
  archived_at: z.string().nullable().optional().transform((v) => v ?? null),
  archived_by: z.string().nullable().optional().transform((v) => v ?? null),
  member_count: z.number().default(0),
  member_preview: z.array(SquadMemberPreviewSchema).default([]),
  // Older backends predate handoff modes; they always routed through the leader.
  handoff_mode: z.enum(["leader", "direct"]).catch("leader").default("leader"),
}).loose();

export const SquadListSchema = z.array(SquadSchema);
export const EMPTY_SQUAD_LIST: Squad[] = [];
export const EMPTY_SQUAD: Squad = {
  id: "",
  workspace_id: "",
  name: "",
  description: "",
  instructions: "",
  avatar_url: null,
  leader_id: "",
  creator_id: "",
  created_at: "",
  updated_at: "",
  archived_at: null,
  archived_by: null,
  member_count: 0,
  member_preview: [],
  handoff_mode: "leader",
};

// Squad member status — backs the Squad detail page's Members tab. status
// is `string | null` (not the narrow `SquadMemberStatusValue` union) so a
// new server-side status doesn't fail the parse; the UI defaults to a
// neutral pill for unknown values.
const SquadActiveIssueBriefSchema = z.object({
  issue_id: z.string(),
  identifier: z.string(),
  title: z.string(),
  issue_status: z.string(),
}).loose();

const SquadMemberStatusSchema = z.object({
  member_type: z.string(),
  member_id: z.string(),
  status: z.string().nullable().optional().transform((v) => v ?? null),
  active_issues: z.array(SquadActiveIssueBriefSchema).default([]),
  last_active_at: z.string().nullable().optional().transform((v) => v ?? null),
}).loose();

export const SquadMemberStatusListResponseSchema = z.object({
  members: z.array(SquadMemberStatusSchema).default([]),
}).loose();

export const EMPTY_SQUAD_MEMBER_STATUS_LIST = { members: [] };

export const SquadBriefsResponseSchema = z.object({
  handoff_mode: z.enum(["leader", "direct"]).catch("leader").default("leader"),
  briefs: z.array(z.object({
    member_type: z.string(),
    member_id: z.string(),
    is_leader: z.boolean().default(false),
    brief: z.string().default(""),
  }).loose()).default([]),
}).loose();

export const EMPTY_SQUAD_BRIEFS = { handoff_mode: "leader" as const, briefs: [] };

// Structured error body — POST /api/workspaces/:wsId/issues 409 conflict.
//
// When the server detects an active issue with the same title in the same
// workspace, it returns `{ code: "active_duplicate_issue", error, issue }`
// instead of letting the create through. The UI uses the embedded issue ref
// to offer "view existing" rather than dropping the user into a generic
// "create failed" toast.
//
// Strict guarantees:
//   - `code` is a literal so a future server rename (e.g. `duplicate_issue`)
//     fails the parse and falls back to a normal error toast — drift never
//     ships as a broken duplicate UI.
//   - `issue` is required; without an id/identifier/title the "view existing"
//     button has nothing to point at, so we'd rather fall back than guess.
//   - `issue.status` is intentionally OMITTED: the duplicate toast doesn't
//     render a StatusIcon (which has no fallback for unknown enum values),
//     so a future server-side rename of `status` must not knock this branch
//     out. `.loose()` lets the field pass through unchanged for any other
//     consumer.

export const DuplicateIssueErrorBodySchema = z.object({
  code: z.literal("active_duplicate_issue"),
  error: z.string().optional(),
  issue: z.object({
    id: z.string(),
    identifier: z.string(),
    title: z.string(),
  }).loose(),
}).loose();

// A destructive confirmation must fail closed on an incomplete plan. Do not
// default missing arrays to empty or silently discard malformed entries.
export const RuntimeAccessRevocationPlanSchema = z.object({
  code: z.enum(["runtime_has_nonowner_dependents", "runtime_access_revocation_plan_changed"]),
  nonowner_agents: z.array(z.object({ id: z.string().uuid(), name: z.string() })),
  active_task_ids: z.array(z.string().uuid()),
}).refine((body) =>
  new Set(body.nonowner_agents.map((agent) => agent.id)).size === body.nonowner_agents.length &&
  new Set(body.active_task_ids).size === body.active_task_ids.length,
).transform((body) => ({ agents: body.nonowner_agents, activeTaskIds: body.active_task_ids }));

export interface DuplicateIssueErrorBody {
  code: "active_duplicate_issue";
  error?: string;
  issue: {
    id: string;
    identifier: string;
    title: string;
  };
}

// Webhook delivery schemas — backing the Autopilot Deliveries section. Enums
// (`status`, `signature_status`, `provider`) are kept as `z.string()` so a
// future server-side value (e.g. a Stripe provider, a new dedupe state)
// degrades to a generic UI fallback rather than collapsing the list into
// the empty array. `.loose()` lets unknown fields pass through, matching
// the rule used by every other endpoint here.

const WebhookPayloadConditionFailureSchema = z.object({
  filter_index: z.number().int().nonnegative(),
  condition_index: z.number().int().nonnegative(),
  condition: z.object({
    path: z.string(),
    op: z.enum(["equals", "not_equals", "contains", "in", "exists"]),
    value: z.union([z.string(), z.number(), z.boolean()]).optional(),
    values: z.array(z.union([z.string(), z.number(), z.boolean()])).optional(),
    ignore_case: z.boolean().optional(),
  }).loose(),
  actual_values: z.array(z.string()),
  missing: z.boolean(),
  truncated: z.boolean(),
}).loose();

const WebhookDeliverySchema = z.object({
  id: z.string(),
  workspace_id: z.string(),
  autopilot_id: z.string(),
  trigger_id: z.string(),
  provider: z.string(),
  event: z.string(),
  dedupe_key: z.string().nullable(),
  dedupe_source: z.string().nullable(),
  signature_status: z.string(),
  status: z.string(),
  attempt_count: z.number().default(0),
  // Older servers predate the durable dispatch queue. Defaults preserve
  // compatibility while the UI rolls out alongside the new worker.
  dispatch_attempts: z.number().default(0),
  available_at: z.string().default(""),
  content_type: z.string().nullable(),
  response_status: z.number().nullable(),
  autopilot_run_id: z.string().nullable(),
  replayed_from_delivery_id: z.string().nullable(),
  error: z.string().nullable(),
  reason_code: z.string().nullable().default(null),
  payload_condition_failure: WebhookPayloadConditionFailureSchema.nullish().catch(null),
  replay_idempotency_key: z.string().nullable().default(null),
  received_at: z.string(),
  last_attempt_at: z.string(),
  created_at: z.string(),
  // Detail-only fields. The list endpoint omits them; the detail endpoint
  // populates raw_body / selected_headers / response_body.
  selected_headers: z.record(z.string(), z.unknown()).nullable().optional(),
  raw_body: z.string().nullable().optional(),
  response_body: z.string().nullable().optional(),
}).loose();

export const ListWebhookDeliveriesResponseSchema = z.object({
  deliveries: z.array(WebhookDeliverySchema).default([]),
  total: z.number().default(0),
}).loose();

export const WebhookDeliveryResponseSchema = WebhookDeliverySchema;

export const EMPTY_LIST_WEBHOOK_DELIVERIES_RESPONSE: ListWebhookDeliveriesResponse = {
  deliveries: [],
  total: 0,
};

// Autopilot list schema. Enums (`status`, `execution_mode`, `trigger_kinds`,
// `last_run_status`) stay `z.string()` so future server-side values degrade
// to a generic UI fallback. The three derived fields (trigger_kinds /
// next_run_at / last_run_status) are list-endpoint-only and absent on older
// servers — optional by contract, the list renders "—" without them.

const AutopilotListItemSchema = z.object({
  id: z.string(),
  workspace_id: z.string(),
  title: z.string(),
  description: z.string().nullable().optional(),
  project_id: z.string().nullable().optional(),
  // Older servers (pre-MUL-2429) omit assignee_type; "agent" is the
  // documented default.
  assignee_type: z.string().default("agent"),
  assignee_id: z.string(),
  status: z.string(),
  execution_mode: z.string(),
  issue_title_template: z.string().nullable().optional(),
  created_by_type: z.string(),
  created_by_id: z.string(),
  last_run_at: z.string().nullable().optional(),
  created_at: z.string(),
  updated_at: z.string(),
  trigger_kinds: z.array(z.string()).optional(),
  next_run_at: z.string().nullable().optional(),
  last_run_status: z.string().nullable().optional(),
  // Per-caller write capability; absent on older servers (treated as unknown).
  can_write: z.boolean().optional(),
  // Narrower per-caller access-management capability (detail endpoint only).
  can_manage_access: z.boolean().optional(),
}).loose();

export const ListAutopilotsResponseSchema = z.object({
  autopilots: z.array(AutopilotListItemSchema).default([]),
  total: z.number().default(0),
}).loose();

export const EMPTY_LIST_AUTOPILOTS_RESPONSE = {
  autopilots: [],
  total: 0,
};

// Autopilot run (POST /trigger, GET /runs). Consumed by the "run now" flow,
// which branches on `status` to avoid a false-success toast (MUL-4525), so the
// response must be schema-parsed. `reason_code` is an additive, stable
// classification of a non-success run the UI localizes; older servers omit it.
// Defaults are conservative: an unreadable run degrades to a non-success status
// so the UI never shows success it cannot confirm. .loose() tolerates new fields.
export const AutopilotRunSchema = z.object({
  id: z.string().default(""),
  autopilot_id: z.string().default(""),
  trigger_id: z.string().nullable().default(null),
  source: z.string().default("manual"),
  status: z.string().default("failed"),
  issue_id: z.string().nullable().default(null),
  task_id: z.string().nullable().default(null),
  triggered_at: z.string().default(""),
  completed_at: z.string().nullable().default(null),
  failure_reason: z.string().nullable().default(null),
  reason_code: z.string().optional(),
  trigger_payload: z.unknown().default(null),
  result: z.unknown().default(null),
  created_at: z.string().default(""),
}).loose();

export const AutopilotQuotaUsageSchema = z.object({
  action: z.enum(["off", "observe", "enforce"]).default("off"),
  used: z.number().nullable().default(null),
  reserved: z.number().nullable().default(null),
  total: z.number().nullable().default(null),
  limit: z.number().nullable().default(null),
  reached: z.boolean().nullable().default(null),
  period_start: z.string().nullable().default(null),
  period_end: z.string().nullable().default(null),
  reset_at: z.string().nullable().default(null),
  blocked_counts: z.record(z.string(), z.number().int().nonnegative()).nullable().catch(null).default(null),
}).loose();

export const FALLBACK_AUTOPILOT_RUN: AutopilotRun = {
  id: "",
  autopilot_id: "",
  trigger_id: null,
  source: "manual",
  status: "failed",
  issue_id: null,
  task_id: null,
  triggered_at: "",
  completed_at: null,
  failure_reason: null,
  trigger_payload: null,
  result: null,
  created_at: "",
};

// Cron preview: the server is the authority on the next occurrences. No
// `.default([])` here — a missing or reshaped field must fail validation so it
// degrades to the `next_runs: null` fallback ("preview unreadable") instead of
// masquerading as a valid empty list ("this expression never fires").
export const CronPreviewResponseSchema = z.object({
  next_runs: z.array(z.string()),
}).loose();

export const UNREADABLE_CRON_PREVIEW_RESPONSE: CronPreviewResponse = {
  next_runs: null,
};

export const EMPTY_WEBHOOK_DELIVERY: WebhookDelivery = {
  id: "",
  workspace_id: "",
  autopilot_id: "",
  trigger_id: "",
  provider: "",
  event: "",
  dedupe_key: null,
  dedupe_source: null,
  signature_status: "not_required",
  status: "queued",
  attempt_count: 0,
  dispatch_attempts: 0,
  available_at: "",
  content_type: null,
  response_status: null,
  autopilot_run_id: null,
  replayed_from_delivery_id: null,
  error: null,
  reason_code: null,
  replay_idempotency_key: null,
  received_at: "",
  last_attempt_at: "",
  created_at: "",
};

// User (`/api/me` GET + PATCH). The auth store and Settings → Account both
// trust this shape — a drift here would knock both surfaces out. Kept
// lenient by the same rules as IssueSchema: enums stay `z.string()`,
// nullable fields are unioned with `null`, unknown server fields pass
// through via `.loose()`. `profile_description` is the field added in
// MUL-2406; the server emits `""` when unset (NOT NULL DEFAULT ''), so
// the schema defaults to `""` too — keeps the type tight without
// breaking older backends that don't return the column yet.

export const UserSchema = z.object({
  id: z.string(),
  name: z.string().default(""),
  email: z.string().default(""),
  avatar_url: z.string().nullable().default(null),
  onboarded_at: z.string().nullable().default(null),
  onboarding_questionnaire: z.record(z.string(), z.unknown()).default({}),
  language: z.string().nullable().default(null),
  profile_description: z.string().default(""),
  timezone: z.string().nullable().default(null),
  // No default: absence is the "server cannot track" signal. A malformed
  // value degrades to that state instead of discarding the whole user.
  whats_new_seen_version: z.string().nullable().optional().catch(undefined),
  // Same "absent means unsupported" contract as above.
  star_prompt_shown_count: z.number().int().nonnegative().optional().catch(undefined),
  star_prompt_last_shown_at: z.string().nullable().optional().catch(undefined),
  star_prompt_starred_at: z.string().nullable().optional().catch(undefined),
  created_at: z.string().default(""),
  updated_at: z.string().default(""),
}).loose();

export const EMPTY_USER: User = {
  id: "",
  name: "",
  email: "",
  avatar_url: null,
  onboarded_at: null,
  onboarding_questionnaire: {},
  language: null,
  profile_description: "",
  timezone: null,
  created_at: "",
  updated_at: "",
};

// Cross-workspace unread inbox summary (`/api/inbox/unread-summary` GET).
// One entry per workspace the user belongs to that has unread items; the
// sidebar derives the workspace-switcher dot from it. Lenient per the usual
// rules so a future field addition can't blank the dot — on malformed JSON
// parseWithFallback returns the empty list, which simply hides the dot.

export const InboxUnreadSummarySchema = z.array(
  z
    .object({
      workspace_id: z.string(),
      count: z.number(),
    })
    .loose(),
);

export const EMPTY_INBOX_UNREAD_SUMMARY: InboxWorkspaceUnread[] = [];

// The viewer's unread issues (`/api/issues/unread` GET), behind the sidebar's
// unread dot. `outcome` stays a string so a future kind still lights the dot;
// on malformed JSON parseWithFallback returns the empty list, which hides it.

export const IssueUnreadPageSchema = z
  .object({
    items: z.array(
      z
        .object({
          issue_id: z.string(),
          outcome: z.string(),
          agent_id: z.string().nullish().transform((v) => v ?? null),
          task_id: z.string().nullish().transform((v) => v ?? null),
          comment_id: z.string().nullish().transform((v) => v ?? null),
          created_at: z.string(),
        })
        .loose(),
    ),
    next_cursor: z.string().nullish().transform((v) => v ?? null),
  })
  .loose();

export const EMPTY_ISSUE_UNREAD_PAGE: { items: IssueUnread[]; next_cursor: string | null } = {
  items: [],
  next_cursor: null,
};

// Reading one issue's notifications (`/api/inbox/issues/{id}/read` POST).
// `count` is how many rows the write read; the client refreshes its inbox
// caches only when it is above zero. A body without a usable count cannot say
// that nothing changed, so the client treats it as unknown and refreshes.
export const IssueInboxReadSchema = z
  .object({
    count: z.number().int().nonnegative(),
  })
  .loose();

// Deletion returns the number of notifications removed from the selected view.
export const InboxDeleteSchema = z
  .object({ count: z.number().int().nonnegative() })
  .loose();

// The viewer's subscribed issue ids (`/api/issues/subscribed` GET), behind the
// "subscribed" relation filter on client-filtered surfaces. A non-string entry
// is dropped; a body without the list has no safe fallback — an empty set
// would hide every row under the filter — so the client fails the request.
export const SubscribedIssueIdsSchema = z
  .object({
    issue_ids: z
      .array(z.unknown())
      .transform((ids) => ids.filter((id): id is string => typeof id === "string" && id.length > 0)),
  })
  .loose();

// The squads behind the viewer's "my agents" relation (`/api/squads/involved`
// GET). Same rules as `SubscribedIssueIdsSchema`: an empty set would hide
// every squad row under the filter, so a body without the list fails.
export const InvolvedSquadIdsSchema = z
  .object({
    squad_ids: z
      .array(z.unknown())
      .transform((ids) => ids.filter((id): id is string => typeof id === "string" && id.length > 0)),
  })
  .loose();

// Inbox items (`/api/inbox` and `/api/inbox/archived` GET).
// Lenient per the usual rules: `severity` / `type` / `recipient_type` stay
// `z.string()` so a notification kind this client doesn't know yet still
// parses and renders (the UI's type-label lookup already tolerates unknown
// kinds). Nullable optional fields are declared optional as well, since older
// rows can omit them entirely. On malformed JSON parseWithFallback returns the
// empty list — the affected view then reads as empty rather than white-
// screening the inbox. Both endpoints share this boundary because they return
// the same row shape and both feed the status/priority filter UI.

export const InboxItemListSchema = z.array(
  z
    .object({
      id: z.string(),
      workspace_id: z.string(),
      recipient_type: z.string(),
      recipient_id: z.string(),
      type: z.string(),
      severity: z.string(),
      issue_id: z.string().nullish(),
      // Only picks the link; a bad value falls back to the issue_id link
      // instead of failing the whole list.
      issue_identifier: z.string().min(1).nullish().catch(null),
      title: z.string(),
      body: z.string().nullish(),
      issue_status: z.string().nullish(),
      issue_priority: z.string().nullish(),
      issue_project_id: z.string().nullish(),
      issue_assignee_type: z.string().nullish(),
      issue_assignee_id: z.string().nullish(),
      issue_due_date: z.string().nullish(),
      issue_label_ids: z.array(z.string()).nullish(),
      read: z.boolean(),
      archived: z.boolean(),
      created_at: z.string(),
      snoozed_until: z.string().nullish(),
      snoozed_at: z.string().nullish(),
    })
    .loose(),
);

// The viewer's snoozed issues (`/api/inbox/snoozed` GET). A malformed body
// reads as nothing snoozed.
export const SnoozedInboxListSchema = z.array(
  z
    .object({
      issue_id: z.string(),
      // Older servers omit it; the row then links by issue_id.
      issue_identifier: z.string().min(1).nullish().catch(null),
      // Older servers omit them; the row then fetches the issue.
      issue_title: z.string().nullish().catch(null),
      issue_status: z.string().min(1).nullish().catch(null),
      item_id: z.string(),
      snoozed_until: z.string(),
      snoozed_at: z.string(),
      updates: z.number().int().nonnegative(),
    })
    .loose(),
);

export const ArchivedInboxPageSchema = z.object({
  items: InboxItemListSchema,
  next_cursor: z.string().min(1).nullable(),
  has_more: z.boolean(),
}).refine((page) => page.has_more === (page.next_cursor !== null) &&
  (!page.has_more || page.items.length > 0))
  .transform((page) => ({ items: page.items, nextCursor: page.next_cursor, hasMore: page.has_more }));

const FacetCountsSchema = z.record(z.string(), z.number().int().nonnegative());

// The type / project / assignee / label counts arrived later; an older server
// omits them, so they default to empty rather than failing the whole menu.
export const ArchivedInboxFacetsSchema = z.object({
  statuses: FacetCountsSchema,
  priorities: FacetCountsSchema,
  actors: FacetCountsSchema,
  types: FacetCountsSchema.optional(),
  projects: FacetCountsSchema.optional(),
  assignees: FacetCountsSchema.optional(),
  labels: FacetCountsSchema.optional(),
  unread_count: z.number().int().nonnegative(),
}).transform((facets) => ({
  statuses: facets.statuses, priorities: facets.priorities,
  actors: facets.actors, types: facets.types ?? {},
  projects: facets.projects ?? {}, assignees: facets.assignees ?? {},
  labels: facets.labels ?? {}, unreadCount: facets.unread_count,
}));

export const EMPTY_INBOX_ITEMS: InboxItem[] = [];
export const EMPTY_SNOOZED_INBOX: SnoozedInboxIssue[] = [];

// Billing schemas (cloud-billing proxy surface)
//
// All billing JSON we receive comes from multica-cloud verbatim — we proxy
// the bytes without re-shaping. These schemas use `loose()` so a future
// non-breaking field addition on the cloud side doesn't crash us; required
// fields are still strictly enforced. EMPTY_* constants supply the
// fallback parseWithFallback uses when the upstream response is malformed
// or unparseable.

export const BillingBalanceSchema = z.object({
  owner_id: z.string(),
  balance_micro: z.number(),
  balance_credit: z.number(),
  updated_at: z.string(),
}).loose();

export const EMPTY_BILLING_BALANCE: BillingBalance = {
  owner_id: "",
  balance_micro: 0,
  balance_credit: 0,
  updated_at: "",
};

// `tx_type` and `source` are kept as plain strings here; the cloud doc
// enumerates the canonical values but the frontend display tolerates
// unknown ones gracefully. Strict enums would crash the page on a future
// addition (e.g. a new `topup` source kind).
export const BillingTransactionSchema = z.object({
  id: z.string(),
  owner_id: z.string(),
  idempotency_key: z.string().default(""),
  tx_type: z.string(),
  source: z.string(),
  amount_micro: z.number(),
  balance_after: z.number(),
  reference_id: z.string().default(""),
  description: z.string().default(""),
  metadata: z.record(z.string(), z.unknown()).default({}),
  created_at: z.string(),
}).loose();

export const BillingTransactionsPageSchema = z.object({
  items: z.array(BillingTransactionSchema).default([]),
  total: z.number().default(0),
  page: z.number().default(1),
  page_size: z.number().default(20),
}).loose();

export const EMPTY_BILLING_TRANSACTIONS_PAGE: BillingTransactionsPage = {
  items: [],
  total: 0,
  page: 1,
  page_size: 20,
};

export const BillingBatchSchema = z.object({
  id: z.string(),
  owner_id: z.string(),
  source_tx_id: z.string().default(""),
  source_type: z.string(),
  total_micro: z.number(),
  remaining_micro: z.number(),
  // Cloud either omits the key (never expires) or sends a string
  // timestamp. Null is also tolerated since some serializers emit
  // explicit nulls for absent timestamps.
  expires_at: z.string().nullable().optional(),
  created_at: z.string(),
  updated_at: z.string(),
}).loose();

export const BillingBatchesPageSchema = z.object({
  items: z.array(BillingBatchSchema).default([]),
  total: z.number().default(0),
  page: z.number().default(1),
  page_size: z.number().default(20),
}).loose();

export const EMPTY_BILLING_BATCHES_PAGE: BillingBatchesPage = {
  items: [],
  total: 0,
  page: 1,
  page_size: 20,
};

export const BillingTopupSchema = z.object({
  id: z.string(),
  owner_id: z.string(),
  amount_cents: z.number(),
  currency: z.string().default("usd"),
  credits: z.number(),
  bonus_credits: z.number().default(0),
  status: z.string(),
  tier_id: z.string().default(""),
  stripe_checkout_id: z.string().default(""),
  // Only set after status reaches `credited` — leave optional rather
  // than coerce to "" so a UI can branch on existence.
  purchase_batch_id: z.string().optional(),
  created_at: z.string(),
  updated_at: z.string(),
}).loose();

export const BillingTopupsPageSchema = z.object({
  items: z.array(BillingTopupSchema).default([]),
  total: z.number().default(0),
  page: z.number().default(1),
  page_size: z.number().default(20),
}).loose();

export const EMPTY_BILLING_TOPUPS_PAGE: BillingTopupsPage = {
  items: [],
  total: 0,
  page: 1,
  page_size: 20,
};

export const BillingPriceTierSchema = z.object({
  id: z.string(),
  // Cloud doc says display_name falls back to id; tolerate empty too.
  display_name: z.string().default(""),
  amount_cents: z.number(),
  credits: z.number(),
  bonus_credits: z.number().optional(),
  bonus_expires_in: z.string().optional(),
}).loose();

export const BillingPriceTierListSchema = z.array(BillingPriceTierSchema);

export const EMPTY_BILLING_PRICE_TIER_LIST: BillingPriceTier[] = [];

export const CreateBillingCheckoutSessionResponseSchema = z.object({
  order_id: z.string(),
  session_id: z.string(),
  url: z.string(),
}).loose();

export const EMPTY_CREATE_BILLING_CHECKOUT_SESSION_RESPONSE: CreateBillingCheckoutSessionResponse = {
  order_id: "",
  session_id: "",
  url: "",
};

export const BillingCheckoutSessionStatusSchema = z.object({
  order_id: z.string(),
  status: z.string(),
  amount_cents: z.number(),
  credits: z.number(),
  bonus_credits: z.number().default(0),
  currency: z.string().default("usd"),
  tier_id: z.string().default(""),
}).loose();

export const EMPTY_BILLING_CHECKOUT_SESSION_STATUS: BillingCheckoutSessionStatus = {
  order_id: "",
  status: "pending",
  amount_cents: 0,
  credits: 0,
  bonus_credits: 0,
  currency: "usd",
  tier_id: "",
};

export const CreateBillingPortalSessionResponseSchema = z.object({
  url: z.string(),
}).loose();

export const EMPTY_CREATE_BILLING_PORTAL_SESSION_RESPONSE: CreateBillingPortalSessionResponse = {
  url: "",
};

// Workspace subscriptions (`/api/cloud-subscriptions/*`)
//
// These schemas are the compatibility boundary with multica-cloud. Three rules
// hold for all of them:
//
//  1. There is no fallback value. Callers get `null` on any parse failure and
//     must render "unavailable" — never a synthetic Free plan, because that
//     turns an upstream outage or an older cloud into a silent downgrade of a
//     paying workspace.
//  2. `.loose()` keeps unknown keys, so a cloud that adds fields does not break
//     an older client.
//  3. `plan` and `status` stay open strings. A new plan or Stripe status must
//     surface as unknown rather than be coerced into a known one.

const WorkspaceSubscriptionIntervalSchema = z.enum(["month", "year"]);
const WorkspaceSubscriptionPaidPlanSchema = z.enum(["pro", "business"]);

// Stripe hosts Checkout and Portal, so those URLs leave the app. `z.string()
// .url()` is not enough on its own — `new URL("javascript:...")` parses — and
// the caller hands this value to location.assign, so the scheme is pinned here.
const StripeHostedURLSchema = z.string().url().refine(
  (value) => value.startsWith("https://"),
  { message: "Stripe hosted URL must use HTTPS" },
);

const WorkspaceEntitlementLimitSchema = z
  .discriminatedUnion("mode", [
    z
      .object({
        mode: z.literal("limited"),
        limit: z.number().int().positive(),
      })
      .loose(),
    z.object({ mode: z.literal("unlimited") }).loose(),
  ])
  .transform(
    (value): WorkspaceSubscriptionEntitlements["limits"]["autopilotRuns"] =>
      value.mode === "limited"
        ? { mode: "limited", limit: value.limit }
        : { mode: "unlimited", limit: null },
  );

export const WorkspaceSubscriptionEntitlementsSchema = z
  .object({
    workspace_id: z.string(),
    plan: z.string(),
    status: z.string(),
    // Cloud documents seats as >= 1, but accepting 0 costs nothing and keeps a
    // workspace that momentarily reports no human members readable instead of
    // failing the whole snapshot.
    seats: z.number().int().nonnegative(),
    limits: z
      .object({
        autopilot_runs: WorkspaceEntitlementLimitSchema,
        // Optional: a Cloud without the storage limit omits it, and a
        // malformed one must not fail the whole snapshot.
        storage_bytes: WorkspaceEntitlementLimitSchema.nullable().optional().catch(null),
        // Newer than autopilot_runs: an older Cloud omits it, and a malformed
        // one only hides its own row.
        dashboard_history_days: WorkspaceEntitlementLimitSchema.nullable().catch(null).default(null),
        // Newer still: an older Cloud omits it, and a malformed one only
        // hides its own row.
        app_tool_calls: WorkspaceEntitlementLimitSchema.nullable().catch(null).default(null),
      })
      .loose(),
    current_period_end: z.string().nullable().optional(),
    snapshot_expires_at: z.string().nullable().optional(),
    version: z.number().int().nonnegative(),
  })
  .loose()
  .transform(
    (value): WorkspaceSubscriptionEntitlements => ({
      workspaceId: value.workspace_id,
      plan: value.plan,
      status: value.status,
      seats: value.seats,
      limits: {
        autopilotRuns: value.limits.autopilot_runs,
        storageBytes: value.limits.storage_bytes ?? null,
        dashboardHistoryDays: value.limits.dashboard_history_days,
        appToolCalls: value.limits.app_tool_calls,
      },
      currentPeriodEnd: value.current_period_end ?? null,
      snapshotExpiresAt: value.snapshot_expires_at ?? null,
      version: value.version,
    }),
  );

export const WorkspaceSubscriptionSummarySchema = z
  .object({
    entitlement: WorkspaceSubscriptionEntitlementsSchema,
    billing_interval: WorkspaceSubscriptionIntervalSchema.nullable(),
    human_members: z.number().int().nonnegative(),
    seat_capacity: z
      .object({
        purchased: z.number().int().positive(),
        used: z.number().int().nonnegative(),
        reserved: z.number().int().nonnegative(),
        available: z.number().int().nonnegative(),
        overcommitted: z.boolean(),
        version: z.number().int().positive(),
        pending_quantity: z.number().int().positive().nullable(),
        active_purchase: z
          .object({
            request_id: z.string(),
            target_seats: z.number().int().positive(),
            status: z.enum(["pending", "processing", "submitted"]),
            expires_at: z.string().min(1).optional(),
          })
          .loose()
          .optional(),
      })
      .loose()
      .nullable(),
    cancel_at_period_end: z.boolean(),
    grace_until: z.string().nullable(),
    has_stripe_customer: z.boolean(),
    available_actions: z.object({
      checkout: z.boolean(),
      portal: z.boolean(),
      purchase_seats: z.boolean(),
      // Absent on a cloud that predates plan switching: no switch is offered.
      change_plan: z.boolean().optional(),
      check_plan_change: z.boolean().optional(),
      // Absent on a cloud that predates add-ons; a malformed value offers none.
      purchase_autopilot_runs: z.boolean().optional().catch(undefined),
    }).loose(),
    // Absent on an older cloud, and a malformed value reads as no add-ons:
    // the limit in the entitlement already includes them.
    autopilot_addon: z
      .object({
        purchased_runs: z.number().int().nonnegative(),
        period_start: z.string().min(1),
        period_end: z.string().min(1),
      })
      .loose()
      .nullable()
      .optional()
      .catch(null),
    // Absent on an older cloud, and a malformed value reads as no hold: the
    // server still refuses the operations a hold blocks.
    plan_change: z
      .object({
        target_plan: z.string().min(1),
        // An unknown state from a newer cloud is shown as still confirming,
        // the state that asks the user to wait rather than act.
        state: z.enum(["confirming", "unresolved"]).catch("confirming"),
        since: z.string().min(1),
      })
      .loose()
      .nullable()
      .optional()
      .catch(null),
    // Absent on a cloud that predates trials. A malformed value reads as no
    // trial: the entitlement still carries the plan the workspace has.
    trial: z
      .object({
        plan: z.string().min(1),
        state: z.enum(["active", "expired", "converted"]),
        started_at: z.string().min(1),
        ends_at: z.string().min(1),
      })
      .loose()
      .refine(
        (value) =>
          !Number.isNaN(Date.parse(value.started_at)) &&
          !Number.isNaN(Date.parse(value.ends_at)),
      )
      .nullable()
      .optional()
      .catch(null),
  })
  .loose()
  .transform(
    (value): WorkspaceSubscriptionSummary => ({
      entitlement: value.entitlement,
      billingInterval: value.billing_interval,
      humanMembers: value.human_members,
      seatCapacity: value.seat_capacity
        ? {
            purchased: value.seat_capacity.purchased,
            used: value.seat_capacity.used,
            reserved: value.seat_capacity.reserved,
            available: value.seat_capacity.available,
            overcommitted: value.seat_capacity.overcommitted,
            version: value.seat_capacity.version,
            pendingQuantity: value.seat_capacity.pending_quantity,
            activePurchase: value.seat_capacity.active_purchase
              ? {
                  requestId:
                    value.seat_capacity.active_purchase.request_id,
                  targetSeats:
                    value.seat_capacity.active_purchase.target_seats,
                  status: value.seat_capacity.active_purchase.status,
                  expiresAt:
                    value.seat_capacity.active_purchase.expires_at ?? null,
                }
              : null,
          }
        : null,
      cancelAtPeriodEnd: value.cancel_at_period_end,
      graceUntil: value.grace_until,
      hasStripeCustomer: value.has_stripe_customer,
      availableActions: {
        checkout: value.available_actions.checkout,
        portal: value.available_actions.portal,
        purchaseSeats: value.available_actions.purchase_seats,
        changePlan: value.available_actions.change_plan ?? false,
        checkPlanChange: value.available_actions.check_plan_change ?? false,
        purchaseAutopilotRuns:
          value.available_actions.purchase_autopilot_runs ?? false,
      },
      autopilotAddon: value.autopilot_addon
        ? {
            purchasedRuns: value.autopilot_addon.purchased_runs,
            periodStart: value.autopilot_addon.period_start,
            periodEnd: value.autopilot_addon.period_end,
          }
        : null,
      planChange: value.plan_change
        ? {
            targetPlan: value.plan_change.target_plan,
            state: value.plan_change.state,
            since: value.plan_change.since,
          }
        : null,
      trial: value.trial
        ? {
            plan: value.trial.plan,
            state: value.trial.state,
            startedAt: value.trial.started_at,
            endsAt: value.trial.ends_at,
          }
        : null,
    }),
  );

const BATCH_ISSUE_OUTCOMES: readonly string[] = [
  "updated",
  "unchanged",
  "skipped",
  "would_update",
  "deleted",
  "would_delete",
] satisfies BatchIssueOutcome[];

// An outcome this client does not know is not a confirmed write: it reads as
// skipped, never as success.
const BatchIssueResultSchema = z
  .object({
    issue_id: z.string().min(1),
    identifier: z.string().optional().catch(undefined),
    outcome: z.string(),
    code: z.string().optional().catch(undefined),
    message: z.string().optional().catch(undefined),
  })
  .loose()
  .transform(
    (value): BatchIssueResult => ({
      issue_id: value.issue_id,
      identifier: value.identifier,
      outcome: BATCH_ISSUE_OUTCOMES.includes(value.outcome) ? (value.outcome as BatchIssueOutcome) : "skipped",
      code: value.code,
      message: value.message,
    }),
  );

/** `results` is absent on servers that predate per-issue batch results. */
export const BatchUpdateIssuesResponseSchema = z
  .object({
    updated: z.number().int().nonnegative(),
    results: z.array(BatchIssueResultSchema).optional(),
    dry_run: z.boolean().optional().catch(undefined),
  })
  .loose()
  .transform(
    (value): BatchUpdateIssuesResponse => ({
      updated: value.updated,
      ...(value.results ? { results: value.results } : {}),
      ...(value.dry_run !== undefined ? { dry_run: value.dry_run } : {}),
    }),
  );

/** A batch response that failed validation: whatever was written is unknown. */
export const UNCONFIRMED_BATCH_UPDATE: BatchUpdateIssuesResponse = { updated: 0, unconfirmed: true };

const WorkspaceSubscriptionPriceSchema = (
  expected: "month" | "year",
) =>
  z
    .object({
      currency: z.string().min(1),
      // Reject 0 and negatives: a free or malformed Price must read as
      // "price unavailable", not as a real amount shown next to a purchase
      // button.
      unit_amount: z.number().int().positive(),
      // Pinned to the slot it arrived in. Cloud validates this too, but a
      // schema that accepted a yearly Price under `month` would let the UI
      // quote a yearly amount as a monthly one — the schema is an independent
      // boundary, so it checks the correspondence itself.
      interval: z.literal(expected),
      interval_count: z.literal(1),
    })
    .loose()
    .transform(
      (value): WorkspaceSubscriptionPrice => ({
        currency: value.currency,
        unitAmount: value.unit_amount,
        interval: value.interval,
        intervalCount: value.interval_count,
      }),
    );

const WorkspaceSubscriptionPlanPricesSchema = z
  .object({
    month: WorkspaceSubscriptionPriceSchema("month"),
    year: WorkspaceSubscriptionPriceSchema("year"),
  })
  .loose()
  .transform(
    (value): WorkspaceSubscriptionPlanPrices => ({
      month: value.month,
      year: value.year,
    }),
  );

// The add-on pack is optional and read on its own: a missing or malformed
// pack price hides the add-on without taking the plan prices down with it.
const WorkspaceAutopilotAddonPriceSchema = z
  .object({
    currency: z.string().regex(/^[a-z]{3}$/),
    unit_amount: z.number().int().positive(),
    runs_per_pack: z.number().int().positive(),
  })
  .loose()
  .transform(
    (value): WorkspaceAutopilotAddonPrice => ({
      currency: value.currency,
      unitAmount: value.unit_amount,
      runsPerPack: value.runs_per_pack,
    }),
  );

// Every paid plan must be priced: a response missing one plan reads as "prices
// unavailable" as a whole rather than offering one plan without the other.
// A day count from a newer or malformed Cloud reads as unknown, never as a
// number: the plan labels it feeds are hints, and a wrong one would point a
// user at the wrong plan.
const PlanDaysSchema = z.number().int().positive().nullable().catch(null).default(null);

const WorkspacePlanLimitSchema = z
  .object({ dashboard_history_days: PlanDaysSchema })
  .loose()
  .transform((value) => ({ dashboardHistoryDays: value.dashboard_history_days }));

// Plan limits are optional: an older Cloud does not send them, and a malformed
// block reads as absent rather than failing the prices themselves.
const WorkspacePlanLimitsSchema = z
  .object({
    free: WorkspacePlanLimitSchema,
    pro: WorkspacePlanLimitSchema,
    business: WorkspacePlanLimitSchema,
  })
  .loose()
  .transform(
    (value): WorkspacePlanLimits => ({
      free: value.free,
      pro: value.pro,
      business: value.business,
    }),
  )
  .nullable()
  .catch(null)
  .default(null);

export const WorkspaceSubscriptionPricesSchema = z
  .object({
    pro: WorkspaceSubscriptionPlanPricesSchema,
    business: WorkspaceSubscriptionPlanPricesSchema,
    autopilot_addon: WorkspaceAutopilotAddonPriceSchema.nullable()
      .optional()
      .catch(null),
    plan_limits: WorkspacePlanLimitsSchema,
  })
  .loose()
  .transform(
    (value): WorkspaceSubscriptionPrices => ({
      pro: value.pro,
      business: value.business,
      autopilotAddon: value.autopilot_addon ?? null,
      planLimits: value.plan_limits,
    }),
  );

export const CreateWorkspaceAutopilotAddonCheckoutResponseSchema = z
  .object({
    session_id: z.string().min(1),
    url: StripeHostedURLSchema,
  })
  .loose()
  .transform(
    (value): CreateWorkspaceAutopilotAddonCheckoutResponse => ({
      sessionId: value.session_id,
      url: value.url,
    }),
  );

// A session status this client does not know reads as open: the page keeps
// waiting for Stripe instead of claiming success or failure.
export const WorkspaceAutopilotAddonCheckoutSchema = z
  .object({
    session_id: z.string().min(1),
    status: z.enum(["paid", "open", "unpaid", "expired"]).catch("open"),
    packs: z.number().int().positive(),
    runs: z.number().int().positive(),
    currency: z.string().regex(/^[a-z]{3}$/),
    amount: z.number().int().nonnegative(),
  })
  .loose()
  .transform(
    (value): WorkspaceAutopilotAddonCheckout => ({
      sessionId: value.session_id,
      status: value.status,
      packs: value.packs,
      runs: value.runs,
      currency: value.currency,
      amount: value.amount,
    }),
  );

// An unknown state from a newer server reads as available: the server still
// enforces on write, so the worst case is an upgrade prompt after the attempt
// instead of before it. A malformed entry is dropped the same way.
const WorkspaceFeatureStateSchema = z
  .object({ state: z.enum(["available", "locked"]).catch("available") })
  .loose()
  .transform((value): WorkspaceFeatureState => value.state)
  .catch("available" as const);

// A limit that is missing (an older server) or malformed reads as none: the
// server still cuts every dashboard response to the enforced range.
const WorkspaceFeatureLimitsSchema = z
  .object({ dashboard_history_days: PlanDaysSchema })
  .loose()
  .transform((value) => ({ dashboardHistoryDays: value.dashboard_history_days }))
  .catch({ dashboardHistoryDays: null })
  .default({ dashboardHistoryDays: null });

export const WorkspaceFeaturesSchema = z
  .object({
    features: z.record(z.string(), WorkspaceFeatureStateSchema).catch({}),
    limits: WorkspaceFeatureLimitsSchema,
  })
  .loose()
  .transform(
    (value): WorkspaceFeatures => ({
      features: value.features as WorkspaceFeatures["features"],
      limits: value.limits,
    }),
  );

// A limit that cannot be read, or an action from a newer server, reads as off:
// the server still checks every admission, so the worst case is a refusal
// shown after the attempt instead of before it.
export const MemberLimitSchema = z
  .object({
    action: z.enum(["off", "observe", "enforce"]).catch("off"),
    limit: z.number().int().nonnegative().nullable().catch(null).default(null),
    members: z.number().int().nonnegative().nullable().catch(null).default(null),
    pending_invitations: z.number().int().nonnegative().nullable().catch(null).default(null),
  })
  .loose()
  .transform(
    (value): MemberLimit =>
      value.action === "off" || value.limit === null
        ? { action: "off", limit: null, members: null, pendingInvitations: null }
        : {
            action: value.action,
            limit: value.limit,
            members: value.members ?? 0,
            pendingInvitations: value.pending_invitations ?? 0,
          },
  );

// Storage usage that cannot be read, or an action from a newer server, reads
// as off: the server still checks every upload.
export const StorageUsageSchema = z
  .object({
    action: z.enum(["off", "observe", "enforce"]).catch("off"),
    used_bytes: z.number().int().nonnegative().nullable().catch(null).default(null),
    limit_bytes: z.number().int().nonnegative().nullable().catch(null).default(null),
    reached: z.boolean().nullable().catch(null).default(null),
  })
  .loose()
  .transform(
    (value): StorageUsage =>
      value.action === "off" || value.limit_bytes === null
        ? { action: "off", usedBytes: null, limitBytes: null, reached: null }
        : {
            action: value.action,
            // Missing when the server could not read usage in time.
            usedBytes: value.used_bytes,
            limitBytes: value.limit_bytes,
            reached:
              value.action === "enforce" && value.used_bytes !== null ? (value.reached ?? false) : null,
          },
  );

// App call usage that cannot be read, or an action from a newer server, reads
// as off: the gateway still checks every call. A malformed breakdown only
// drops the breakdown.
export const AppCallUsageSchema = z
  .object({
    action: z.enum(["off", "observe", "enforce"]).catch("off"),
    used: z.number().int().nonnegative().nullable().catch(null).default(null),
    limit: z.number().int().nonnegative().nullable().catch(null).default(null),
    reached: z.boolean().nullable().catch(null).default(null),
    reset_at: z.string().nullable().catch(null).default(null),
    members: z
      .array(
        z.object({
          user_id: z.string().nullable().catch(null).default(null),
          calls: z.number().int().nonnegative(),
        }),
      )
      .nullable()
      .catch(null)
      .default(null),
  })
  .loose()
  .transform(
    (value): AppCallUsage =>
      value.action === "off" || value.limit === null || value.used === null
        ? { action: "off", used: null, limit: null, reached: null, resetAt: null, members: [] }
        : {
            action: value.action,
            used: value.used,
            limit: value.limit,
            reached: value.action === "enforce" ? (value.reached ?? value.used >= value.limit) : null,
            resetAt: value.reset_at,
            members: (value.members ?? []).map((m) => ({ userId: m.user_id, calls: m.calls })),
          },
  );

export const CreateWorkspaceSubscriptionCheckoutResponseSchema = z
  .object({
    request_id: z.string(),
    session_id: z.string(),
    url: StripeHostedURLSchema,
  })
  .loose()
  .transform(
    (value): CreateWorkspaceSubscriptionCheckoutResponse => ({
      requestId: value.request_id,
      sessionId: value.session_id,
      url: value.url,
    }),
  );

export const WorkspaceSubscriptionSeatReconcileResultSchema = z
  .object({
    workspace_id: z.string(),
    action: z.string(),
    version: z.number().int().nonnegative(),
  })
  .loose()
  .transform(
    (value): WorkspaceSubscriptionSeatReconcileResult => ({
      workspaceId: value.workspace_id,
      action: value.action,
      version: value.version,
    }),
  );

export const WorkspaceSeatPurchasePreviewSchema = z
  .object({
    current_seats: z.number().int().positive(),
    additional_seats: z.number().int().positive(),
    resulting_seats: z.number().int().positive(),
    purchase_version: z.number().int().positive(),
    currency: z.string().regex(/^[a-z]{3}$/),
    proration_amount: z.number().int().nonnegative(),
    next_invoice_amount: z.number().int().nonnegative(),
    quoted_at: z.string().min(1),
  })
  .loose()
  .transform(
    (value): WorkspaceSeatPurchasePreview => ({
      currentSeats: value.current_seats,
      additionalSeats: value.additional_seats,
      resultingSeats: value.resulting_seats,
      purchaseVersion: value.purchase_version,
      currency: value.currency,
      prorationAmount: value.proration_amount,
      nextInvoiceAmount: value.next_invoice_amount,
      quotedAt: value.quoted_at,
    }),
  );

export const PurchaseWorkspaceSeatsResponseSchema = z
  .object({
    request_id: z.string(),
    current_seats: z.number().int().positive(),
    additional_seats: z.number().int().positive(),
    resulting_seats: z.number().int().positive(),
    currency: z.string().regex(/^[a-z]{3}$/),
    proration_amount: z.number().int().nonnegative(),
    next_invoice_amount: z.number().int().nonnegative(),
    status: z.enum(["pending", "submitted", "confirmed"]),
  })
  .loose()
  .transform(
    (value): PurchaseWorkspaceSeatsResponse => ({
      requestId: value.request_id,
      currentSeats: value.current_seats,
      additionalSeats: value.additional_seats,
      resultingSeats: value.resulting_seats,
      currency: value.currency,
      prorationAmount: value.proration_amount,
      nextInvoiceAmount: value.next_invoice_amount,
      status: value.status,
    }),
  );

export const WorkspacePlanChangePreviewSchema = z
  .object({
    current_plan: WorkspaceSubscriptionPaidPlanSchema,
    target_plan: WorkspaceSubscriptionPaidPlanSchema,
    interval: WorkspaceSubscriptionIntervalSchema,
    seats: z.number().int().positive(),
    subscription_version: z.number().int().positive(),
    currency: z.string().regex(/^[a-z]{3}$/),
    // Negative for a downgrade: the unused difference is a credit.
    proration_amount: z.number().int(),
    next_invoice_amount: z.number().int().nonnegative(),
    invoice_now: z.boolean(),
    quoted_at: z.string().min(1),
    // Absent on a cloud that predates add-ons.
    autopilot_runs_limit_after: z
      .number()
      .int()
      .nonnegative()
      .nullable()
      .optional()
      .catch(null),
  })
  .loose()
  .refine((value) => value.current_plan !== value.target_plan, {
    message: "plan change preview must name two different plans",
  })
  .transform(
    (value): WorkspacePlanChangePreview => ({
      currentPlan: value.current_plan,
      targetPlan: value.target_plan,
      interval: value.interval,
      seats: value.seats,
      subscriptionVersion: value.subscription_version,
      currency: value.currency,
      prorationAmount: value.proration_amount,
      nextInvoiceAmount: value.next_invoice_amount,
      invoiceNow: value.invoice_now,
      quotedAt: value.quoted_at,
      autopilotRunsLimitAfter: value.autopilot_runs_limit_after ?? null,
    }),
  );

export const ChangeWorkspacePlanResponseSchema = z
  .object({
    plan: WorkspaceSubscriptionPaidPlanSchema,
    previous_plan: WorkspaceSubscriptionPaidPlanSchema,
    interval: WorkspaceSubscriptionIntervalSchema,
    seats: z.number().int().positive(),
    currency: z.string().regex(/^[a-z]{3}$/),
    proration_amount: z.number().int(),
    invoice_now: z.boolean(),
  })
  .loose()
  .transform(
    (value): ChangeWorkspacePlanResponse => ({
      plan: value.plan,
      previousPlan: value.previous_plan,
      interval: value.interval,
      seats: value.seats,
      currency: value.currency,
      prorationAmount: value.proration_amount,
      invoiceNow: value.invoice_now,
    }),
  );

export const CreateWorkspaceSubscriptionPortalResponseSchema = z
  .object({
    url: StripeHostedURLSchema,
  })
  .loose()
  .transform(
    (value): CreateWorkspaceSubscriptionPortalResponse => ({
      url: value.url,
    }),
  );

// Runtime model discovery (`POST /api/runtimes/:id/models`,
// `GET /api/runtimes/:id/models/:requestId`). Both endpoints return the same
// request record, and the UI drives a state machine off `status`, so the two
// fields that decide behaviour are pinned: `status` gates the polling loop and
// `supported` gates whether the picker is usable at all. Everything else stays
// lenient per the rules at the top of this file.
//
// `status` deliberately stays `z.string()` (a newer server may add a state);
// `resolveRuntimeModels` treats anything it does not recognise as an explicit
// failure rather than a completed-but-empty catalog. `supported` defaults to
// true so a server old enough to omit it keeps the picker enabled instead of
// rendering "managed by runtime" off an `undefined`.
//
// `cached` / `cached_at` are additive markers for a snapshot served from the
// server-side catalog cache (MUL-5444); an older backend omits them.

const RuntimeModelThinkingLevelSchema = z.object({
  value: z.string(),
  label: z.string().default(""),
  description: z.string().optional(),
}).loose();

const RuntimeModelThinkingSchema = z.object({
  supported_levels: z.array(RuntimeModelThinkingLevelSchema).default([]),
  default_level: z.string().optional(),
}).loose();

const RuntimeModelServiceTierSchema = z.object({
  id: z.string(),
  name: z.string().default(""),
  description: z.string().optional(),
}).loose();

// A model entry with no `id` is unselectable — `onChange(m.id)` would persist
// an empty model — so `id` is required and a malformed entry drops the whole
// response to the fallback rather than rendering a dead row.
const RuntimeModelSchema = z.object({
  id: z.string(),
  label: z.string().default(""),
  provider: z.string().optional(),
  default: z.boolean().optional(),
  thinking: RuntimeModelThinkingSchema.nullable().optional()
    .transform((v) => v ?? undefined),
  service_tiers: z.array(RuntimeModelServiceTierSchema).optional(),
  supports_explicit_standard_service_tier: z.boolean().optional(),
}).loose();

// A row the runtime named but will not run (MUL-6961). Parsed from its own
// top-level list, never from `models`, so nothing here can become a selectable
// value. `id` is required for the same reason it is on RuntimeModelSchema — a
// row without one cannot even be keyed in a list.
const RuntimeUnavailableModelSchema = z.object({
  id: z.string(),
  label: z.string().default(""),
  reason: z.string().optional(),
}).loose();

export const RuntimeModelListRequestSchema = z.object({
  id: z.string().default(""),
  runtime_id: z.string().default(""),
  status: z.string(),
  models: z.array(RuntimeModelSchema).optional(),
  // Absent on any daemon or server older than the field, which simply means
  // the picker shows no unavailable section.
  unavailable_models: z.array(RuntimeUnavailableModelSchema).optional(),
  supported: z.boolean().default(true),
  error: z.string().optional(),
  created_at: z.string().default(""),
  updated_at: z.string().default(""),
  cached: z.boolean().optional(),
  cached_at: z.string().optional(),
}).loose();

// Fallback for an unparseable model-discovery response. `failed` is the only
// honest choice: `completed` would fabricate an empty catalog (and silently
// clear a saved model when `supported` is read as false), while `pending`
// would spin the picker until the client-side poll timeout. `failed` surfaces
// "discovery failed" immediately and leaves the creatable manual-entry field
// working, which is the same degradation as a real discovery failure.
export const MALFORMED_RUNTIME_MODEL_LIST_REQUEST: RuntimeModelListRequest = {
  id: "",
  runtime_id: "",
  status: "failed",
  supported: true,
  error: "invalid model discovery response",
  created_at: "",
  updated_at: "",
};

export const DingTalkInstallationSchema = z.object({
  id: z.string(),
  workspace_id: z.string().default(""),
  agent_id: z.string().default(""),
  installer_user_id: z.string().default(""),
  status: z.string().default("revoked"),
  installed_at: z.string().default(""),
  created_at: z.string().default(""),
  updated_at: z.string().default(""),
  agent_available: z.boolean().optional(),
  bound_dingtalk_user_ids: z.array(z.string()).catch([]).default([]),
}).loose();

export const EMPTY_DINGTALK_INSTALLATION: DingTalkInstallation = {
  id: "",
  workspace_id: "",
  agent_id: "",
  installer_user_id: "",
  status: "revoked",
  installed_at: "",
  created_at: "",
  updated_at: "",
  bound_dingtalk_user_ids: [],
};

export const ListDingTalkInstallationsResponseSchema = z.object({
  installations: z.array(DingTalkInstallationSchema).default([]),
  configured: z.boolean().default(false),
  install_supported: z.boolean().optional(),
}).loose();

export const EMPTY_LIST_DINGTALK_INSTALLATIONS_RESPONSE: ListDingTalkInstallationsResponse = {
  installations: [],
  configured: false,
};

export const DingTalkGroupBotSchema = z.object({
  installation_id: z.string().default(""),
  agent_id: z.string().default(""),
  bot_name: z.string().default(""),
  bot_identity_issue: z.string().default(""),
  last_active_at: z.string().optional(),
  mention_count: z.number().int().nonnegative().optional(),
}).loose();

export const DingTalkGroupSchema = z.object({
  conversation_id: z.string(),
  conversation_title: z.string().default(""),
  bots: z.array(DingTalkGroupBotSchema).catch([]).default([]),
}).loose();

export const ListDingTalkGroupsResponseSchema = z.object({
  groups: z.array(DingTalkGroupSchema).default([]),
  group_discovery_supported: z.boolean().default(false),
  inactive_group_counts: z.record(z.string(), z.number().int().nonnegative()).optional(),
  bot_identities: z.record(z.string(), DingTalkGroupBotSchema).optional(),
  next_offset: z.number().int().nonnegative().optional(),
}).loose();

export const EMPTY_LIST_DINGTALK_GROUPS_RESPONSE: ListDingTalkGroupsResponse = {
  groups: [],
  group_discovery_supported: false,
};

export const RedeemDingTalkBindingTokenResponseSchema = z.object({
  workspace_id: z.string().default(""),
  installation_id: z.string().default(""),
  dingtalk_user_id: z.string().default(""),
}).loose();

export const EMPTY_REDEEM_DINGTALK_BINDING_TOKEN_RESPONSE: RedeemDingTalkBindingTokenResponse = {
  workspace_id: "",
  installation_id: "",
  dingtalk_user_id: "",
};

// WeCom smart-bot ("智能机器人" / aibot) installation responses. `.loose()` so a
// newer backend field never fails the parse on an older desktop build (see
// CLAUDE.md → API Compatibility). Defaults are chosen so a malformed response
// degrades safely: `configured` defaults false (renders the "ask your operator"
// state rather than a Connect dialog whose submit is guaranteed to fail), and a
// missing `status` defaults to "revoked" rather than "active" so a broken read
// never shows a bot as connected when it may not be.
export const WecomInstallationSchema = z.object({
  id: z.string(),
  workspace_id: z.string().default(""),
  agent_id: z.string().default(""),
  bot_id: z.string().default(""),
  bot_name: z.string().optional(),
  installer_user_id: z.string().default(""),
  status: z.string().default("revoked"),
}).loose();

export const EMPTY_WECOM_INSTALLATION: WecomInstallation = {
  id: "",
  workspace_id: "",
  agent_id: "",
  bot_id: "",
  installer_user_id: "",
  status: "revoked",
};

export const ListWecomInstallationsResponseSchema = z.object({
  installations: z.array(WecomInstallationSchema).default([]),
  configured: z.boolean().default(false),
  install_supported: z.boolean().optional(),
  // The settings tab gates the QR install entry point on this, so it is
  // declared and validated here rather than left to pass through the loose
  // object unchecked.
  scan_install_supported: z.boolean().optional(),
}).loose();

export const EMPTY_LIST_WECOM_INSTALLATIONS_RESPONSE: ListWecomInstallationsResponse = {
  installations: [],
  configured: false,
};

export const BeginWecomInstallResponseSchema = z.object({
  session_id: z.string(),
  status: z.string().default("error"),
  poll_interval_seconds: z.number().optional().default(1),
}).loose();

// A malformed begin response degrades to a terminal error rather than a session
// the client would poll forever with an empty id.
export const MALFORMED_BEGIN_WECOM_INSTALL_RESPONSE: BeginWecomInstallResponse = {
  session_id: "",
  status: "error",
  poll_interval_seconds: 1,
};

export const WecomInstallStatusResponseSchema = z.object({
  status: z.string().default("error"),
  qr_code_url: z.string().optional(),
  expires_in_seconds: z.number().optional(),
  poll_interval_seconds: z.number().optional().default(2),
  installation_id: z.string().optional(),
  error_reason: z.string().optional(),
}).loose();

// Same reasoning: an unparseable status stops the poll loop instead of leaving
// the dialog spinning on a response nobody can read.
export const MALFORMED_WECOM_INSTALL_STATUS_RESPONSE: WecomInstallStatusResponse = {
  status: "error",
  poll_interval_seconds: 2,
  error_reason: "internal_error",
};

export const RedeemWecomBindingTokenResponseSchema = z.object({
  workspace_id: z.string().default(""),
  installation_id: z.string().default(""),
  wecom_user_id: z.string().default(""),
}).loose();

export const EMPTY_REDEEM_WECOM_BINDING_TOKEN_RESPONSE: RedeemWecomBindingTokenResponse = {
  workspace_id: "",
  installation_id: "",
  wecom_user_id: "",
};

export const TelegramInstallationSchema = z.object({
  id: z.string(),
  workspace_id: z.string().default(""),
  agent_id: z.string().default(""),
  bot_id: z.string().default(""),
  bot_username: z.string().default(""),
  installer_user_id: z.string().default(""),
  status: z.string().default("revoked"),
  installed_at: z.string().default(""),
  created_at: z.string().default(""),
  updated_at: z.string().default(""),
}).loose();

export const EMPTY_TELEGRAM_INSTALLATION: TelegramInstallation = {
  id: "",
  workspace_id: "",
  agent_id: "",
  bot_id: "",
  bot_username: "",
  installer_user_id: "",
  status: "revoked",
  installed_at: "",
  created_at: "",
  updated_at: "",
};

export const ListTelegramInstallationsResponseSchema = z.object({
  installations: z.array(TelegramInstallationSchema).default([]),
  configured: z.boolean().default(false),
  install_supported: z.boolean().optional(),
}).loose();

export const EMPTY_LIST_TELEGRAM_INSTALLATIONS_RESPONSE: ListTelegramInstallationsResponse = {
  installations: [],
  configured: false,
};

export const RedeemTelegramBindingTokenResponseSchema = z.object({
  workspace_id: z.string().default(""),
  installation_id: z.string().default(""),
  telegram_user_id: z.string().default(""),
}).loose();

export const EMPTY_REDEEM_TELEGRAM_BINDING_TOKEN_RESPONSE: RedeemTelegramBindingTokenResponse = {
  workspace_id: "",
  installation_id: "",
  telegram_user_id: "",
};

// Skills. Introduced for `POST /api/skills/:id/refresh` (update a skill from
// its imported source). `config` stays a loose record: the server owns the
// `origin` provenance shape and may extend it freely.
export const SkillFileSchema = z.object({
  id: z.string(),
  skill_id: z.string(),
  path: z.string(),
  content: z.string().optional().default(""),
  created_at: z.string().optional().default(""),
  updated_at: z.string().optional().default(""),
}).loose();

// Workspace list shape (`GET /api/skills`). Intentionally no `content` /
// `files`: those belong on the detail endpoint. Defaulting them here used
// to invent empty bodies on every list row (GH #2174).
export const SkillSummarySchema = z.object({
  id: z.string(),
  workspace_id: z.string(),
  name: z.string(),
  description: z.string().optional().default(""),
  config: z.record(z.string(), z.unknown()).optional().default({}),
  created_by: z.string().nullable().optional().default(null),
  created_at: z.string().optional().default(""),
  updated_at: z.string().optional().default(""),
  enabled: z.boolean().optional(),
  // Catch-to-empty so a missing/malformed labels field cannot fail the
  // whole skill (or the list it lives in). Older backends omit it; the
  // filter treats an empty array as "no labels".
  labels: z.array(LabelSchema).catch([]),
}).loose();

export const EMPTY_SKILL_SUMMARY: SkillSummary = {
  id: "",
  workspace_id: "",
  name: "",
  description: "",
  config: {},
  created_by: null,
  created_at: "",
  updated_at: "",
  labels: [],
};

export const SkillSchema = SkillSummarySchema.extend({
  content: z.string().optional().default(""),
  files: z.array(SkillFileSchema).optional().default([]),
}).loose();

export const EMPTY_SKILL: Skill = {
  ...EMPTY_SKILL_SUMMARY,
  content: "",
  files: [],
};

export const SkillSummaryListSchema = z.array(SkillSummarySchema).default([]);

const SKILL_VERSION_SOURCES = [
  "baseline",
  "create",
  "update",
  "import",
  "refresh",
  "restore",
] as const;

export const SkillVersionSummarySchema = z.object({
  id: z.string(),
  skill_id: z.string(),
  version: z.number(),
  name: z.string().optional().default(""),
  source: z.enum(SKILL_VERSION_SOURCES).catch("update"),
  restored_from_version: z.number().nullable().optional().default(null),
  created_by: z.string().nullable().optional().default(null),
  created_at: z.string().optional().default(""),
}).loose();

export const SkillVersionSummaryListSchema = z
  .array(SkillVersionSummarySchema)
  .default([]);

export const EMPTY_SKILL_VERSION_SUMMARY_LIST: SkillVersionSummary[] = [];

export const SkillVersionSchema = SkillVersionSummarySchema.extend({
  description: z.string().optional().default(""),
  content: z.string().optional().default(""),
  files: z
    .array(z.object({ path: z.string(), content: z.string().optional().default("") }).loose())
    .optional()
    .default([]),
}).loose();

export const EMPTY_SKILL_VERSION: SkillVersion = {
  id: "",
  skill_id: "",
  version: 0,
  name: "",
  source: "update",
  restored_from_version: null,
  created_by: null,
  created_at: "",
  description: "",
  content: "",
  files: [],
};

export const EMPTY_SKILL_SUMMARY_LIST: SkillSummary[] = [];

export const SkillImportExistingSkillSchema = z.object({
  id: z.string(),
  name: z.string(),
  created_by: z.string().optional(),
  can_overwrite: z.boolean().optional(),
}).loose();

/**
 * Envelope of POST /api/skills/import.
 *
 * `status` stays a plain string (not an enum) so a status added by a newer
 * backend still parses and its `reason` survives to the user. `z.enum` here
 * would fail the whole envelope on an unknown value, drop the server's reason
 * and leave only a generic "Import failed" — the server field is a bare
 * `string`, so it is free to grow. `skillFromImportResult` has the default
 * branch: anything outside created/updated is treated as a failure.
 */
export const SkillImportResultSchema = z.object({
  status: z.string().default("failed"),
  reason: z.string().optional().default(""),
  skill: SkillSchema.optional(),
  existing_skill: SkillImportExistingSkillSchema.optional(),
}).loose();

export const EMPTY_SKILL_IMPORT_RESULT: SkillImportResult = {
  status: "failed",
  reason: "",
};

/**
 * Read shape of one workspace MCP server.
 *
 * This is the ONLY schema in this file that must not be `.loose()`. Everywhere
 * else, keeping unknown fields is forward-compatibility; here it would be a
 * hole in the write-only boundary — a server that regressed to returning the
 * stored entry (or a `url` / `headers` on the summary) would have it land in
 * the parsed object and in the query cache. zod strips unknown keys by
 * default, so the client only ever holds the safe summary.
 *
 * `transport` stays a plain string (not an enum) so an unknown value from a
 * newer backend still parses — the UI has a default branch for it.
 */
export const WorkspaceMcpServerSchema = z.object({
  id: z.string().default(""),
  workspace_id: z.string().default(""),
  name: z.string().default(""),
  transport: z.string().default("unknown"),
  enabled: z.boolean().optional(),
  // Older servers omit it; a malformed value drops to "unknown" rather than
  // failing the whole list.
  agent_count: z.number().int().nonnegative().optional().catch(undefined),
  created_at: z.string().default(""),
  updated_at: z.string().default(""),
});

export const WorkspaceMcpServerListSchema = z.array(WorkspaceMcpServerSchema);

export const EMPTY_WORKSPACE_MCP_SERVER: WorkspaceMcpServer = {
  id: "",
  workspace_id: "",
  name: "",
  transport: "unknown",
  created_at: "",
  updated_at: "",
};

// Share links. Introduced with the workspace share-link invite flow; schemas
// mirror the API responses so malformed payloads fall back to safe defaults.
export const ShareLinkSchema = z.object({
  id: z.string(),
  workspace_id: z.string(),
  code: z.string(),
  created_by: z.string(),
  role: z.string(),
  expires_at: z.string().nullable().optional().default(null),
  max_uses: z.number().nullable().optional().default(null),
  use_count: z.number().optional().default(0),
  is_active: z.boolean().optional().default(true),
  created_at: z.string().optional().default(""),
  creator_name: z.string().optional().default(""),
  creator_email: z.string().optional().default(""),
}).loose();

export const EMPTY_SHARE_LINK: ShareLink = {
  id: "",
  workspace_id: "",
  code: "",
  created_by: "",
  role: "member",
  expires_at: null,
  max_uses: null,
  use_count: 0,
  is_active: false,
  created_at: "",
};

export const ShareLinkListResponseSchema = z.array(ShareLinkSchema).default([]);

export const ShareLinkInfoSchema = z.object({
  workspace_name: z.string().optional().default(""),
  workspace_slug: z.string().optional().default(""),
  creator_name: z.string().optional().default(""),
  role: z.string().optional().default("member"),
}).loose();

export const EMPTY_SHARE_LINK_INFO: ShareLinkInfo = {
  workspace_name: "",
  workspace_slug: "",
  role: "member",
};

export const MemberWithUserSchema = z.object({
  id: z.string(),
  workspace_id: z.string(),
  user_id: z.string(),
  role: z.string(),
  created_at: z.string().optional().default(""),
  name: z.string().optional().default(""),
  email: z.string().optional().default(""),
  avatar_url: z.string().nullable().optional().default(null),
}).loose();

export const JoinShareLinkResponseSchema = z.object({
  member: MemberWithUserSchema,
  workspace_id: z.string(),
  workspace_slug: z.string().optional().default(""),
}).loose();

export const EMPTY_JOIN_SHARE_LINK_RESPONSE: {
  member: MemberWithUser;
  workspace_id: string;
  workspace_slug: string;
} = {
  member: {
    id: "",
    workspace_id: "",
    user_id: "",
    role: "member",
    created_at: "",
    name: "",
    email: "",
    avatar_url: null,
  },
  workspace_id: "",
  workspace_slug: "",
};

export const WakeupConditionSchema = z.union([
  z.object({ type: z.literal("issue_field"), field: z.literal("status"), value: z.string() }),
  z.object({ type: z.literal("issue_field"), field: z.literal("assignee"), assignee_type: z.enum(["member", "agent", "squad"]), assignee_id: z.string() }),
  z.object({ type: z.literal("issue_field"), field: z.literal("label"), label_id: z.string() }),
  z.object({ type: z.literal("issue_field"), field: z.literal("property"), property_id: z.string(), value: z.unknown() }),
  z.object({ type: z.literal("children_done"), stage: z.number().int().nullish() }),
  z.object({ type: z.literal("pull_request"), event: z.enum(["checks_finished", "merged", "conflicts"]) }),
  z.object({ type: z.literal("other_issue"), issue_id: z.string(), state: z.enum(["done", "ended", "in_review", "blocked"]), identifier: z.string().optional() }),
]);

export const WakeupRunSchema = z.object({
  id: z.string(), status: z.string(), created_at: z.string(),
  started_at: z.string().nullable(), completed_at: z.string().nullable(),
  checkin_note: z.string().default(""), triggers: z.array(z.string()).default([]),
  commented: z.boolean().default(false),
});

export const PausedWakeupSchema = z.object({
  issue_id: z.string(), id: z.string(), agent_id: z.string(),
  paused_reason: z.enum(["max_fires", "loop", "rate"]).catch("rate"),
});

export const IssueWakeupSchema = z.object({
  id: z.string(), issue_id: z.string(), agent_id: z.string(), agent_name: z.string().default(""),
  instruction: z.string(), kind: z.enum(["event", "at", "every", "cron"]), mode: z.enum(["once", "continuous"]),
  event_types: z.array(z.string()), filter_agent_id: z.string().nullable(), filter_task_id: z.string().nullable(),
  interval_seconds: z.number().nullable(), cron_expression: z.string().nullable(), timezone: z.string(),
  next_fire_at: z.string().nullable(), enabled: z.boolean(), disabled_at: z.string().nullable(),
  last_task_id: z.string().nullable(), last_error: z.string().nullable(),
  filter_actor_type: z.enum(["member", "agent"]).nullable().optional(),
  filter_actor_id: z.string().nullable().optional(),
  filter_actor_name: z.string().nullable().optional(),
  revision: z.number().int().positive().optional(),
  filter_agent_name: z.string().nullable().optional(), last_task_status: z.string().nullable().optional(),
  expires_at: z.string().nullish(), expiry_seconds: z.number().nullish(),
  on_timeout: z.enum(["wake", "end"]).nullish().catch(null), timed_out_at: z.string().nullish(),
  created_by_agent: z.boolean().optional(), created_by_name: z.string().nullish(),
  source_agent_id: z.string().nullish(), source_agent_name: z.string().nullish(),
  // An unknown condition shape from a newer server reads as "no condition".
  condition: WakeupConditionSchema.nullish().catch(null),
  max_fires: z.number().int().nullish(), fire_count: z.number().int().nonnegative().optional().catch(undefined),
  paused_reason: z.enum(["max_fires", "loop", "rate"]).nullish().catch(null),
});

export const SystemWakeupSchema = z.object({
  id: z.string().default(""), revision: z.number().int().nonnegative().default(0),
  rule: z.literal("child_done"), enabled: z.boolean(), instruction: z.string().default(""),
  default_instruction: z.string().default(""), customized: z.boolean().default(false),
  paused_reason: z.enum(["max_fires", "loop", "rate"]).nullish().catch(null).transform((v) => v ?? null),
  staged: z.boolean(), stage: z.number().int().nullable(), total: z.number().int().nonnegative(),
  remaining: z.number().int().nonnegative(), waiting: z.array(z.string()).default([]),
  target: z.object({ type: z.enum(["agent", "squad", "member"]), id: z.string(), name: z.string() }).nullable().catch(null),
  blocked: z.enum(["", "backlog", "member_assignee", "no_assignee"]).catch(""),
  workspace_default: z.boolean().default(true),
});

export const WorkspaceSystemWakeupSchema = z.object({
  rule: z.literal("child_done"), enabled: z.boolean().default(true), instruction: z.string().default(""),
  builtin_instruction: z.string().default(""), customized: z.number().int().nonnegative().default(0),
});

export const IssueWakeupSummaryRowSchema = IssueWakeupSchema.pick({
  id: true, issue_id: true, agent_id: true, agent_name: true, kind: true, mode: true,
  event_types: true, filter_task_id: true, filter_agent_name: true, interval_seconds: true,
  filter_actor_type: true, filter_actor_id: true, filter_actor_name: true,
  cron_expression: true, timezone: true, next_fire_at: true, condition: true,
}).extend({ active_count: z.number().int().positive(), event_count: z.number().int().nonnegative() });

export const WorkspaceWakeupPageSchema = z.object({
  items: z.array(IssueWakeupSchema.omit({ instruction: true }).extend({
    issue_title: z.string(), issue_identifier: z.string(), issue_closed: z.boolean(),
    can_manage: z.boolean(), active_runs: z.number().int().nonnegative(),
    task: AgentTaskSchema.nullable(),
    // System rule rows have no target when the issue has no agent assignee,
    // and no revision.
    agent_id: z.string().nullish().transform((v) => v ?? ""),
    revision: z.number().int().positive().nullish().catch(undefined).transform((v) => v ?? undefined),
    source: z.enum(["member", "agent", "system"]).catch("member").default("member"),
    runs_7d: z.number().int().nonnegative().default(0),
    rule: z.literal("child_done").nullish().catch(null),
    system_stage: z.number().int().nullish(), system_remaining: z.number().int().nullish(),
    target_type: z.string().nullish(),
  })),
  total: z.number().int().nonnegative(),
  counts: z.object({
    active: z.number().int().nonnegative(), all: z.number().int().nonnegative(),
    paused: z.number().int().nonnegative().default(0),
    disabled: z.number().int().nonnegative(), ended: z.number().int().nonnegative(),
  }),
  agents: z.array(z.object({ id: z.string(), name: z.string() })),
});

// Older servers omit runtime_type; the protocol remains their compatibility target.
export const RuntimeProfileSchema = z
  .object({
    id: z.string(),
    workspace_id: z.string(),
    daemon_id: z.string().nullable().catch(null),
    display_name: z.string(),
    protocol_family: z.string(),
    runtime_type: z.string().nullish().catch(undefined),
    command_name: z.string(),
    description: z.string().nullable().catch(null),
    fixed_args: z.array(z.string()).catch([]),
    visibility: z.string().catch("workspace"),
    created_by: z.string().nullable().catch(null),
    enabled: z.boolean().catch(true),
    created_at: z.string().catch(""),
    updated_at: z.string().catch(""),
  })
  .passthrough()
  .transform((profile) => ({
    ...profile,
    runtime_type: profile.runtime_type || profile.protocol_family,
  }));
export const RuntimeProfileListSchema = z.array(RuntimeProfileSchema);

// Receipt evidence is optional across mixed server/desktop versions. A malformed
// timestamp must never render the binding as verified.
export const LarkInstallationSchema = z.object({
  id: z.string(),
  workspace_id: z.string().default(""),
  agent_id: z.string().default(""),
  app_id: z.string().default(""),
  bot_open_id: z.string().default(""),
  installer_user_id: z.string().default(""),
  tenant_key: z.string().nullable().optional(),
  status: z.string().default("revoked"),
  region: z.string().optional(),
  installed_at: z.string().default(""),
  created_at: z.string().default(""),
  updated_at: z.string().default(""),
  first_message_received_at: z.iso.datetime({ offset: true }).nullable().optional().catch(undefined),
}).loose();

export const ListLarkInstallationsResponseSchema = z.object({
  installations: z.array(LarkInstallationSchema).default([]),
  configured: z.boolean().default(false),
  install_supported: z.boolean().optional(),
}).loose();

export const EMPTY_LIST_LARK_INSTALLATIONS_RESPONSE: ListLarkInstallationsResponse = {
  installations: [],
  configured: false,
};

// Connected Apps (MUL-8516). Enum fields the UI indexes into label tables
// fall back to their most cautious value, so a value this client does not
// know yet renders as something safe instead of a missing label; statuses
// and outcomes stay strings because their renderers already handle unknowns.

const appNullableString = z.string().nullish().transform((v) => v ?? null);
const AppAccountScopeSchema = z.enum(["team", "personal"]).catch("personal");
const AppAccessLevelSchema = z.enum(["read", "confirm_writes", "write"]).catch("confirm_writes");
const AppActionClassSchema = z.enum(["read", "write", "send", "delete", "share"]).catch("send");
const AppActionPolicySchema = z.enum(["allow", "confirm", "block"]).catch("confirm");
const AppCatalogModeSchema = z.enum(["all", "curated", "custom"]).catch("curated");

const AppConnectionSchema = z.object({
  id: z.string(),
  toolkit_slug: z.string(),
  status: z.string().default("active"),
  connected_by: appNullableString,
  connected_at: z.string().default(""),
  last_used_at: appNullableString,
  actions_blocked: z.boolean().default(false).catch(false),
}).loose();

const AgentAppSchema = z.object({
  agent_id: z.string(),
  toolkit_slug: z.string(),
  account_scope: AppAccountScopeSchema,
  level: AppAccessLevelSchema,
}).loose();

export const AppsOverviewSchema = z.object({
  catalog_mode: AppCatalogModeSchema,
  allowed_toolkits: z.array(z.string()).default([]).catch([]),
  can_manage: z.boolean().default(false).catch(false),
  team: z.array(AppConnectionSchema).default([]),
  personal: z.array(AppConnectionSchema).default([]),
  agent_apps: z.array(AgentAppSchema).default([]),
  team_requests: z.array(z.object({
    toolkit_slug: z.string(),
    requested_by: z.array(z.string()).default([]).catch([]),
    asked_by: z.array(z.string()).optional().catch(undefined),
    agent_ids: z.array(z.string()).default([]).catch([]),
    last_requested_at: z.string().default(""),
  }).loose()).default([]).catch([]),
}).loose();

export const PersonalAppImpactSchema = z.object({
  agents: z.array(z.object({
    id: z.string(),
    name: z.string().default(""),
    workspace_id: z.string().default(""),
    workspace_name: z.string().default(""),
    running: z.boolean().default(false).catch(false),
  }).loose()).default([]),
}).loose();

const AppCatalogEntrySchema = z.object({
  slug: z.string(),
  name: z.string().default(""),
  logo: z.string().optional().catch(undefined),
  category: z.string().optional().catch(undefined),
  category_id: z.string().optional().catch(undefined),
  curated: z.boolean().default(false).catch(false),
  recommended_scope: AppAccountScopeSchema.optional(),
  allowed: z.boolean().optional().catch(undefined),
  connect_method: z.enum(["oauth", "key"]).optional().catch(undefined),
}).loose();

export const AppCatalogResponseSchema = z.object({
  catalog_mode: AppCatalogModeSchema,
  apps: z.array(AppCatalogEntrySchema).default([]),
}).loose();

export const AppSettingsResponseSchema = z.object({
  catalog_mode: AppCatalogModeSchema,
  allowed_toolkits: z.array(z.string()).default([]).catch([]),
}).loose();

export const AppRulesResponseSchema = z.object({
  rules: z.array(z.object({
    action_class: AppActionClassSchema,
    policy: AppActionPolicySchema,
    default_policy: AppActionPolicySchema,
    custom: z.boolean().default(false).catch(false),
  }).loose()).default([]),
}).loose();

export const AppActivityResponseSchema = z.object({
  calls: z.array(z.object({
    id: z.string(),
    agent_id: z.string(),
    issue_id: appNullableString,
    chat_session_id: appNullableString,
    triggered_by: appNullableString,
    tool_slug: z.string(),
    action_class: AppActionClassSchema,
    outcome: z.string(),
    error: appNullableString,
    created_at: z.string(),
  }).loose()).default([]),
}).loose();

export const TeamAppImpactSchema = z.object({
  agents: z.array(z.object({ id: z.string(), name: z.string().default("") }).loose()).default([]),
  active_tasks: z.number().default(0).catch(0),
  active_autopilots: z.number().default(0).catch(0),
  pending_requests: z.number().default(0).catch(0),
}).loose();

export const AgentAppsResponseSchema = z.object({
  apps: z.array(AgentAppSchema).default([]),
}).loose();

export const AppRequestSchema = z.object({
  id: z.string(),
  kind: z.enum(["action", "connect"]).catch("action"),
  status: z.string(),
  agent_id: z.string(),
  issue_id: appNullableString,
  chat_session_id: appNullableString,
  approver_user_id: z.string(),
  account_scope: AppAccountScopeSchema,
  toolkit_slug: z.string(),
  tool_slug: z.string().default(""),
  action_class: AppActionClassSchema,
  summary: z.string().default(""),
  arguments: z.record(z.string(), z.unknown()).optional().catch(undefined),
  approved_arguments: z.record(z.string(), z.unknown()).optional().catch(undefined),
  result: z.unknown().optional(),
  error: appNullableString,
  reason: appNullableString,
  // Withhold the details unless the server said otherwise.
  redacted: z.boolean().default(true).catch(true),
  adds_app: z.boolean().default(false).catch(false),
  can_decide: z.boolean().optional().catch(undefined),
  expires_at: z.string(),
  decided_by: appNullableString,
  decided_at: appNullableString,
  created_at: z.string(),
}).loose();

export const AppRequestListSchema = z.object({
  requests: z.array(AppRequestSchema).default([]),
  next_before: appNullableString,
}).loose();

export const IssueAppCallsResponseSchema = z.object({
  calls: z.array(z.object({
    id: z.string(),
    task_id: appNullableString,
    agent_id: z.string(),
    account_scope: AppAccountScopeSchema,
    account_owner_user_id: appNullableString,
    toolkit_slug: z.string(),
    tool_slug: z.string(),
    action_class: AppActionClassSchema,
    outcome: z.string(),
    request_id: appNullableString,
    error: appNullableString,
    created_at: z.string(),
  }).loose()).default([]),
}).loose();

export const AppConnectInitSchema = z.object({ redirect_url: z.string().min(1) }).loose();

export const EMPTY_APPS_OVERVIEW: AppsOverview = {
  catalog_mode: "curated",
  allowed_toolkits: [],
  can_manage: false,
  team: [],
  personal: [],
  agent_apps: [],
  team_requests: [],
};

export const EMPTY_APP_CATALOG: AppCatalogResponse = { catalog_mode: "curated", apps: [] };

export const EMPTY_TEAM_APP_IMPACT: TeamAppImpact = {
  agents: [],
  active_tasks: 0,
  active_autopilots: 0,
  pending_requests: 0,
};

// Only `pinned_at` is new here; every other field keeps its pre-schema
// leniency so one odd workspace can never empty the whole switcher.
export const WorkspaceListItemSchema = z.object({
  id: z.string(),
  name: z.string(),
  slug: z.string(),
  created_at: z.string(),
  pinned_at: z.iso.datetime({ offset: true }).nullable().optional().catch(null).default(null),
}).loose();

export const WorkspacePinnedSchema = z.object({
  pinned_at: z.iso.datetime({ offset: true }).nullable().catch(null),
});