File size: 2,379 Bytes
8f76784
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
from sqlalchemy import Column, String, Integer, DateTime, JSON, Text, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from datetime import datetime, timezone

from config import config

# Database Configuration — Local SQLite
DATABASE_URL = config.LOCAL_DATABASE_URL

engine = create_engine(
    DATABASE_URL, 
    connect_args={"check_same_thread": False}
)


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

class UserProfile(Base):
    __tablename__ = "profiles"
    id = Column(String, primary_key=True, index=True)
    name = Column(String)
    email = Column(String, unique=True, index=True)
    hashed_password = Column(String)
    plan = Column(String, default="free")
    
    # Credit System
    credits = Column(Integer, default=3)
    chat_credits = Column(Integer, default=5)
    last_credit_reset = Column(DateTime, default=lambda: datetime.now(timezone.utc))
    
    created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))

class Analysis(Base):
    __tablename__ = "analyses"
    id = Column(String, primary_key=True, index=True)
    user_id = Column(String, index=True)
    filename = Column(String)
    doc_type = Column(String)
    overall_risk = Column(String)
    summary = Column(Text) # Encrypted
    red_flags = Column(Text) # Encrypted JSON string
    positive_clauses = Column(Text) # Encrypted JSON string
    
    # Sharing Features
    is_public = Column(Boolean, default=False)
    share_token = Column(String, unique=True, index=True, nullable=True)
    
    # Retention Features
    renewal_date = Column(String, nullable=True)
    reminded = Column(Boolean, default=False)
    
    created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))

class Comparison(Base):
    __tablename__ = "comparisons"
    id = Column(String, primary_key=True, index=True)
    user_id = Column(String, index=True)
    filename_a = Column(String)
    filename_b = Column(String)
    result = Column(JSON) # Stores the ComparisonResult data
    created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))

def init_db():
    Base.metadata.create_all(bind=engine)

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