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

export type InvoiceItemType = 'subscription' | 'user' | 'profile' | 'overage' | 'addon';

@Entity({ name: 'invoice_items', schema: 'billing' })
export class InvoiceItem {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  // Descripcion
  @Column({ type: 'varchar', length: 500 })
  description: string;

  @Index()
  @Column({ name: 'item_type', type: 'varchar', length: 30 })
  itemType: InvoiceItemType;

  // Cantidades
  @Column({ type: 'integer', default: 1 })
  quantity: number;

  @Column({ name: 'unit_price', type: 'decimal', precision: 12, scale: 2 })
  unitPrice: number;

  @Column({ type: 'decimal', precision: 12, scale: 2 })
  subtotal: number;

  // Detalles adicionales
  @Column({ name: 'profile_code', type: 'varchar', length: 10, nullable: true })
  profileCode: string;

  @Column({ type: 'varchar', length: 20, nullable: true })
  platform: string;

  @Column({ name: 'period_start', type: 'date', nullable: true })
  periodStart: Date;

  @Column({ name: 'period_end', type: 'date', nullable: true })
  periodEnd: Date;

  // Metadata
  @Column({ type: 'jsonb', default: {} })
  metadata: Record<string, any>;

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

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