// src/entities/order.entity.ts
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, OneToMany, JoinColumn } from 'typeorm';
import { User } from '../entity/Users';
import { Address } from '../entity/Address';
import { OrderItem } from '../entity/OrderItem';

export enum OrderStatus {
  PENDING = 'pending',
  PROCESSING = 'processing',
  SHIPPED = 'shipped',
  DELIVERED = 'delivered',
  CANCELLED = 'cancelled'
}

export enum PaymentMethod {
  COD = 'cod',
  CREDIT_CARD = 'credit_card',
  DEBIT_CARD = 'debit_card',
  NET_BANKING = 'net_banking',
  UPI = 'upi',
  WALLET = 'wallet'
}

@Entity()
export class Order {
  @PrimaryGeneratedColumn()
  order_id?: number;

  @Column({
    type: 'enum',
    enum: OrderStatus,
    default: OrderStatus.PENDING
  })
  status?: OrderStatus;

  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  order_date?: Date;

  @Column('decimal', { precision: 10, scale: 2 })
  subtotal?: number;

  @Column('decimal', { precision: 10, scale: 2 })
  tax_amount?: number;

  @Column('decimal', { precision: 10, scale: 2 })
  total_amount?: number;

  @Column({
    type: 'enum',
    enum: PaymentMethod
  })
  payment_method?: PaymentMethod;

  @Column({ nullable: true })
  tracking_number?: string;

  @Column({ default: false })
  is_tax_inclusive?: boolean;

  @Column({ default: false })
  gst_invoice_required?: boolean;

  // Relations
  @ManyToOne(() => User, user => user.orders)
  @JoinColumn({ name: 'user_id' })
  user?: User;

  @ManyToOne(() => Address)
  @JoinColumn({ name: 'address_id' })
  address?: Address;

  @OneToMany(() => OrderItem, orderItem => orderItem.order)
  items?: OrderItem[];
}