import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, JoinColumn } from 'typeorm';
import { UserEntity } from '../users/user.entity';
import { RouteEntity } from '../routes/route.entity';
import { OrderStatus, PaymentStatus } from '../../share/order.types';

@Entity('orders')
export class Order {
  @PrimaryGeneratedColumn({ comment: '订单ID' })
  id!: number;

  @Column({ type: 'int', unsigned: true, comment: '用户ID' })
  userId!: number;

  @Column({ type: 'int', unsigned: true, comment: '路线ID' })
  routeId!: number;

  @Column({ type: 'int', unsigned: true, comment: '乘客数量' })
  passengerCount!: number;

  @Column('decimal', { precision: 10, scale: 2, comment: '订单总金额' })
  totalAmount!: number;

  @Column({
    type: 'enum',
    enum: OrderStatus,
    default: OrderStatus.PENDING_PAYMENT,
    comment: '订单状态'
  })
  status!: OrderStatus;

  @Column({
    type: 'enum',
    enum: PaymentStatus,
    default: PaymentStatus.PENDING,
    comment: '支付状态'
  })
  paymentStatus!: PaymentStatus;

  @Column('json', { comment: '乘客信息快照数组（下单时的多个乘客信息）' })
  passengerSnapshots!: any[];

  @Column('json', { comment: '路线信息快照（下单时的路线信息）' })
  routeSnapshot!: any;

  @Column({ type: 'int', unsigned: true, nullable: true, comment: '创建人ID' })
  createdBy?: number;

  @Column({ type: 'int', unsigned: true, nullable: true, comment: '更新人ID' })
  updatedBy?: number;

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

  @UpdateDateColumn({ comment: '更新时间' })
  updatedAt!: Date;

  @ManyToOne(() => UserEntity)
  @JoinColumn({ name: 'userId' })
  user!: UserEntity;

  @ManyToOne(() => RouteEntity)
  @JoinColumn({ name: 'routeId' })
  route!: RouteEntity;
}