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

export const InvoiceItemType = {
  SUBSCRIPTION: 'subscription',
  OVERAGE: 'overage',
  CREDIT: 'credit',
  ADJUSTMENT: 'adjustment',
} as const;
export type InvoiceItemType = typeof InvoiceItemType[keyof typeof InvoiceItemType];

@Entity({ name: 'billing_invoice_items', schema: 'public' })
export class InvoiceItem {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

  @Column({ type: 'varchar', length: 200 })
  description!: string; // e.g. 'Pro Plan - Monthly', 'Token Overage (2.5M tokens)'

  @Column({ name: 'item_type', type: 'varchar', length: 30 })
  itemType!: InvoiceItemType; // 'subscription' | 'overage' | 'credit' | 'adjustment'

  @Column({ type: 'bigint', default: 1 })
  quantity!: number; // 1 for subscription, token count for overage

  @Column({ name: 'unit_price', type: 'int' })
  unitPrice!: number; // in cents/fen

  @Column({ type: 'int' })
  amount!: number; // quantity * unitPrice (rounded)

  @Column({ type: 'varchar', length: 3, default: 'USD' })
  currency!: string;

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