// User & Auth Types
export type UserRole = 'ADMIN' | 'GM' | 'PLAYER';

export interface User {
  id: string;
  username: string;
  email: string;
  role: UserRole;
  avatarUrl?: string;
  createdAt?: string;
  updatedAt?: string;
}

export interface AuthResponse {
  user: User;
  token: string;
}

// Campaign Types
export interface Campaign {
  id: string;
  name: string;
  description?: string;
  gmId: string;
  imageUrl?: string;
  createdAt: string;
  updatedAt: string;
  gm: Pick<User, 'id' | 'username' | 'avatarUrl'>;
  members: CampaignMember[];
  characters?: CharacterSummary[];
  _count?: {
    characters: number;
    battleSessions?: number;
    documents?: number;
  };
}

export interface CampaignMember {
  campaignId: string;
  userId: string;
  joinedAt: string;
  user: Pick<User, 'id' | 'username' | 'email' | 'avatarUrl'>;
}

// Character Types
export type CharacterType = 'PC' | 'NPC';
export type AbilityType = 'STR' | 'DEX' | 'CON' | 'INT' | 'WIS' | 'CHA';
export type Proficiency = 'UNTRAINED' | 'TRAINED' | 'EXPERT' | 'MASTER' | 'LEGENDARY';

export interface CharacterSummary {
  id: string;
  name: string;
  type: CharacterType;
  level: number;
  avatarUrl?: string;
  hpCurrent: number;
  hpMax: number;
  owner?: Pick<User, 'id' | 'username'>;
}

export interface Character extends CharacterSummary {
  campaignId: string;
  ownerId?: string;
  hpTemp: number;
  ancestryId?: string;
  heritageId?: string;
  classId?: string;
  backgroundId?: string;
  experiencePoints: number;
  credits: number; // Ironvale currency (1 Gold = 100, 1 Silver = 10, 1 Copper = 1)
  pathbuilderData?: unknown;
  createdAt: string;
  updatedAt: string;
  abilities: CharacterAbility[];
  feats: CharacterFeat[];
  skills: CharacterSkill[];
  spells: CharacterSpell[];
  items: CharacterItem[];
  conditions: CharacterCondition[];
  resources: CharacterResource[];
  // Alchemy
  alchemyState?: CharacterAlchemyState;
  formulas?: CharacterFormula[];
  preparedItems?: CharacterPreparedItem[];
}

export interface CharacterAbility {
  id: string;
  characterId: string;
  ability: AbilityType;
  score: number;
}

export interface CharacterFeat {
  id: string;
  characterId: string;
  featId?: string;
  name: string;
  nameGerman?: string;
  level: number;
  source: 'CLASS' | 'ANCESTRY' | 'GENERAL' | 'SKILL' | 'BONUS' | 'ARCHETYPE';
}

export interface CharacterSkill {
  id: string;
  characterId: string;
  skillName: string;
  proficiency: Proficiency;
}

export interface CharacterSpell {
  id: string;
  characterId: string;
  spellId?: string;
  name: string;
  nameGerman?: string;
  tradition: 'ARCANE' | 'DIVINE' | 'OCCULT' | 'PRIMAL';
  spellLevel: number;
  prepared: boolean;
}

export interface CharacterItem {
  id: string;
  characterId: string;
  equipmentId?: string;
  name: string;
  nameGerman?: string;
  quantity: number;
  bulk: number;
  equipped: boolean;
  invested: boolean;
  containerId?: string;
  notes?: string;
  // Player-editable alias
  alias?: string;
  // GM-editable custom overrides
  customName?: string;
  customDamage?: string;
  customDamageType?: string;
  customTraits?: string[];
  customRange?: string;
  customHands?: string;
  // Equipment-Details (geladen via Relation)
  equipment?: Equipment;
}

export interface CharacterCondition {
  id: string;
  characterId: string;
  name: string;
  nameGerman?: string;
  value?: number;
  duration?: string;
  source?: string;
}

export interface CharacterResource {
  id: string;
  characterId: string;
  name: string;
  current: number;
  max: number;
}

// Alchemy Types
export type ResearchField = 'BOMBER' | 'CHIRURGEON' | 'MUTAGENIST' | 'TOXICOLOGIST';

export interface CharacterAlchemyState {
  id: string;
  characterId: string;
  researchField?: ResearchField;
  versatileVialsCurrent: number;
  versatileVialsMax: number;
  advancedAlchemyBatch: number;
  advancedAlchemyMax: number;
  lastRestAt?: string;
}

export interface CharacterFormula {
  id: string;
  characterId: string;
  equipmentId: string;
  name: string;
  nameGerman?: string;
  learnedAt: number;
  formulaSource?: string;
  equipment?: Equipment;
}

export interface CharacterPreparedItem {
  id: string;
  characterId: string;
  equipmentId: string;
  name: string;
  nameGerman?: string;
  quantity: number;
  isQuickAlchemy: boolean;
  isInfused: boolean;
  createdAt: string;
  equipment?: Equipment;
}

// Available formula - equipment with isLearned flag for auto-upgrade system
export interface AvailableFormula extends Equipment {
  nameGerman?: string;
  summaryGerman?: string;
  effectGerman?: string;
  isLearned: boolean; // true if this exact version was learned, false if it's an upgraded version
}

// Rest Types
export interface ConditionReduced {
  name: string;
  oldValue: number;
  newValue: number;
}

export interface RestPreview {
  hpToHeal: number;
  hpAfterRest: number;
  conditionsToRemove: string[];
  conditionsToReduce: ConditionReduced[];
  resourcesToReset: string[];
  alchemyReset: boolean;
  infusedItemsCount: number;
}

export interface RestResult {
  hpHealed: number;
  hpCurrent: number;
  conditionsRemoved: string[];
  conditionsReduced: ConditionReduced[];
  resourcesReset: string[];
  alchemyReset: boolean;
}

// Battle Types
export interface BattleMap {
  id: string;
  campaignId: string;
  name: string;
  imageUrl: string;
  gridSizeX: number;
  gridSizeY: number;
  gridOffsetX: number;
  gridOffsetY: number;
  createdAt: string;
}

export interface Combatant {
  id: string;
  campaignId: string;
  name: string;
  type: 'PC' | 'NPC' | 'MONSTER';
  level: number;
  hpMax: number;
  ac: number;
  fortitude: number;
  reflex: number;
  will: number;
  perception: number;
  speed: number;
  avatarUrl?: string;
  description?: string;
  createdAt: string;
  abilities: CombatantAbility[];
}

export interface CombatantAbility {
  id: string;
  combatantId: string;
  name: string;
  actionCost: number;
  actionType: 'ACTION' | 'REACTION' | 'FREE';
  description: string;
  damage?: string;
  traits: string[];
}

export interface BattleSession {
  id: string;
  campaignId: string;
  mapId?: string;
  name?: string;
  isActive: boolean;
  roundNumber: number;
  createdAt: string;
  map?: BattleMap;
  tokens: BattleToken[];
}

export interface BattleToken {
  id: string;
  battleSessionId: string;
  combatantId?: string;
  characterId?: string;
  name: string;
  positionX: number;
  positionY: number;
  hpCurrent: number;
  hpMax: number;
  initiative?: number;
  conditions: string[];
  size: number;
}

// Document Types
export interface Document {
  id: string;
  campaignId: string;
  title: string;
  description?: string;
  category?: string;
  tags: string[];
  filePath: string;
  fileType: string;
  uploadedBy: string;
  createdAt: string;
}

// Equipment Database Types
export interface Equipment {
  id: string;
  name: string;
  traits: string[];
  itemCategory: string;
  itemSubcategory?: string;
  bulk?: string;
  url?: string;
  summary?: string;
  level?: number;
  price?: number;
  // Translated fields (from Translation cache)
  nameGerman?: string;
  summaryGerman?: string;
  // Weapon fields
  hands?: string;
  damage?: string;
  damageType?: string;
  range?: string;
  reload?: string;
  weaponCategory?: string;
  weaponGroup?: string;
  // Armor fields
  ac?: number;
  dexCap?: number;
  checkPenalty?: number;
  speedPenalty?: number;
  strength?: number;
  armorCategory?: string;
  armorGroup?: string;
  // Shield fields
  shieldHp?: number;
  shieldHardness?: number;
  shieldBt?: number;
  // Consumable fields
  activation?: string;
  duration?: string;
  usage?: string;
  effect?: string; // Specific effect text for item variants (Lesser, Moderate, Greater, Major)
  effectGerman?: string; // Translated effect text
}

export interface EquipmentSearchResult {
  items: Equipment[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
  categories: string[];
}

// Feat Database Types
export interface Feat {
  id: string;
  name: string;
  traits: string[];
  summary?: string;
  description?: string;
  actions?: string; // "1", "2", "3", "free", "reaction", null for passive
  url?: string;
  level?: number;
  sourceBook?: string;
  // Feat classification
  featType?: string; // "General", "Skill", "Class", "Ancestry", "Archetype", "Heritage"
  rarity?: string; // "Common", "Uncommon", "Rare", "Unique"
  // Prerequisites
  prerequisites?: string;
  // For class/archetype feats
  className?: string;
  archetypeName?: string;
  // For ancestry feats
  ancestryName?: string;
  // For skill feats
  skillName?: string;
  // Cached German translation
  nameGerman?: string;
  summaryGerman?: string;
}

export interface FeatSearchResult {
  items: Feat[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
}

// API Response Types
export interface ApiError {
  statusCode: number;
  message: string;
  error?: string;
}
