import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
  CreateDateColumn,
  UpdateDateColumn,
  Unique,
} from 'typeorm';
import { User } from '../../user/user.entity';

@Entity('characters')
@Unique(['userId'])
export class Character {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'user_id' })
  userId: string;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'user_id' })
  user: User;

  @Column({ length: 100 })
  name: string;

  @Column({ default: 1 })
  level: number;

  @Column({ default: 0 })
  xp: number;

  @Column({ default: 0 })
  gold: number;

  // Stats (cap : 101)
  @Column({ default: 1 })
  force: number;

  @Column({ default: 1 })
  agilite: number;

  @Column({ default: 1 })
  intelligence: number;

  @Column({ default: 1 })
  chance: number;

  @Column({ default: 1 })
  vitalite: number;

  @Column({ name: 'hp_current', default: 100 })
  hpCurrent: number;

  @Column({ name: 'hp_max', default: 100 })
  hpMax: number;

  // Endurance — lazy calculation (pas de timer actif)
  @Column({ name: 'endurance_saved', default: 100 })
  enduranceSaved: number;

  @Column({ name: 'last_endurance_ts', type: 'timestamp', default: () => 'NOW()' })
  lastEnduranceTs: Date;

  @Column({ name: 'endurance_max', default: 100 })
  enduranceMax: number;

  // Points de stats disponibles (non distribués) — +5 par level up
  @Column({ name: 'stat_points', default: 0 })
  statPoints: number;

  // Sprint 4 — Profil enrichi
  @Column({ type: 'varchar', name: 'active_title', length: 100, nullable: true })
  activeTitle: string | null;

  @Column({ name: 'total_gold_earned', type: 'bigint', default: 0 })
  totalGoldEarned: number;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date;
}
