import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, Index, ManyToOne, JoinColumn } from 'typeorm';
import { User } from './user.entity';
import { Order } from './order.entity';
import { MktActivity } from './mkt-activity.entity';

export type CashbackStatus = 'pending' | 'settled' | 'cancelled';

@Entity('mkt_cashbacks')
export class MktCashback {
  @PrimaryGeneratedColumn({ type: 'bigint', unsigned: true })
  id: number;

  @Index()
  @Column({ name: 'activity_id', type: 'bigint', unsigned: true, comment: '活动ID' })
  activityId: number;

  @Index()
  @Column({ name: 'inviter_id', type: 'bigint', unsigned: true, comment: '邀请人用户ID' })
  inviterId: number;

  @Index()
  @Column({ name: 'invitee_id', type: 'bigint', unsigned: true, comment: '被邀请人用户ID' })
  inviteeId: number;

  @Index()
  @Column({ name: 'order_id', type: 'bigint', unsigned: true, comment: '关联订单ID' })
  orderId: number;

  @Column({ name: 'order_no', length: 32, comment: '订单号' })
  orderNo: string;

  @Column({ name: 'order_amount', type: 'decimal', precision: 10, scale: 2, unsigned: true, comment: '订单金额' })
  orderAmount: number;

  @Column({ name: 'order_index', type: 'tinyint', unsigned: true, comment: '被邀请人第几单(1/2)' })
  orderIndex: number;

  @Column({ type: 'decimal', precision: 5, scale: 4, unsigned: true, comment: '返现比例' })
  rate: number;

  @Column({ type: 'decimal', precision: 10, scale: 2, unsigned: true, comment: '返现金额' })
  amount: number;

  @Index()
  @Column({
    type: 'enum',
    enum: ['pending', 'settled', 'cancelled'],
    default: 'pending',
    comment: '状态',
  })
  status: CashbackStatus;

  @Column({ name: 'settled_at', type: 'datetime', nullable: true, comment: '到账时间' })
  settledAt: Date;

  @ManyToOne(() => MktActivity)
  @JoinColumn({ name: 'activity_id' })
  activity: MktActivity;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'inviter_id' })
  inviter: User;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'invitee_id' })
  invitee: User;

  @ManyToOne(() => Order)
  @JoinColumn({ name: 'order_id' })
  order: Order;

  @CreateDateColumn({ name: 'created_at', comment: '创建时间' })
  createdAt: Date;
}
