import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, Check } from 'typeorm';
import { License } from './license.entity';
import { StoreItem } from './store-item.entity';

@Entity('store_transactions')
@Check(`"status" IN ('pending', 'paid', 'delivered', 'failed', 'refunded')`)
export class StoreTransaction {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'uuid' })
  license_id: string;

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

  @Column({ type: 'varchar', length: 20 })
  steam_id: string;

  @Column({ type: 'varchar', length: 100, nullable: true })
  player_name: string | null;

  @Column({ type: 'varchar', length: 255, unique: true })
  paypal_order_id: string;

  @Column({ type: 'varchar', length: 255, nullable: true })
  paypal_transaction_id: string | null;

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

  @Column({ type: 'varchar', length: 3, default: 'USD' })
  currency: string;

  @Column({ type: 'varchar', length: 50 })
  status: string;

  @Column({ type: 'boolean', default: false })
  delivered: boolean;

  @Column({ type: 'timestamptz', nullable: true })
  delivered_at: Date | null;

  @Column({ type: 'varchar', length: 255, nullable: true })
  payer_email: string | null;

  @Column({ type: 'timestamptz', default: () => 'NOW()' })
  created_at: Date;

  @ManyToOne(() => License, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'license_id' })
  license: License;

  @ManyToOne(() => StoreItem, { onDelete: 'SET NULL', nullable: true })
  @JoinColumn({ name: 'item_id' })
  item: StoreItem | null;
}
