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

/**
 * Tipo de descuento
 */
export enum DiscountType {
  PERCENTAGE = 'percentage',  // Porcentaje del total
  FIXED = 'fixed',            // Monto fijo
  PRICE_OVERRIDE = 'price_override', // Precio especial
}

/**
 * Aplicación del descuento
 */
export enum DiscountAppliesTo {
  ALL = 'all',              // Todos los productos
  CATEGORY = 'category',     // Categoría específica
  PRODUCT = 'product',       // Producto específico
  CUSTOMER = 'customer',     // Cliente específico
  CUSTOMER_GROUP = 'customer_group', // Grupo de clientes
}

/**
 * Condición de activación
 */
export enum DiscountCondition {
  NONE = 'none',                    // Sin condición
  MIN_QUANTITY = 'min_quantity',    // Cantidad mínima
  MIN_AMOUNT = 'min_amount',        // Monto mínimo
  DATE_RANGE = 'date_range',        // Rango de fechas
  FIRST_PURCHASE = 'first_purchase', // Primera compra
}

/**
 * Regla de descuento
 */
@Entity({ schema: 'core', name: 'discount_rules' })
@Index('idx_discount_rules_tenant_id', ['tenantId'])
@Index('idx_discount_rules_code_tenant', ['tenantId', 'code'], { unique: true })
@Index('idx_discount_rules_active', ['tenantId', 'isActive'])
@Index('idx_discount_rules_dates', ['tenantId', 'startDate', 'endDate'])
@Index('idx_discount_rules_priority', ['tenantId', 'priority'])
export class DiscountRule {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

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

  @Column({
    type: 'enum',
    enum: DiscountType,
    default: DiscountType.PERCENTAGE,
    name: 'discount_type',
  })
  discountType: DiscountType;

  @Column({
    type: 'decimal',
    precision: 15,
    scale: 4,
    nullable: false,
    name: 'discount_value',
  })
  discountValue: number;

  @Column({
    type: 'decimal',
    precision: 15,
    scale: 2,
    nullable: true,
    name: 'max_discount_amount',
  })
  maxDiscountAmount: number | null;

  @Column({
    type: 'enum',
    enum: DiscountAppliesTo,
    default: DiscountAppliesTo.ALL,
    name: 'applies_to',
  })
  appliesTo: DiscountAppliesTo;

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

  @Column({
    type: 'enum',
    enum: DiscountCondition,
    default: DiscountCondition.NONE,
    name: 'condition_type',
  })
  conditionType: DiscountCondition;

  @Column({
    type: 'decimal',
    precision: 15,
    scale: 4,
    nullable: true,
    name: 'condition_value',
  })
  conditionValue: number | null;

  @Column({ type: 'timestamp', nullable: true, name: 'start_date' })
  startDate: Date | null;

  @Column({ type: 'timestamp', nullable: true, name: 'end_date' })
  endDate: Date | null;

  @Column({ type: 'integer', nullable: false, default: 10 })
  priority: number;

  @Column({ type: 'boolean', nullable: false, default: true, name: 'combinable' })
  combinable: boolean;

  @Column({ type: 'integer', nullable: true, name: 'usage_limit' })
  usageLimit: number | null;

  @Column({ type: 'integer', nullable: false, default: 0, name: 'usage_count' })
  usageCount: number;

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

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

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

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