import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn, VersionColumn, Index } from 'typeorm';

export enum CouponStatus {
  MINTED = 'minted',
  LISTED = 'listed',
  SOLD = 'sold',
  IN_CIRCULATION = 'in_circulation',
  REDEEMED = 'redeemed',
  EXPIRED = 'expired',
  RECALLED = 'recalled',
}
export enum CouponType { UTILITY = 'utility', SECURITY = 'security' }

@Entity('coupons')
@Index('idx_coupons_issuer', ['issuerId'])
@Index('idx_coupons_status', ['status'])
@Index('idx_coupons_category', ['category'])
export class Coupon {
  @PrimaryGeneratedColumn('uuid') id: string;
  @Column({ name: 'chain_token_id', type: 'bigint', nullable: true, unique: true }) chainTokenId: string | null;
  @Column({ name: 'issuer_id', type: 'uuid' }) issuerId: string;
  @Column({ type: 'varchar', length: 200 }) name: string;
  @Column({ type: 'text', nullable: true }) description: string | null;
  @Column({ name: 'image_url', type: 'varchar', length: 500, nullable: true }) imageUrl: string | null;
  @Column({ name: 'face_value', type: 'numeric', precision: 12, scale: 2 }) faceValue: string;
  @Column({ name: 'current_price', type: 'numeric', precision: 12, scale: 2, nullable: true }) currentPrice: string | null;
  @Column({ name: 'issue_price', type: 'numeric', precision: 12, scale: 2, nullable: true }) issuePrice: string | null;
  @Column({ name: 'total_supply', type: 'int', default: 1 }) totalSupply: number;
  @Column({ name: 'remaining_supply', type: 'int', default: 1 }) remainingSupply: number;
  @Column({ name: 'expiry_date', type: 'date' }) expiryDate: Date;
  @Column({ name: 'coupon_type', type: 'varchar', length: 10, default: 'utility' }) couponType: string;
  @Column({ type: 'varchar', length: 50, nullable: true }) category: string;
  @Column({ type: 'varchar', length: 20, default: 'minted' }) status: string;
  @Column({ name: 'owner_user_id', type: 'uuid', nullable: true }) ownerUserId: string | null;
  @Column({ name: 'resale_count', type: 'smallint', default: 0 }) resaleCount: number;
  @Column({ name: 'max_resale_count', type: 'smallint', default: 3 }) maxResaleCount: number;
  @Column({ name: 'is_transferable', type: 'boolean', default: true }) isTransferable: boolean;
  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' }) createdAt: Date;
  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' }) updatedAt: Date;

  // Virtual: populated via JOIN
  issuer?: any;
}
