from datetime import datetime
from sqlalchemy import Boolean, Column, String, Integer, DateTime, Enum
from sqlalchemy.sql import func

from app.db.session import Base


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(50), unique=True, index=True, nullable=False)
    password_hash = Column(String(255), nullable=False)
    user_type = Column(Enum('farmer', 'merchant', 'admin', 'super_admin'), default='farmer')
    real_name = Column(String(50))
    nickname = Column(String(50))
    avatar_url = Column(String(255))
    email = Column(String(100), unique=True, index=True)
    phone = Column(String(20), unique=True, index=True)
    gender = Column(Enum('male', 'female', 'other'), default='other')
    birthday = Column(DateTime)
    status = Column(Enum('active', 'inactive'), default='active')
    wechat_openid = Column(String(100), unique=True, index=True)
    wechat_unionid = Column(String(100), unique=True, index=True)
    level = Column(Integer, default=1)
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
    last_login = Column(DateTime)


class Admin(Base):
    __tablename__ = "admins"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(50), unique=True, index=True, nullable=False)
    password = Column(String(255), nullable=False)
    email = Column(String(100), unique=True, index=True)
    nickname = Column(String(50))
    avatar = Column(String(255))
    role = Column(Enum('admin', 'super_admin'), default='admin')
    status = Column(Enum('active', 'inactive'), default='active')
    last_login = Column(DateTime)
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())