import { sqliteTable, text, integer, uniqueIndex } from "drizzle-orm/sqlite-core";
import { projects } from "./projects";

export const characters = sqliteTable("characters", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  projectId: integer("project_id")
    .notNull()
    .references(() => projects.id),
  name: text("name").notNull(),
  slug: text("slug").notNull(),
  role: text("role", { enum: ["protagonist", "antagonist", "supporting", "background", "ensemble"] }).notNull().default("supporting"),
  bio: text("bio"),
  description: text("description"),
  arc: text("arc"),
  arcType: text("arc_type", { enum: ["positive", "negative", "flat", "complex"] }),
  age: integer("age"),
  gender: text("gender"),
  voice: text("voice"),
  traits: text("traits"),
  motivation: text("motivation"),
  conflict: text("conflict"),
  secret: text("secret"),
  imageUrl: text("image_url"),
  createdAt: integer("created_at", { mode: "timestamp" }).$defaultFn(() => new Date()),
  updatedAt: integer("updated_at", { mode: "timestamp" }).$defaultFn(() => new Date()),
});

export const characterRelationships = sqliteTable("character_relationships", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  characterIdA: integer("character_a_id")
    .notNull()
    .references(() => characters.id),
  characterIdB: integer("character_b_id")
    .notNull()
    .references(() => characters.id),
  relationshipType: text("relationship_type", {
    enum: ["family", "romantic", "friendship", "rivalry", "mentor", "alliance", "conflict", "professional", "other"],
  }).notNull(),
  description: text("description"),
  strength: integer("strength").notNull().default(50),
  isAntagonistic: integer("is_antagonistic", { mode: "boolean" }).notNull().default(false),
  createdAt: integer("created_at", { mode: "timestamp" }).$defaultFn(() => new Date()),
  updatedAt: integer("updated_at", { mode: "timestamp" }).$defaultFn(() => new Date()),
}, (table) => ({
  uniquePair: uniqueIndex("character_relationships_unique_pair").on(
    table.characterIdA,
    table.characterIdB
  ),
}));

export type Character = typeof characters.$inferSelect;
export type NewCharacter = typeof characters.$inferInsert;
export type CharacterRelationship = typeof characterRelationships.$inferSelect;
export type NewCharacterRelationship = typeof characterRelationships.$inferInsert;
