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

/**
 * Partner Entity
 *
 * Synchronized with DDL: database/ddl/16-partners.sql
 * Table: partners.partners
 *
 * Represents customers, suppliers, and other business partners.
 * Extended fiscal information is stored in partners.partner_tax_info (separate entity).
 */
@Entity({ name: 'partners', schema: 'partners' })
export class Partner {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  // Identification
  @Index()
  @Column({ type: 'varchar', length: 30 })
  code: string;

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

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

  // Partner type
  @Index()
  @Column({ name: 'partner_type', type: 'varchar', length: 20, default: 'customer' })
  partnerType: 'customer' | 'supplier' | 'both' | 'contact';

  // Fiscal data
  @Index()
  @Column({ name: 'tax_id', type: 'varchar', length: 50, nullable: true })
  taxId: string;

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

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

  // Primary contact
  @Column({ type: 'varchar', length: 255, nullable: true })
  email: string;

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

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

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

  // Credit and payments
  @Column({ name: 'credit_limit', type: 'decimal', precision: 15, scale: 2, default: 0 })
  creditLimit: number;

  @Column({ name: 'payment_term_days', type: 'int', default: 0 })
  paymentTermDays: number;

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

  // Classification
  @Column({ type: 'varchar', length: 50, nullable: true })
  category: string;

  @Column({ type: 'text', array: true, default: '{}' })
  tags: string[];

  // Status
  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive: boolean;

  @Column({ name: 'is_verified', type: 'boolean', default: false })
  isVerified: boolean;

  @Column({ name: 'verified_at', type: 'timestamptz', nullable: true })
  verifiedAt: Date | null;

  // Settings (flexible JSONB)
  @Column({ type: 'jsonb', default: '{}' })
  settings: Record<string, unknown>;

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

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

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

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