/**
 * ChartOfAccounts Entity - Catálogo de Cuentas Contables
 *
 * Plan de cuentas configurable por proyecto/empresa.
 *
 * @module Finance
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  Tree,
  TreeChildren,
  TreeParent,
} from 'typeorm';

export type AccountType = 'asset' | 'liability' | 'equity' | 'income' | 'expense';
export type AccountNature = 'debit' | 'credit';
export type AccountStatus = 'active' | 'inactive' | 'blocked';

@Entity('chart_of_accounts', { schema: 'finance' })
@Tree('closure-table')
@Index(['tenantId', 'code'], { unique: true })
@Index(['tenantId', 'accountType'])
export class ChartOfAccounts {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

  // Código jerárquico de cuenta
  @Column({ length: 50 })
  code!: string;

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

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

  // Tipo y naturaleza
  @Column({
    name: 'account_type',
    type: 'enum',
    enum: ['asset', 'liability', 'equity', 'income', 'expense'],
    enumName: 'account_type',
  })
  accountType!: AccountType;

  @Column({
    type: 'enum',
    enum: ['debit', 'credit'],
    enumName: 'account_nature',
  })
  nature!: AccountNature;

  @Column({
    type: 'enum',
    enum: ['active', 'inactive', 'blocked'],
    enumName: 'account_status',
    default: 'active',
  })
  status!: AccountStatus;

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

  @TreeParent()
  parent?: ChartOfAccounts;

  @TreeChildren()
  children?: ChartOfAccounts[];

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

  // Configuración de imputación
  @Column({ name: 'cost_center_required', type: 'boolean', default: false })
  costCenterRequired!: boolean;

  @Column({ name: 'project_required', type: 'boolean', default: false })
  projectRequired!: boolean;

  @Column({ name: 'allows_direct_posting', type: 'boolean', default: true })
  allowsDirectPosting!: boolean;

  // Códigos de integración con ERPs externos
  @Column({ name: 'sap_code', length: 50, nullable: true })
  sapCode?: string;

  @Column({ name: 'contpaqi_code', length: 50, nullable: true })
  contpaqiCode?: string;

  @Column({ name: 'aspel_code', length: 50, nullable: true })
  aspelCode?: string;

  // Saldos (actualizados periódicamente)
  @Column({
    name: 'initial_balance',
    type: 'decimal',
    precision: 18,
    scale: 2,
    default: 0,
  })
  initialBalance!: number;

  @Column({
    name: 'current_balance',
    type: 'decimal',
    precision: 18,
    scale: 2,
    default: 0,
  })
  currentBalance!: number;

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

  // Notas y metadatos
  @Column({ type: 'text', nullable: true })
  notes?: string;

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

  // Auditoría
  @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;
}
