"""
品牌和供应商数据模型
"""
from datetime import datetime
from sqlalchemy import Column, BigInteger, String, Integer, Text, ForeignKey, DateTime
from sqlalchemy.orm import relationship
from app.db.base import Base


class Brand(Base):
    """品牌表"""

    __tablename__ = "brands"

    id = Column(BigInteger, primary_key=True, index=True)
    brand_code = Column(String(50), unique=True, nullable=False, index=True, comment="品牌代码")
    brand_name = Column(String(200), nullable=False, comment="品牌名称")
    logo_url = Column(String(500), nullable=True, comment="Logo URL")
    website = Column(String(500), nullable=True, comment="官网地址")
    status = Column(String(20), default="active", nullable=False, comment="状态: active, inactive")
    sort_order = Column(Integer, default=0, nullable=False, comment="排序")
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    created_by = Column(BigInteger, ForeignKey("users.id"), nullable=True)
    updated_by = Column(BigInteger, ForeignKey("users.id"), nullable=True)
    deleted_at = Column(DateTime, nullable=True)
    deleted_by = Column(BigInteger, ForeignKey("users.id"), nullable=True)

    # 关系
    created_user = relationship("User", foreign_keys=[created_by])
    updated_user = relationship("User", foreign_keys=[updated_by])
    deleted_user = relationship("User", foreign_keys=[deleted_by])
    assets = relationship("Asset", back_populates="brand")

    def __repr__(self):
        return f"<Brand(id={self.id}, brand_code={self.brand_code}, brand_name={self.brand_name})>"


class Supplier(Base):
    """供应商表"""

    __tablename__ = "suppliers"

    id = Column(BigInteger, primary_key=True, index=True)
    supplier_code = Column(String(50), unique=True, nullable=False, index=True, comment="供应商代码")
    supplier_name = Column(String(200), nullable=False, comment="供应商名称")
    contact_person = Column(String(100), nullable=True, comment="联系人")
    contact_phone = Column(String(20), nullable=True, comment="联系电话")
    email = Column(String(255), nullable=True, comment="邮箱")
    address = Column(String(500), nullable=True, comment="地址")
    credit_code = Column(String(50), nullable=True, comment="统一社会信用代码")
    bank_name = Column(String(200), nullable=True, comment="开户银行")
    bank_account = Column(String(100), nullable=True, comment="银行账号")
    status = Column(String(20), default="active", nullable=False, comment="状态: active, inactive")
    remark = Column(Text, nullable=True, comment="备注")
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    created_by = Column(BigInteger, ForeignKey("users.id"), nullable=True)
    updated_by = Column(BigInteger, ForeignKey("users.id"), nullable=True)
    deleted_at = Column(DateTime, nullable=True)
    deleted_by = Column(BigInteger, ForeignKey("users.id"), nullable=True)

    # 关系
    created_user = relationship("User", foreign_keys=[created_by])
    updated_user = relationship("User", foreign_keys=[updated_by])
    deleted_user = relationship("User", foreign_keys=[deleted_by])
    assets = relationship("Asset", back_populates="supplier")

    def __repr__(self):
        return f"<Supplier(id={self.id}, supplier_code={self.supplier_code}, supplier_name={self.supplier_name})>"
