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

/**
 * Partner Segment Entity (schema: partners.partner_segments)
 *
 * Defines customer/supplier segments for grouping and analytics.
 * Examples: VIP Customers, Wholesale Buyers, Local Suppliers, etc.
 */
@Entity({ name: 'partner_segments', schema: 'partners' })
export class PartnerSegment {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

  // Segment type
  @Column({ name: 'segment_type', type: 'varchar', length: 20, default: 'customer' })
  segmentType: 'customer' | 'supplier' | 'both';

  // Styling
  @Column({ type: 'varchar', length: 20, nullable: true })
  color: string;

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

  // Rules for auto-assignment (stored as JSON)
  @Column({ type: 'jsonb', nullable: true })
  rules: Record<string, unknown>;

  // Benefits/conditions
  @Column({ name: 'default_discount', type: 'decimal', precision: 5, scale: 2, default: 0 })
  defaultDiscount: number;

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

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

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

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

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