# models/database/order.py

from sqlalchemy import (
    Boolean,
    Column,
    DateTime,
    Float,
    ForeignKey,
    Integer,
    String,
    Text,
)
from sqlalchemy.orm import relationship

from app.core.database import Base
from models.database.base import TimestampMixin


class Order(Base, TimestampMixin):
    """Customer orders."""

    __tablename__ = "orders"

    id = Column(Integer, primary_key=True, index=True)
    vendor_id = Column(Integer, ForeignKey("vendors.id"), nullable=False, index=True)
    customer_id = Column(
        Integer, ForeignKey("customers.id"), nullable=False, index=True
    )

    order_number = Column(String, nullable=False, unique=True, index=True)

    # Order status
    status = Column(String, nullable=False, default="pending", index=True)
    # pending, processing, shipped, delivered, cancelled, refunded

    # Financial
    subtotal = Column(Float, nullable=False)
    tax_amount = Column(Float, default=0.0)
    shipping_amount = Column(Float, default=0.0)
    discount_amount = Column(Float, default=0.0)
    total_amount = Column(Float, nullable=False)
    currency = Column(String, default="EUR")

    # Addresses (stored as IDs)
    shipping_address_id = Column(
        Integer, ForeignKey("customer_addresses.id"), nullable=False
    )
    billing_address_id = Column(
        Integer, ForeignKey("customer_addresses.id"), nullable=False
    )

    # Shipping
    shipping_method = Column(String, nullable=True)
    tracking_number = Column(String, nullable=True)

    # Notes
    customer_notes = Column(Text, nullable=True)
    internal_notes = Column(Text, nullable=True)

    # Timestamps
    paid_at = Column(DateTime, nullable=True)
    shipped_at = Column(DateTime, nullable=True)
    delivered_at = Column(DateTime, nullable=True)
    cancelled_at = Column(DateTime, nullable=True)

    # Relationships
    vendor = relationship("Vendor")
    customer = relationship("Customer", back_populates="orders")
    items = relationship(
        "OrderItem", back_populates="order", cascade="all, delete-orphan"
    )
    shipping_address = relationship(
        "CustomerAddress", foreign_keys=[shipping_address_id]
    )
    billing_address = relationship("CustomerAddress", foreign_keys=[billing_address_id])

    def __repr__(self):
        return f"<Order(id={self.id}, order_number='{self.order_number}', status='{self.status}')>"


class OrderItem(Base, TimestampMixin):
    """Individual items in an order."""

    __tablename__ = "order_items"

    id = Column(Integer, primary_key=True, index=True)
    order_id = Column(Integer, ForeignKey("orders.id"), nullable=False, index=True)
    product_id = Column(Integer, ForeignKey("products.id"), nullable=False)

    # Product details at time of order (snapshot)
    product_name = Column(String, nullable=False)
    product_sku = Column(String, nullable=True)

    quantity = Column(Integer, nullable=False)
    unit_price = Column(Float, nullable=False)
    total_price = Column(Float, nullable=False)

    # Inventory tracking
    inventory_reserved = Column(Boolean, default=False)
    inventory_fulfilled = Column(Boolean, default=False)

    # Relationships
    order = relationship("Order", back_populates="items")
    product = relationship("Product")

    def __repr__(self):
        return f"<OrderItem(id={self.id}, order_id={self.order_id}, product_id={self.product_id})>"
