from sqlalchemy import Column, Integer, String, ForeignKey, Boolean, DateTime, JSON, Numeric
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
# JAVÍTVA: Import közvetlenül a base_class-ból
from app.db.base_class import Base

class SubscriptionTier(Base):
    __tablename__ = "subscription_tiers"
    __table_args__ = {"schema": "data"}
    id = Column(Integer, primary_key=True)
    name = Column(String, unique=True) # Free, Premium, VIP, Custom
    rules = Column(JSON) # {"max_vehicles": 5, "allow_api": true}
    is_custom = Column(Boolean, default=False)

class OrganizationSubscription(Base):
    __tablename__ = "org_subscriptions"
    __table_args__ = {"schema": "data"}
    id = Column(Integer, primary_key=True)
    org_id = Column(Integer, ForeignKey("data.organizations.id"))
    tier_id = Column(Integer, ForeignKey("data.subscription_tiers.id"))
    valid_from = Column(DateTime, server_default=func.now())
    valid_until = Column(DateTime)
    is_active = Column(Boolean, default=True)

class CreditTransaction(Base):
    __tablename__ = "credit_logs"
    __table_args__ = {"schema": "data"}
    id = Column(Integer, primary_key=True)
    org_id = Column(Integer, ForeignKey("data.organizations.id"))
    amount = Column(Numeric(10, 2))
    description = Column(String)
    created_at = Column(DateTime, server_default=func.now())

class ServiceSpecialty(Base):
    """Fa struktúra a szerviz szolgáltatásokhoz"""
    __tablename__ = "service_specialties"
    __table_args__ = {"schema": "data"}
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey("data.service_specialties.id"), nullable=True)
    name = Column(String, nullable=False)
    slug = Column(String, unique=True)

    parent = relationship("ServiceSpecialty", remote_side=[id], backref="children")