from sqlalchemy import Column, Integer, String, DateTime, JSON, ForeignKey, Float
from sqlalchemy.dialects.postgresql import UUID
from database import Base
import datetime
import uuid
import hashlib

# Patent 4: Immutable Audit Trail
class AuditLog(Base):
    __tablename__ = "audit_logs"
    
    id = Column(String, primary_key=True, index=True, default=lambda: str(uuid.uuid4()))
    timestamp = Column(DateTime, default=datetime.datetime.utcnow)
    event_type = Column(String, index=True)
    details = Column(JSON)
    user_id = Column(String, index=True)
    prev_hash = Column(String)
    current_hash = Column(String, unique=True, index=True)

    def calculate_hash(self):
        """Re-calculates hash for integrity verification."""
        data_str = f"{self.timestamp}{self.event_type}{str(self.details)}{self.user_id}{self.prev_hash}"
        return hashlib.sha256(data_str.encode()).hexdigest()

# Patent 3: Risk Assessment History
class RiskAssessment(Base):
    __tablename__ = "risk_assessments"
    
    id = Column(String, primary_key=True, index=True, default=lambda: str(uuid.uuid4()))
    timestamp = Column(DateTime, default=datetime.datetime.utcnow)
    proposal_hash = Column(String, index=True)
    risk_score = Column(Float)
    risk_level = Column(String)
    dimension_scores = Column(JSON)
    industry = Column(String)

# Patent 7: Hallucination Checks
class HallucinationCheck(Base):
    __tablename__ = "hallucination_checks"
    
    id = Column(String, primary_key=True, index=True, default=lambda: str(uuid.uuid4()))
    timestamp = Column(DateTime, default=datetime.datetime.utcnow)
    text_snippet = Column(String) # Store snippet or hash for privacy
    confidence_score = Column(Float)
    is_valid = Column(Integer) # 0 or 1
    issues_found = Column(JSON)
