"""
SQLAlchemy Models for RH Elektronik Website
"""
from sqlalchemy import Column, String, Integer, Float, Boolean, Text, DateTime, JSON
from sqlalchemy.sql import func
from database import Base
import uuid


def generate_uuid():
    return str(uuid.uuid4())


class Admin(Base):
    __tablename__ = "admins"
    
    id = Column(String(36), primary_key=True, default=generate_uuid)
    username = Column(String(100), unique=True, nullable=False, index=True)
    password_hash = Column(String(255), nullable=False)
    role = Column(String(50), default="admin")
    created_at = Column(DateTime(timezone=True), server_default=func.now())


class BlogPost(Base):
    __tablename__ = "blog_posts"
    
    id = Column(String(36), primary_key=True, default=generate_uuid)
    title = Column(String(255), nullable=False)
    slug = Column(String(255), unique=True, nullable=False, index=True)
    content = Column(Text, nullable=False)
    excerpt = Column(Text, default="")
    meta_description = Column(String(500), default="")
    meta_keywords = Column(String(500), default="")
    author = Column(String(100), default="Admin")
    featured_image = Column(String(500), default="")
    status = Column(String(50), default="published", index=True)
    views = Column(Integer, default=0)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())


class Service(Base):
    __tablename__ = "services"
    
    id = Column(String(36), primary_key=True, default=generate_uuid)
    title = Column(String(255), nullable=False)
    description = Column(Text, nullable=False)
    icon = Column(String(50), default="wrench")
    order_num = Column(Integer, default=0)


class Brand(Base):
    __tablename__ = "brands"
    
    id = Column(String(36), primary_key=True, default=generate_uuid)
    name = Column(String(255), nullable=False)
    logo_url = Column(String(500), nullable=False)
    alt_text = Column(String(255), default="")
    order_num = Column(Integer, default=0)


class Area(Base):
    __tablename__ = "areas"
    
    id = Column(String(36), primary_key=True, default=generate_uuid)
    province = Column(String(255), nullable=False)
    cities = Column(JSON, default=list)


class Contact(Base):
    __tablename__ = "contact"
    
    id = Column(String(36), primary_key=True, default="main")
    phone = Column(String(100), default="")
    email = Column(String(255), default="")
    address = Column(Text, default="")
    hours = Column(String(255), default="")
    map_embed = Column(Text, default="")


class AboutCard(Base):
    __tablename__ = "about_cards"
    
    id = Column(String(36), primary_key=True, default=generate_uuid)
    title = Column(String(255), nullable=False)
    description = Column(Text, nullable=False)
    order_num = Column(Integer, default=0)


class Product(Base):
    __tablename__ = "products"
    
    id = Column(String(36), primary_key=True, default=generate_uuid)
    name = Column(String(255), nullable=False)
    slug = Column(String(255), unique=True, nullable=False, index=True)
    description = Column(Text, nullable=False)
    price = Column(Float, default=0)
    original_price = Column(Float, default=0)
    category = Column(String(100), default="Spareparts", index=True)
    brand = Column(String(100), default="")
    images = Column(JSON, default=list)
    stock = Column(Integer, default=0)
    is_available = Column(Boolean, default=True)
    featured = Column(Boolean, default=False, index=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
