import { sqliteTable, AnySQLiteColumn, uniqueIndex, foreignKey, text, integer } from "drizzle-orm/sqlite-core"
  import { sql } from "drizzle-orm"

export const agents = sqliteTable("agents", {
	id: text().primaryKey().notNull(),
	name: text().notNull(),
	taskId: text("task_id").references(() => tasks.id, { onDelete: "set null" } ),
	sessionId: text("session_id"),
	worktreeId: text("worktree_id").notNull(),
	status: text().default("idle").notNull(),
	mode: text().default("execute").notNull(),
	createdAt: integer("created_at").notNull(),
	updatedAt: integer("updated_at").notNull(),
},
(table) => [
	uniqueIndex("agents_name_unique").on(table.name),
]);

export const initiatives = sqliteTable("initiatives", {
	id: text().primaryKey().notNull(),
	name: text().notNull(),
	description: text(),
	status: text().default("active").notNull(),
	createdAt: integer("created_at").notNull(),
	updatedAt: integer("updated_at").notNull(),
});

export const messages = sqliteTable("messages", {
	id: text().primaryKey().notNull(),
	senderType: text("sender_type").notNull(),
	senderId: text("sender_id").references(() => agents.id, { onDelete: "set null" } ),
	recipientType: text("recipient_type").notNull(),
	recipientId: text("recipient_id").references(() => agents.id, { onDelete: "set null" } ),
	type: text().default("info").notNull(),
	content: text().notNull(),
	requiresResponse: integer("requires_response").default(false).notNull(),
	status: text().default("pending").notNull(),
	parentMessageId: text("parent_message_id"),
	createdAt: integer("created_at").notNull(),
	updatedAt: integer("updated_at").notNull(),
},
(table) => [
	foreignKey(() => ({
			columns: [table.parentMessageId],
			foreignColumns: [table.id],
			name: "messages_parent_message_id_messages_id_fk"
		})).onDelete("set null"),
]);

export const phases = sqliteTable("phases", {
	id: text().primaryKey().notNull(),
	initiativeId: text("initiative_id").notNull().references(() => initiatives.id, { onDelete: "cascade" } ),
	number: integer().notNull(),
	name: text().notNull(),
	description: text(),
	status: text().default("pending").notNull(),
	createdAt: integer("created_at").notNull(),
	updatedAt: integer("updated_at").notNull(),
});

export const plans = sqliteTable("plans", {
	id: text().primaryKey().notNull(),
	phaseId: text("phase_id").notNull().references(() => phases.id, { onDelete: "cascade" } ),
	number: integer().notNull(),
	name: text().notNull(),
	description: text(),
	status: text().default("pending").notNull(),
	createdAt: integer("created_at").notNull(),
	updatedAt: integer("updated_at").notNull(),
});

export const taskDependencies = sqliteTable("task_dependencies", {
	id: text().primaryKey().notNull(),
	taskId: text("task_id").notNull().references(() => tasks.id, { onDelete: "cascade" } ),
	dependsOnTaskId: text("depends_on_task_id").notNull().references(() => tasks.id, { onDelete: "cascade" } ),
	createdAt: integer("created_at").notNull(),
});

export const tasks = sqliteTable("tasks", {
	id: text().primaryKey().notNull(),
	planId: text("plan_id").notNull().references(() => plans.id, { onDelete: "cascade" } ),
	name: text().notNull(),
	description: text(),
	type: text().default("auto").notNull(),
	priority: text().default("medium").notNull(),
	status: text().default("pending").notNull(),
	order: integer().default(0).notNull(),
	createdAt: integer("created_at").notNull(),
	updatedAt: integer("updated_at").notNull(),
});

export const phaseDependencies = sqliteTable("phase_dependencies", {
	id: text().primaryKey().notNull(),
	phaseId: text("phase_id").notNull().references(() => phases.id, { onDelete: "cascade" } ),
	dependsOnPhaseId: text("depends_on_phase_id").notNull().references(() => phases.id, { onDelete: "cascade" } ),
	createdAt: integer("created_at").notNull(),
});

