from datetime import datetime from sqlalchemy import ( Column, DateTime, ForeignKey, Integer, Numeric, String, Text, ) from sqlalchemy.orm import relationship, Mapped, mapped_column from .db import Base class Customer(Base): __tablename__ = "customers" id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) name: Mapped[str] = mapped_column(String(255), nullable=False) contact_info: Mapped[str | None] = mapped_column(String(512), nullable=True) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=datetime.utcnow, nullable=False ) projects: Mapped[list["Project"]] = relationship( "Project", back_populates="customer", cascade="all, delete-orphan" ) class Project(Base): """ Project Archive: stores original requirement text and AI-generated solution. """ __tablename__ = "projects" id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) customer_id: Mapped[int] = mapped_column( Integer, ForeignKey("customers.id", ondelete="CASCADE"), nullable=False, index=True ) raw_requirement: Mapped[str] = mapped_column(Text, nullable=False) ai_solution_md: Mapped[str | None] = mapped_column(Text, nullable=True) status: Mapped[str] = mapped_column(String(50), default="draft", nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=datetime.utcnow, nullable=False ) customer: Mapped[Customer] = relationship("Customer", back_populates="projects") quotes: Mapped[list["Quote"]] = relationship( "Quote", back_populates="project", cascade="all, delete-orphan" ) class Quote(Base): __tablename__ = "quotes" id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) project_id: Mapped[int] = mapped_column( Integer, ForeignKey("projects.id", ondelete="CASCADE"), nullable=False, index=True ) total_amount: Mapped[Numeric] = mapped_column(Numeric(12, 2), nullable=False) file_path: Mapped[str] = mapped_column(String(512), nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=datetime.utcnow, nullable=False ) project: Mapped[Project] = relationship("Project", back_populates="quotes") class FinanceRecord(Base): __tablename__ = "finance_records" id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) month: Mapped[str] = mapped_column(String(7), nullable=False, index=True) # YYYY-MM type: Mapped[str] = mapped_column(String(50), nullable=False) # invoice / bank_receipt / ... file_name: Mapped[str] = mapped_column(String(255), nullable=False) file_path: Mapped[str] = mapped_column(String(512), nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=datetime.utcnow, nullable=False )