import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  OneToMany,
} from 'typeorm';
import { CouponRedemption } from './coupon-redemption.entity';

export type DiscountType = 'percentage' | 'fixed';
export type DurationPeriod = 'once' | 'forever' | 'months';

@Entity({ name: 'coupons', schema: 'billing' })
export class Coupon {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

  @Column({ type: 'varchar', length: 50, unique: true })
  code!: string;

  @Column({ type: 'varchar', length: 255 })
  name!: string;

  @Column({ type: 'text', nullable: true })
  description?: string;

  @Column({ name: 'discount_type', type: 'varchar', length: 20 })
  discountType!: DiscountType;

  @Column({ name: 'discount_value', type: 'decimal', precision: 10, scale: 2 })
  discountValue!: number;

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

  @Column({ name: 'applicable_plans', type: 'uuid', array: true, default: [] })
  applicablePlans!: string[];

  @Column({ name: 'min_amount', type: 'decimal', precision: 10, scale: 2, default: 0 })
  minAmount!: number;

  @Column({ name: 'duration_period', type: 'varchar', length: 20, default: 'once' })
  durationPeriod!: DurationPeriod;

  @Column({ name: 'duration_months', type: 'integer', nullable: true })
  durationMonths?: number;

  @Column({ name: 'max_redemptions', type: 'integer', nullable: true })
  maxRedemptions?: number;

  @Column({ name: 'current_redemptions', type: 'integer', default: 0 })
  currentRedemptions!: number;

  @Column({ name: 'valid_from', type: 'timestamptz', nullable: true })
  validFrom?: Date;

  @Column({ name: 'valid_until', type: 'timestamptz', nullable: true })
  validUntil?: Date;

  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive!: boolean;

  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt!: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
  updatedAt!: Date;

  @OneToMany(() => CouponRedemption, (redemption) => redemption.coupon)
  redemptions!: CouponRedemption[];
}
