/**
 * Payment Entity
 * Payment received or made
 * Compatible with erp-core payment.entity
 *
 * @module Invoices
 */

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

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

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

  @Index()
  @Column({ name: 'payment_number', type: 'varchar', length: 30 })
  paymentNumber: string;

  @Index()
  @Column({ name: 'payment_type', type: 'varchar', length: 20, default: 'received' })
  paymentType: 'received' | 'made';

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

  @Column({ name: 'partner_name', type: 'varchar', length: 200, nullable: true })
  partnerName: string;

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

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

  @Column({ name: 'exchange_rate', type: 'decimal', precision: 10, scale: 6, default: 1 })
  exchangeRate: number;

  @Column({ name: 'payment_date', type: 'date', default: () => 'CURRENT_DATE' })
  paymentDate: Date;

  @Index()
  @Column({ name: 'payment_method', type: 'varchar', length: 50 })
  paymentMethod: string;

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

  @Column({ name: 'bank_account_id', type: 'uuid', nullable: true })
  bankAccountId: string;

  @Index()
  @Column({ type: 'varchar', length: 20, default: 'draft' })
  status: 'draft' | 'confirmed' | 'reconciled' | 'cancelled';

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

  @Column({ name: 'cfdi_uuid', type: 'varchar', length: 40, nullable: true })
  cfdiUuid: string;

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

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

  @Column({ name: 'created_by', type: 'uuid', nullable: true })
  createdBy?: string;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
  updatedAt: Date;

  @DeleteDateColumn({ name: 'deleted_at', type: 'timestamptz', nullable: true })
  deletedAt: Date;
}
