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

/**
 * Tipo de cálculo para la línea del término de pago
 */
export enum PaymentTermLineType {
  BALANCE = 'balance',      // Saldo restante
  PERCENT = 'percent',      // Porcentaje del total
  FIXED = 'fixed',          // Monto fijo
}

/**
 * Línea de término de pago (para términos con múltiples vencimientos)
 */
@Entity({ schema: 'core', name: 'payment_term_lines' })
@Index('idx_payment_term_lines_term', ['paymentTermId'])
export class PaymentTermLine {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'uuid', nullable: false, name: 'payment_term_id' })
  paymentTermId: string;

  @Column({ type: 'integer', nullable: false, default: 1 })
  sequence: number;

  @Column({
    type: 'enum',
    enum: PaymentTermLineType,
    default: PaymentTermLineType.BALANCE,
    name: 'line_type',
  })
  lineType: PaymentTermLineType;

  @Column({
    type: 'decimal',
    precision: 5,
    scale: 2,
    nullable: true,
    name: 'value_percent',
  })
  valuePercent: number | null;

  @Column({
    type: 'decimal',
    precision: 15,
    scale: 2,
    nullable: true,
    name: 'value_amount',
  })
  valueAmount: number | null;

  @Column({ type: 'integer', nullable: false, default: 0 })
  days: number;

  @Column({ type: 'integer', nullable: true, name: 'day_of_month' })
  dayOfMonth: number | null;

  @Column({ type: 'boolean', nullable: false, default: false, name: 'end_of_month' })
  endOfMonth: boolean;
}

/**
 * Término de pago (Net 30, 50% advance + 50% on delivery, etc.)
 */
@Entity({ schema: 'core', name: 'payment_terms' })
@Index('idx_payment_terms_tenant_id', ['tenantId'])
@Index('idx_payment_terms_code_tenant', ['tenantId', 'code'], { unique: true })
@Index('idx_payment_terms_active', ['tenantId', 'isActive'])
export class PaymentTerm {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ type: 'uuid', nullable: true, name: 'company_id' })
  companyId: string | null;

  @Column({ type: 'varchar', length: 50, nullable: false })
  code: string;

  @Column({ type: 'varchar', length: 255, nullable: false })
  name: string;

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

  @Column({ type: 'integer', nullable: false, default: 0, name: 'due_days' })
  dueDays: number;

  @Column({
    type: 'decimal',
    precision: 5,
    scale: 2,
    nullable: true,
    default: 0,
    name: 'discount_percent',
  })
  discountPercent: number | null;

  @Column({ type: 'integer', nullable: true, default: 0, name: 'discount_days' })
  discountDays: number | null;

  @Column({ type: 'boolean', nullable: false, default: false, name: 'is_immediate' })
  isImmediate: boolean;

  @Column({ type: 'boolean', nullable: false, default: true, name: 'is_active' })
  isActive: boolean;

  @Column({ type: 'integer', nullable: false, default: 0 })
  sequence: number;

  @OneToMany(() => PaymentTermLine, (line) => line.paymentTermId, { eager: true })
  lines: PaymentTermLine[];

  // Audit fields
  @CreateDateColumn({ name: 'created_at', type: 'timestamp' })
  createdAt: Date;

  @Column({ type: 'uuid', nullable: true, name: 'created_by' })
  createdBy: string | null;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp', nullable: true })
  updatedAt: Date | null;

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

  @DeleteDateColumn({ name: 'deleted_at', type: 'timestamptz', nullable: true })
  deletedAt: Date | null;

  @Column({ type: 'uuid', nullable: true, name: 'deleted_by' })
  deletedBy: string | null;
}
