/**
 * Database entity type definitions
 * These interfaces describe the shape of database entities
 */

import type { BaseEntity } from "./index";
import type {
  TaskStatus,
  TaskPriority,
  ProjectStatus,
  WorkspaceMemberRole,
  TeamMemberRole,
  ActivityAction,
  EntityType,
  EntryStatus,
  Visibility,
  FormalityLevel,
} from "./enums";

/**
 * User entity
 */
export interface User extends BaseEntity {
  email: string;
  name: string;
  emailVerified: boolean;
  image: string | null;
  authProviderId: string | null;
  preferences: Record<string, unknown>;
  deactivatedAt: Date | null;
  isLocalAuth: boolean;
  passwordHash: string | null;
  invitedBy: string | null;
  invitationToken: string | null;
  invitedAt: Date | null;
}

/**
 * Workspace entity
 */
export interface Workspace extends BaseEntity {
  name: string;
  ownerId: string;
  settings: Record<string, unknown>;
}

/**
 * Workspace member entity (join table)
 */
export interface WorkspaceMember {
  workspaceId: string;
  userId: string;
  role: WorkspaceMemberRole;
  joinedAt: Date;
}

/**
 * Workspace member with populated user data
 */
export interface WorkspaceMemberWithUser extends WorkspaceMember {
  user: User;
}

/**
 * Team entity
 */
export interface Team extends BaseEntity {
  workspaceId: string;
  name: string;
  description: string | null;
  metadata: Record<string, unknown>;
}

/**
 * Team member entity (join table)
 */
export interface TeamMember {
  teamId: string;
  userId: string;
  role: TeamMemberRole;
  joinedAt: Date;
}

/**
 * Task entity
 */
export interface Task extends BaseEntity {
  workspaceId: string;
  title: string;
  description: string | null;
  status: TaskStatus;
  priority: TaskPriority;
  dueDate: Date | null;
  assigneeId: string | null;
  creatorId: string;
  projectId: string | null;
  parentId: string | null;
  sortOrder: number;
  metadata: Record<string, unknown>;
  completedAt: Date | null;
}

/**
 * Event entity
 */
export interface Event extends BaseEntity {
  workspaceId: string;
  title: string;
  description: string | null;
  startTime: Date;
  endTime: Date | null;
  allDay: boolean;
  location: string | null;
  recurrence: Record<string, unknown> | null;
  creatorId: string;
  projectId: string | null;
  metadata: Record<string, unknown>;
}

/**
 * Project entity
 */
export interface Project extends BaseEntity {
  workspaceId: string;
  name: string;
  description: string | null;
  status: ProjectStatus;
  startDate: Date | null;
  endDate: Date | null;
  creatorId: string;
  color: string | null;
  metadata: Record<string, unknown>;
}

/**
 * Activity log entity
 */
export interface ActivityLog {
  readonly id: string;
  workspaceId: string;
  userId: string;
  action: ActivityAction;
  entityType: EntityType;
  entityId: string;
  details: Record<string, unknown>;
  readonly createdAt: Date;
}

/**
 * Memory embedding entity
 */
export interface MemoryEmbedding extends BaseEntity {
  workspaceId: string;
  content: string;
  embedding: number[] | null;
  entityType: EntityType | null;
  entityId: string | null;
  metadata: Record<string, unknown>;
}

/**
 * Knowledge tag entity
 */
export interface KnowledgeTag extends BaseEntity {
  workspaceId: string;
  name: string;
  slug: string;
  color: string | null;
}

/**
 * Knowledge entry entity
 */
export interface KnowledgeEntry extends BaseEntity {
  workspaceId: string;
  slug: string;
  title: string;
  content: string;
  contentHtml: string | null;
  summary: string | null;
  status: EntryStatus;
  visibility: Visibility;
  createdBy: string;
  updatedBy: string;
}

/**
 * Knowledge entry with tags relation
 */
export interface KnowledgeEntryWithTags extends KnowledgeEntry {
  tags: KnowledgeTag[];
}

/**
 * Knowledge entry version entity
 */
export interface KnowledgeEntryVersion {
  readonly id: string;
  entryId: string;
  version: number;
  title: string;
  content: string;
  summary: string | null;
  readonly createdAt: Date;
  createdBy: string;
  changeNote: string | null;
}

/**
 * Knowledge entry version with author information
 */
export interface KnowledgeEntryVersionWithAuthor extends KnowledgeEntryVersion {
  author: {
    id: string;
    name: string;
    email: string;
  };
}

/**
 * Knowledge entry backlink
 * Represents an entry that links to another entry
 */
export interface KnowledgeBacklink {
  readonly id: string;
  sourceId: string;
  targetId: string;
  linkText: string;
  displayText: string;
  positionStart: number;
  positionEnd: number;
  resolved: boolean;
  context: string | null;
  readonly createdAt: Date;
  source: {
    id: string;
    title: string;
    slug: string;
    summary?: string | null;
  };
}

/**
 * Domain entity
 */
export interface Domain extends BaseEntity {
  workspaceId: string;
  name: string;
  slug: string;
  description: string | null;
  color: string | null;
  icon: string | null;
  sortOrder: number;
  metadata: Record<string, unknown>;
}

/**
 * Domain with usage counts
 */
export interface DomainWithCounts extends Domain {
  _count?: {
    tasks: number;
    projects: number;
    events: number;
    ideas: number;
  };
}

/**
 * Personality entity
 * Note: This type exists for the frontend but requires database schema migration
 */
export interface Personality extends BaseEntity {
  workspaceId: string;
  name: string;
  description: string | null;
  tone: string;
  formalityLevel: FormalityLevel;
  systemPromptTemplate: string;
  isDefault: boolean;
  isActive: boolean;
}
