import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Invoice } from './invoice.entity.js';
import { Account } from './account.entity.js';

@Entity({ schema: 'financial', name: 'invoice_lines' })
@Index('idx_invoice_lines_invoice_id', ['invoiceId'])
@Index('idx_invoice_lines_tenant_id', ['tenantId'])
@Index('idx_invoice_lines_product_id', ['productId'])
export class InvoiceLine {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

  @Column({ type: 'decimal', precision: 15, scale: 4, nullable: false })
  quantity: number;

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

  @Column({ type: 'decimal', precision: 15, scale: 2, nullable: false, name: 'price_unit' })
  priceUnit: number;

  @Column({ type: 'uuid', array: true, default: '{}', name: 'tax_ids' })
  taxIds: string[];

  @Column({ type: 'decimal', precision: 15, scale: 2, default: 0, nullable: false, name: 'amount_untaxed' })
  amountUntaxed: number;

  @Column({ type: 'decimal', precision: 15, scale: 2, default: 0, nullable: false, name: 'amount_tax' })
  amountTax: number;

  @Column({ type: 'decimal', precision: 15, scale: 2, default: 0, nullable: false, name: 'amount_total' })
  amountTotal: number;

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

  // Relations
  @ManyToOne(() => Invoice, (invoice) => invoice.lines, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'invoice_id' })
  invoice: Invoice;

  @ManyToOne(() => Account)
  @JoinColumn({ name: 'account_id' })
  account: Account | null;

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

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