import enum
from datetime import datetime

from sqlalchemy import Integer, String, Enum, Float, ForeignKey, UniqueConstraint, DateTime, Text
from sqlalchemy.orm import mapped_column, Mapped, relationship

from src.category.models import Category
from src.database import Base
from src.manufacturer.models import Manufacturer



class ProductStatus(enum.IntEnum):
    OFF = 0
    ON = 1

class Product(Base):
    __tablename__ = "product"

    product_id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    description: Mapped[str] = mapped_column(Text, nullable=True)
    seo_keyword: Mapped[str] = mapped_column(String(255), nullable=True)
    meta_title: Mapped[str] = mapped_column(String(255), nullable=True)
    meta_description: Mapped[str] = mapped_column(Text, nullable=True)
    meta_keyword: Mapped[str] = mapped_column(String(255), nullable=True)
    image: Mapped[str] = mapped_column(String(255), nullable=True)
    status: Mapped[ProductStatus] = mapped_column(Enum(ProductStatus), default=ProductStatus.ON, nullable=False)
    date_added: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
    date_modify: Mapped[datetime] = mapped_column(
        DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
    )
    price: Mapped[float] = mapped_column(Float, nullable=False)
    price_old: Mapped[float] = mapped_column(Float, nullable=True)
    model: Mapped[str] = mapped_column(String(255), nullable=True)
    manufacturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("manufacturer.manufacturer_id"), nullable=True)
    rating: Mapped[float] =mapped_column(Float, default=0.0)
    viewed: Mapped[int] = mapped_column(Integer, default=0)

    manufacturer: Mapped["Manufacturer"] = relationship("Manufacturer")
    categories: Mapped[list["ProductCategory"]] = relationship("ProductCategory", back_populates="product", cascade="all, delete-orphan")
    images: Mapped[list["ProductImage"]] = relationship("ProductImage", back_populates="product", cascade="all, delete-orphan")
    attributes: Mapped[list["ProductAttribute"]] = relationship("ProductAttribute", back_populates="product", cascade="all, delete-orphan")
    product_links: Mapped[list["ProductStore"]] = relationship(
        "ProductStore", back_populates="product", cascade="all, delete-orphan"
    )
class ProductCategory(Base):
    __tablename__ = "product_category"

    product_category_id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    product_id: Mapped[int] = mapped_column(Integer, ForeignKey("product.product_id"), nullable=False)
    category_id: Mapped[int] = mapped_column(Integer, ForeignKey("category.category_id"), nullable=False)

    __table_args__ = (
        UniqueConstraint("product_id", "category_id", name="uq_product_category"),
    )

    product: Mapped["Product"] = relationship("Product", back_populates="categories")
    category: Mapped["Category"] = relationship("Category")

class ProductImage(Base):
    __tablename__ = "product_image"

    product_image_id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    product_id: Mapped[int] = mapped_column(Integer, ForeignKey("product.product_id"), nullable=False)
    image: Mapped[str] = mapped_column(String(255), nullable=False)
    sort_order: Mapped[int] = mapped_column(Integer, default=0)

    product: Mapped["Product"] = relationship("Product", back_populates="images")

class ProductAttribute(Base):
    __tablename__ = "product_attribute"

    product_attribute_id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    product_id: Mapped[int] = mapped_column(Integer, ForeignKey("product.product_id"), nullable=False)
    group_name: Mapped[str] = mapped_column(String(255), nullable=False)
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    value: Mapped[str] = mapped_column(String(255), nullable=False)
    sort_order: Mapped[int] = mapped_column(Integer, default=0)

    product: Mapped["Product"] = relationship("Product", back_populates="attributes")

class FilterGroup(Base):
    __tablename__ = "filter_group"
    filter_group_id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    sort_order: Mapped[int] = mapped_column(Integer, default=0)

class Filter(Base):
    __tablename__ = "filter"
    filter_id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    filter_group_id: Mapped[int] = mapped_column(Integer, nullable=False)
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    sort_order: Mapped[int] = mapped_column(Integer, default=0)

class ProductFilter(Base):
    __tablename__ = "product_filter"
    product_filter_id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    product_id: Mapped[int] = mapped_column(Integer, nullable=False)
    filter_id: Mapped[int] = mapped_column(Integer, nullable=False)