from sqlalchemy import create_engine, Column, Integer, String, DateTime, ForeignKey, Enum, Text from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship import enum import datetime Base = declarative_base() class SubscriptionStatus(enum.Enum): ACTIVE = "active" EXPIRED = "expired" PENDING = "pending" CANCELLED = "cancelled" class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) user_id = Column(String, unique=True, nullable=False) # UUID generated in frontend created_at = Column(DateTime, default=datetime.datetime.utcnow) subscriptions = relationship("Subscription", back_populates="user") payments = relationship("Payment", back_populates="user") class Subscription(Base): __tablename__ = 'subscriptions' id = Column(Integer, primary_key=True) user_id = Column(Integer, ForeignKey('users.id')) invoice_id = Column(String, unique=True) public_key = Column(Text, nullable=False) # WireGuard public key start_time = Column(DateTime, nullable=False) expiry_time = Column(DateTime, nullable=False) status = Column(Enum(SubscriptionStatus), default=SubscriptionStatus.PENDING) warning_sent = Column(Integer, default=0) assigned_ip = Column(String) # WireGuard IP address assigned to this subscription user = relationship("User", back_populates="subscriptions") payments = relationship("Payment", back_populates="subscription") class Payment(Base): __tablename__ = 'payments' id = Column(Integer, primary_key=True) user_id = Column(Integer, ForeignKey('users.id')) subscription_id = Column(Integer, ForeignKey('subscriptions.id')) invoice_id = Column(String, unique=True) amount = Column(Integer, nullable=False) # Amount in sats timestamp = Column(DateTime, default=datetime.datetime.utcnow) user = relationship("User", back_populates="payments") subscription = relationship("Subscription", back_populates="payments")