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

export enum PaymentType {
  INBOUND = 'inbound',
  OUTBOUND = 'outbound',
}

export enum PaymentMethod {
  CASH = 'cash',
  BANK_TRANSFER = 'bank_transfer',
  CHECK = 'check',
  CARD = 'card',
  OTHER = 'other',
}

export enum PaymentStatus {
  DRAFT = 'draft',
  POSTED = 'posted',
  RECONCILED = 'reconciled',
  CANCELLED = 'cancelled',
}

@Entity({ schema: 'financial', name: 'payments' })
@Index('idx_payments_tenant_id', ['tenantId'])
@Index('idx_payments_company_id', ['companyId'])
@Index('idx_payments_partner_id', ['partnerId'])
@Index('idx_payments_date', ['paymentDate'])
@Index('idx_payments_status', ['status'])
@Index('idx_payments_type', ['paymentType'])
export class Payment {
  @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: PaymentType,
    nullable: false,
    name: 'payment_type',
  })
  paymentType: PaymentType;

  @Column({
    type: 'enum',
    enum: PaymentMethod,
    nullable: false,
    name: 'payment_method',
  })
  paymentMethod: PaymentMethod;

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

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

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

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

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

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

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

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

  // 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: 'posted_at' })
  postedAt: Date | null;

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