"""Database models for coverage service."""

# FILE: apps/svc_coverage/models.py

from datetime import datetime

from sqlalchemy import JSON, Column, DateTime, Integer, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class CoverageVersion(Base):
    """Policy version tracking table"""

    __tablename__ = "coverage_versions"

    id = Column(Integer, primary_key=True, autoincrement=True)
    version = Column(String(50), nullable=False)
    jurisdiction = Column(String(10), nullable=False)
    tax_year = Column(String(10), nullable=False)
    tenant_id = Column(String(100), nullable=True)
    source_files = Column(JSON, nullable=False, default=list)
    compiled_at = Column(DateTime, nullable=False, default=datetime.utcnow)
    hash = Column(String(64), nullable=False)

    def __repr__(self) -> str:
        return f"<CoverageVersion(id={self.id}, version='{self.version}', hash='{self.hash[:8]}...')>"


class CoverageAudit(Base):
    """Coverage evaluation audit trail"""

    __tablename__ = "coverage_audit"

    id = Column(Integer, primary_key=True, autoincrement=True)
    taxpayer_id = Column(String(100), nullable=False)
    tax_year = Column(String(10), nullable=False)
    policy_version = Column(String(50), nullable=False)
    overall_status = Column(String(20), nullable=False)
    blocking_items = Column(JSON, nullable=False, default=list)
    created_at = Column(DateTime, nullable=False, default=datetime.utcnow)
    trace_id = Column(String(100), nullable=True)

    def __repr__(self) -> str:
        return f"<CoverageAudit(id={self.id}, taxpayer_id='{self.taxpayer_id}', status='{self.overall_status}')>"
