/**
 * AssetCategory Entity - Categorias de Activos
 *
 * Clasificacion jerarquica de activos con configuracion de depreciacion.
 *
 * @module Assets (MAE-015)
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
} from 'typeorm';

@Entity('asset_categories', { schema: 'assets' })
@Index(['tenantId', 'code'], { unique: true })
@Index(['tenantId', 'parentId'])
export class AssetCategory {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

  @Column({ name: 'tenant_id', type: 'uuid' })
  @Index()
  tenantId!: string;

  // Informacion basica
  @Column({ length: 20 })
  code!: string;

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

  @Column({ type: 'text', nullable: true })
  description?: string;

  // Jerarquia
  @Column({ name: 'parent_id', type: 'uuid', nullable: true })
  parentId?: string;

  @ManyToOne(() => AssetCategory, (cat) => cat.children, { nullable: true })
  @JoinColumn({ name: 'parent_id' })
  parent?: AssetCategory;

  @OneToMany(() => AssetCategory, (cat) => cat.parent)
  children?: AssetCategory[];

  @Column({ type: 'int', default: 1 })
  level!: number;

  // Configuracion de depreciacion
  @Column({ name: 'useful_life_years', type: 'int', nullable: true })
  usefulLifeYears?: number;

  @Column({ name: 'depreciation_method', length: 50, nullable: true })
  depreciationMethod?: string; // straight_line, declining_balance, units_of_production

  @Column({
    name: 'salvage_value_percentage',
    type: 'decimal',
    precision: 5,
    scale: 2,
    nullable: true,
  })
  salvageValuePercentage?: number;

  // Estado
  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive!: boolean;

  // Metadatos
  @Column({ type: 'jsonb', nullable: true })
  metadata?: Record<string, any>;

  // Auditoria
  @Column({ name: 'created_by', type: 'uuid', nullable: true })
  createdBy?: string;

  @Column({ name: 'updated_by', type: 'uuid', nullable: true })
  updatedBy?: string;

  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt!: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
  updatedAt!: Date;

  @Column({ name: 'deleted_at', type: 'timestamptz', nullable: true })
  deletedAt?: Date;
}
