import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
  OneToMany,
} from 'typeorm';
import { Company } from '../../auth/entities/company.entity.js';
import { Journal } from './journal.entity.js';
import { JournalEntry } from './journal-entry.entity.js';
import { InvoiceLine } from './invoice-line.entity.js';

export enum InvoiceType {
  CUSTOMER = 'customer',
  SUPPLIER = 'supplier',
}

export enum InvoiceStatus {
  DRAFT = 'draft',
  OPEN = 'open',
  PAID = 'paid',
  CANCELLED = 'cancelled',
}

@Entity({ schema: 'financial', name: 'invoices' })
@Index('idx_invoices_tenant_id', ['tenantId'])
@Index('idx_invoices_company_id', ['companyId'])
@Index('idx_invoices_partner_id', ['partnerId'])
@Index('idx_invoices_number', ['number'])
@Index('idx_invoices_date', ['invoiceDate'])
@Index('idx_invoices_status', ['status'])
@Index('idx_invoices_type', ['invoiceType'])
export class Invoice {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

  @Column({
    type: 'enum',
    enum: InvoiceType,
    nullable: false,
    name: 'invoice_type',
  })
  invoiceType: InvoiceType;

  @Column({ type: 'varchar', length: 100, nullable: true })
  number: string | null;

  @Column({ type: 'varchar', length: 255, nullable: true })
  ref: string | null;

  @Column({ type: 'date', nullable: false, name: 'invoice_date' })
  invoiceDate: Date;

  @Column({ type: 'date', nullable: true, name: 'due_date' })
  dueDate: Date | null;

  @Column({ type: 'uuid', nullable: false, name: 'currency_id' })
  currencyId: 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: 'decimal', precision: 15, scale: 2, default: 0, nullable: false, name: 'amount_paid' })
  amountPaid: number;

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

  @Column({
    type: 'enum',
    enum: InvoiceStatus,
    default: InvoiceStatus.DRAFT,
    nullable: false,
  })
  status: InvoiceStatus;

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

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

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

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

  // Relations
  @ManyToOne(() => Company)
  @JoinColumn({ name: 'company_id' })
  company: Company;

  @ManyToOne(() => Journal)
  @JoinColumn({ name: 'journal_id' })
  journal: Journal | null;

  @ManyToOne(() => JournalEntry)
  @JoinColumn({ name: 'journal_entry_id' })
  journalEntry: JournalEntry | null;

  @OneToMany(() => InvoiceLine, (line) => line.invoice, { cascade: true })
  lines: InvoiceLine[];

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

  @Column({ type: 'timestamp', nullable: true, name: 'validated_at' })
  validatedAt: Date | null;

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

  @Column({ type: 'timestamp', nullable: true, name: 'cancelled_at' })
  cancelledAt: Date | null;

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