/**
 * AccountingEntryLine Entity - Líneas de Póliza Contable
 *
 * Detalle de movimientos (debe/haber) de una póliza.
 *
 * @module Finance
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { AccountingEntry } from './accounting-entry.entity';
import { ChartOfAccounts } from './chart-of-accounts.entity';

@Entity('accounting_entry_lines', { schema: 'finance' })
@Index(['tenantId', 'entryId'])
@Index(['tenantId', 'accountId'])
export class AccountingEntryLine {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

  // Referencia a la póliza
  @Column({ name: 'entry_id', type: 'uuid' })
  entryId!: string;

  @ManyToOne(() => AccountingEntry, (entry) => entry.lines, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'entry_id' })
  entry?: AccountingEntry;

  // Número de línea
  @Column({ name: 'line_number', type: 'int' })
  lineNumber!: number;

  // Cuenta contable
  @Column({ name: 'account_id', type: 'uuid' })
  accountId!: string;

  @ManyToOne(() => ChartOfAccounts)
  @JoinColumn({ name: 'account_id' })
  account?: ChartOfAccounts;

  @Column({ name: 'account_code', length: 50 })
  accountCode!: string;

  // Descripción de la línea
  @Column({ type: 'text' })
  description!: string;

  // Montos
  @Column({
    type: 'decimal',
    precision: 18,
    scale: 2,
    default: 0,
  })
  debit!: number;

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

  // Centro de costo (opcional)
  @Column({ name: 'cost_center_id', type: 'uuid', nullable: true })
  costCenterId?: string;

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

  // Proyecto (opcional)
  @Column({ name: 'project_id', type: 'uuid', nullable: true })
  projectId?: string;

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

  // Tercero (proveedor/cliente)
  @Column({ name: 'partner_id', type: 'uuid', nullable: true })
  partnerId?: string;

  @Column({ name: 'partner_name', length: 255, nullable: true })
  partnerName?: string;

  // Documento de referencia
  @Column({ name: 'document_type', length: 50, nullable: true })
  documentType?: string;

  @Column({ name: 'document_number', length: 100, nullable: true })
  documentNumber?: string;

  // Moneda original (si es diferente)
  @Column({ name: 'original_currency', length: 3, nullable: true })
  originalCurrency?: string;

  @Column({
    name: 'original_amount',
    type: 'decimal',
    precision: 18,
    scale: 2,
    nullable: true,
  })
  originalAmount?: number;

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

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

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

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