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

@Entity('webstore_subscriptions')
@Check(`"status" IN ('active', 'cancelled', 'suspended', 'past_due')`)
export class WebstoreSubscription {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

  @Column({ type: 'varchar', length: 100 })
  plan_id: string;

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

  @Column({ type: 'timestamptz' })
  current_period_start: Date;

  @Column({ type: 'timestamptz' })
  current_period_end: Date;

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

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

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