from sqlalchemy import create_engine, Column, String, Integer, DateTime, ForeignKey, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from datetime import datetime

DATABASE_URL = "sqlite:///c:/Users/rbiter/Desktop/AboutMe/backend/database.db"

engine = create_engine(DATABASE_URL)

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True, autoincrement=True) 
    login = Column(String, nullable=False, unique=True) 
    password = Column(String, nullable=False) 
    email = Column(String, nullable=False, unique=True) 
    last_name = Column(String, nullable=False) 
    first_name = Column(String, nullable=False) 
    middle_name = Column(String, nullable=True) 
    access_level = Column(Integer, nullable=False, default=1) 
    avatar = Column(String, nullable=True) 
    dateregistration = Column(DateTime, default=datetime.utcnow)

class Poster(Base):
    __tablename__ = 'posters'
    id = Column(Integer, primary_key=True, autoincrement=True)
    title = Column(String, nullable=False) 
    description = Column(String, nullable=False)
    image = Column(String, nullable=False)  
    date = Column(DateTime, nullable=False)  
    price = Column(Integer, nullable=False, default=0)
    like = Column(Integer, nullable=False, default=0)
    datecreation = Column(DateTime, default=datetime.utcnow)

class Sale(Base):
    __tablename__ = "sales"

    id = Column(Integer, primary_key=True, autoincrement=True)
    amount = Column(Integer, nullable=False)
    status = Column(String(50), nullable=False, default="pending")  # pending, paid, failed, canceled
    created_at = Column(DateTime, default=datetime.utcnow)
    description = Column(String(255), nullable=False)

    poster_id = Column(Integer, ForeignKey("posters.id"), nullable=True)
    user_id = Column(Integer, nullable=False)
    user_email = Column(String, ForeignKey("users.email"), nullable=False)
    payment_id = Column(String, nullable=False, unique=True)
    confirmation_url = Column(String, nullable=True)



class PosterLike(Base):
    __tablename__ = 'poster_likes'
    id = Column(Integer, primary_key=True, autoincrement=True)
    poster_id = Column(Integer, ForeignKey('posters.id'), nullable=False)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    date_created = Column(DateTime, default=datetime.utcnow)
    

    __table_args__ = (UniqueConstraint('poster_id', 'user_id', name='uix_poster_user'),)
class FeedBack(Base):
    __tablename__ = "feedback"

    id = Column(Integer, primary_key=True, autoincrement=True)
    userid = Column(Integer, ForeignKey("users.id"), nullable=False)
    text = Column(String, nullable=False)
    date = Column(DateTime, default=datetime.utcnow)
    rating = Column(Integer, nullable=False) 

def initialize_database():
    Base.metadata.create_all(engine)
    with engine.connect() as connection:
        print("База данных успешно создана или подключена!")

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()