from datetime import datetime, timezone
from decimal import Decimal
from sqlalchemy import String, DateTime, DECIMAL, Integer, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column
from app.core.database import Base


class VehicleStock(Base):
    __tablename__ = "vehicle_stock"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    plate: Mapped[str] = mapped_column(String(10), nullable=False, index=True)
    brand: Mapped[str | None] = mapped_column(String(50))
    model: Mapped[str | None] = mapped_column(String(100))
    version: Mapped[str | None] = mapped_column(String(200))
    registration_date: Mapped[str | None] = mapped_column(String(20))
    mileage: Mapped[int | None] = mapped_column(Integer)
    location: Mapped[str | None] = mapped_column(String(100))
    import_batch: Mapped[str | None] = mapped_column(String(100))
    imported_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
    )


class RepairCostAvg(Base):
    __tablename__ = "repair_costs_avg"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    model: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
    avg_cost: Mapped[Decimal | None] = mapped_column(DECIMAL(10, 2))
    import_batch: Mapped[str | None] = mapped_column(String(100))
    imported_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
    )


class VehicleSalesAvg(Base):
    __tablename__ = "vehicle_sales_avg"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    model: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
    avg_stock_days: Mapped[Decimal | None] = mapped_column(DECIMAL(6, 1))
    avg_repair_cost: Mapped[Decimal | None] = mapped_column(DECIMAL(10, 2))
    avg_margin_eur: Mapped[Decimal | None] = mapped_column(DECIMAL(10, 2))
    avg_margin_pct: Mapped[Decimal | None] = mapped_column(DECIMAL(5, 2))
    avg_sale_price: Mapped[Decimal | None] = mapped_column(DECIMAL(10, 2))
    import_batch: Mapped[str | None] = mapped_column(String(100))
    imported_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
    )
