/**
 * @fileoverview Issue table schema for Rudder's durable work surface,
 * ownership fields, review gates, execution locks, and origin traceability.
 *
 * @see doc/product/domains/issues/state-machines.md - ISSUE.STATE.001 status contract
 * @see doc/product/domains/issues/hierarchy-and-comments.md - hierarchy and comment-driven reopen behavior
 * @see doc/product/domains/work-routing/atomic-checkout.md - checkout and execution lock semantics
 */
import { sql } from "drizzle-orm";
import { index, integer, jsonb, pgTable, text, timestamp, uniqueIndex, uuid, } from "drizzle-orm/pg-core";
import { agents } from "./agents.js";
import { executionWorkspaces } from "./execution_workspaces.js";
import { goals } from "./goals.js";
import { heartbeatRuns } from "./heartbeat_runs.js";
import { organizations } from "./organizations.js";
import { projectWorkspaces } from "./project_workspaces.js";
import { projects } from "./projects.js";
export const issues = pgTable("issues", {
    id: uuid("id").primaryKey().defaultRandom(),
    orgId: uuid("org_id").notNull().references(() => organizations.id),
    projectId: uuid("project_id").references(() => projects.id),
    projectWorkspaceId: uuid("project_workspace_id").references(() => projectWorkspaces.id, { onDelete: "set null" }),
    goalId: uuid("goal_id").references(() => goals.id),
    parentId: uuid("parent_id").references(() => issues.id),
    title: text("title").notNull(),
    description: text("description"),
    status: text("status").notNull().default("backlog"),
    priority: text("priority").notNull().default("medium"),
    boardOrder: integer("board_order").notNull().default(0),
    assigneeAgentId: uuid("assignee_agent_id").references(() => agents.id),
    assigneeUserId: text("assignee_user_id"),
    reviewerAgentId: uuid("reviewer_agent_id").references(() => agents.id),
    reviewerUserId: text("reviewer_user_id"),
    checkoutRunId: uuid("checkout_run_id").references(() => heartbeatRuns.id, { onDelete: "set null" }),
    executionRunId: uuid("execution_run_id").references(() => heartbeatRuns.id, { onDelete: "set null" }),
    executionAgentNameKey: text("execution_agent_name_key"),
    executionLockedAt: timestamp("execution_locked_at", { withTimezone: true }),
    createdByAgentId: uuid("created_by_agent_id").references(() => agents.id),
    createdByUserId: text("created_by_user_id"),
    issueNumber: integer("issue_number"),
    identifier: text("identifier"),
    originKind: text("origin_kind").notNull().default("manual"),
    originId: text("origin_id"),
    originRunId: text("origin_run_id"),
    requestDepth: integer("request_depth").notNull().default(0),
    billingCode: text("billing_code"),
    assigneeAgentRuntimeOverrides: jsonb("assignee_agent_runtime_overrides").$type(),
    executionWorkspaceId: uuid("execution_workspace_id")
        .references(() => executionWorkspaces.id, { onDelete: "set null" }),
    executionWorkspacePreference: text("execution_workspace_preference"),
    executionWorkspaceSettings: jsonb("execution_workspace_settings").$type(),
    startedAt: timestamp("started_at", { withTimezone: true }),
    completedAt: timestamp("completed_at", { withTimezone: true }),
    cancelledAt: timestamp("cancelled_at", { withTimezone: true }),
    hiddenAt: timestamp("hidden_at", { withTimezone: true }),
    createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
    updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
}, (table) => ({
    companyStatusIdx: index("issues_company_status_idx").on(table.orgId, table.status),
    companyStatusBoardOrderIdx: index("issues_company_status_board_order_idx").on(table.orgId, table.status, table.boardOrder),
    assigneeStatusIdx: index("issues_company_assignee_status_idx").on(table.orgId, table.assigneeAgentId, table.status),
    assigneeUserStatusIdx: index("issues_company_assignee_user_status_idx").on(table.orgId, table.assigneeUserId, table.status),
    reviewerAgentStatusIdx: index("issues_company_reviewer_agent_status_idx").on(table.orgId, table.reviewerAgentId, table.status),
    reviewerUserStatusIdx: index("issues_company_reviewer_user_status_idx").on(table.orgId, table.reviewerUserId, table.status),
    createdByUserUpdatedIdx: index("issues_company_created_by_user_updated_idx").on(table.orgId, table.createdByUserId, table.updatedAt, table.id),
    parentIdx: index("issues_company_parent_idx").on(table.orgId, table.parentId),
    projectIdx: index("issues_company_project_idx").on(table.orgId, table.projectId),
    originIdx: index("issues_company_origin_idx").on(table.orgId, table.originKind, table.originId),
    projectWorkspaceIdx: index("issues_company_project_workspace_idx").on(table.orgId, table.projectWorkspaceId),
    executionWorkspaceIdx: index("issues_company_execution_workspace_idx").on(table.orgId, table.executionWorkspaceId),
    identifierIdx: uniqueIndex("issues_identifier_idx").on(table.identifier),
    openAutomationExecutionIdx: uniqueIndex("issues_open_automation_execution_uq")
        .on(table.orgId, table.originKind, table.originId, table.originRunId)
        .where(sql `${table.originKind} = 'automation_execution'
          and ${table.originId} is not null
          and ${table.originRunId} is not null
          and ${table.hiddenAt} is null
          and ${table.executionRunId} is not null
          and ${table.status} in ('backlog', 'todo', 'in_progress', 'in_review', 'blocked')`),
}));
//# sourceMappingURL=issues.js.map