/**
 * BankAccount Entity - Cuentas Bancarias
 *
 * Registro de cuentas bancarias de la empresa.
 *
 * @module Finance
 */

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

export type BankAccountType = 'checking' | 'savings' | 'investment' | 'credit_line' | 'other';
export type BankAccountStatus = 'active' | 'inactive' | 'blocked' | 'closed';

@Entity('bank_accounts', { schema: 'finance' })
@Index(['tenantId', 'accountNumber'], { unique: true })
@Index(['tenantId', 'status'])
export class BankAccount {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

  // Información de la cuenta
  @Column({ length: 100 })
  name!: string;

  @Column({ name: 'account_number', length: 50 })
  accountNumber!: string;

  @Column({ length: 18, nullable: true })
  clabe?: string;

  @Column({
    name: 'account_type',
    type: 'enum',
    enum: ['checking', 'savings', 'investment', 'credit_line', 'other'],
    enumName: 'bank_account_type',
    default: 'checking',
  })
  accountType!: BankAccountType;

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

  // Banco
  @Column({ name: 'bank_name', length: 100 })
  bankName!: string;

  @Column({ name: 'bank_code', length: 10, nullable: true })
  bankCode?: string;

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

  @Column({ name: 'branch_code', length: 20, nullable: true })
  branchCode?: string;

  // Moneda
  @Column({ length: 3, default: 'MXN' })
  currency!: string;

  // Saldos
  @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: 'available_balance',
    type: 'decimal',
    precision: 18,
    scale: 2,
    default: 0,
  })
  availableBalance!: number;

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

  // Límites (para líneas de crédito)
  @Column({
    name: 'credit_limit',
    type: 'decimal',
    precision: 18,
    scale: 2,
    nullable: true,
  })
  creditLimit?: number;

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

  // Proyecto asociado (si es cuenta específica de proyecto)
  @Column({ name: 'project_id', type: 'uuid', nullable: true })
  projectId?: string;

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

  // Cuenta contable vinculada
  @Column({ name: 'ledger_account_id', type: 'uuid', nullable: true })
  ledgerAccountId?: string;

  // Contacto del banco
  @Column({ name: 'bank_contact_name', length: 255, nullable: true })
  bankContactName?: string;

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

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

  // Conciliación
  @Column({ name: 'last_reconciliation_date', type: 'date', nullable: true })
  lastReconciliationDate?: Date;

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

  // Acceso banca en línea
  @Column({ name: 'online_banking_user', length: 100, nullable: true })
  onlineBankingUser?: string;

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

  // Flags
  @Column({ name: 'is_default', type: 'boolean', default: false })
  isDefault!: boolean;

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

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

  // 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;
}
