/**
 * Partner Entity
 * Clientes, proveedores y socios comerciales
 *
 * @module Partners
 * @table partners.partners
 */

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

@Entity({ name: 'partners', schema: 'partners' })
export class Partner {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  // Identificacion
  @Index()
  @Column({ type: 'varchar', length: 20, unique: true })
  code: string;

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

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

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

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

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

  @Column({ name: 'cfdi_use', type: 'varchar', length: 10, nullable: true })
  cfdiUse: string;

  // Contacto principal
  @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: 500, nullable: true })
  website: string;

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

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

  @Column({ name: 'current_balance', type: 'decimal', precision: 15, scale: 2, default: 0 })
  currentBalance: number;

  // Lista de precios
  @Column({ name: 'price_list_id', type: 'uuid', nullable: true })
  priceListId: string;

  // Descuentos
  @Column({ name: 'discount_percent', type: 'decimal', precision: 5, scale: 2, default: 0 })
  discountPercent: number;

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

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

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

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

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

  // Vendedor asignado
  @Column({ name: 'sales_rep_id', type: 'uuid', nullable: true })
  salesRepId: 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;
}
