import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, Index, ManyToOne, JoinColumn } from 'typeorm';
import { User } from './user.entity';
import { Merchant } from './merchant.entity';
import { Room } from './room.entity';

export type OrderStatus = 'pending_pay' | 'pending_confirm' | 'pending_checkin' | 'checked_in' | 'completed' | 'cancelled' | 'refunding' | 'refunded';

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

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

  @Index()
  @Column({ name: 'user_id', type: 'bigint', unsigned: true, comment: '用户ID' })
  userId: number;

  @Index()
  @Column({ name: 'merchant_id', type: 'bigint', unsigned: true, comment: '商家ID' })
  merchantId: number;

  @Index()
  @Column({ name: 'room_id', type: 'bigint', unsigned: true, comment: '房型ID' })
  roomId: number;

  @Index()
  @Column({ name: 'check_in_date', type: 'date', comment: '入住日期' })
  checkInDate: string;

  @Column({ name: 'check_out_date', type: 'date', comment: '离店日期' })
  checkOutDate: string;

  @Column({ type: 'tinyint', unsigned: true, comment: '入住晚数' })
  nights: number;

  @Column({ name: 'room_count', type: 'tinyint', unsigned: true, default: 1, comment: '房间数' })
  roomCount: number;

  @Column({ name: 'room_price', type: 'decimal', precision: 10, scale: 2, unsigned: true, comment: '房费单价' })
  roomPrice: number;

  @Column({ name: 'room_amount', type: 'decimal', precision: 10, scale: 2, unsigned: true, comment: '房费总额' })
  roomAmount: number;

  @Column({ name: 'service_fee', type: 'decimal', precision: 10, scale: 2, unsigned: true, default: 0, comment: '软件服务费' })
  serviceFee: number;

  @Column({ name: 'merchant_income', type: 'decimal', precision: 10, scale: 2, unsigned: true, default: 0, comment: '商家预计收入' })
  merchantIncome: number;

  @Column({ name: 'coupon_discount', type: 'decimal', precision: 10, scale: 2, unsigned: true, default: 0, comment: '优惠券抵扣' })
  couponDiscount: number;

  @Column({ name: 'user_coupon_id', type: 'bigint', unsigned: true, nullable: true, comment: '使用的用户优惠券ID' })
  userCouponId: number | null;

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

  @Column({ name: 'pay_amount', type: 'decimal', precision: 10, scale: 2, unsigned: true, comment: '实付金额' })
  payAmount: number;

  @Column({ name: 'payment_method', type: 'enum', enum: ['wechat', 'alipay', 'balance'], nullable: true, comment: '支付方式' })
  paymentMethod: 'wechat' | 'alipay' | 'balance' | null;

  @Column({ name: 'payment_no', length: 64, nullable: true, comment: '支付流水号' })
  paymentNo: string;

  @Column({ name: 'transaction_id', length: 64, nullable: true, comment: '第三方支付交易号' })
  transactionId: string;

  @Column({ name: 'paid_at', type: 'datetime', nullable: true, comment: '支付时间' })
  paidAt: Date;

  @Index()
  @Column({ type: 'enum', enum: ['pending_pay', 'pending_confirm', 'pending_checkin', 'checked_in', 'completed', 'cancelled', 'refunding', 'refunded'], default: 'pending_pay', comment: '订单状态' })
  status: OrderStatus;

  @Column({ name: 'cancel_reason', length: 500, nullable: true, comment: '取消原因' })
  cancelReason: string;

  @Column({ name: 'contact_name', length: 50, comment: '联系人姓名' })
  contactName: string;

  @Column({ name: 'contact_phone', length: 20, comment: '联系人手机' })
  contactPhone: string;

  @Column({ name: 'contact_id_card', length: 18, nullable: true, comment: '联系人身份证号' })
  contactIdCard?: string;

  @Column({ name: 'guest_count', type: 'tinyint', unsigned: true, default: 1, comment: '入住人数' })
  guestCount: number;

  @Column({ length: 500, default: '', comment: '备注' })
  remark: string;

  @Column({ type: 'enum', enum: ['miniapp', 'web', 'third_party'], default: 'miniapp', comment: '订单来源' })
  source: 'miniapp' | 'web' | 'third_party';

  @Column({ name: 'confirmed_at', type: 'datetime', nullable: true, comment: '商家确认时间' })
  confirmedAt: Date;

  @Column({ name: 'checkin_at', type: 'datetime', nullable: true, comment: '实际入住时间' })
  checkinAt: Date;

  @Column({ name: 'checkout_at', type: 'datetime', nullable: true, comment: '实际离店时间' })
  checkoutAt: Date;

  @Column({ name: 'cancelled_at', type: 'datetime', nullable: true, comment: '取消时间' })
  cancelledAt: Date;

  @Column({ name: 'refund_amount', type: 'decimal', precision: 10, scale: 2, unsigned: true, nullable: true, comment: '退款金额' })
  refundAmount: number;

  @Column({ name: 'refund_at', type: 'datetime', nullable: true, comment: '退款时间' })
  refundAt: Date;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'user_id' })
  user: User;

  @ManyToOne(() => Merchant)
  @JoinColumn({ name: 'merchant_id' })
  merchant: Merchant;

  @ManyToOne(() => Room)
  @JoinColumn({ name: 'room_id' })
  room: Room;

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

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