from datetime import datetime
import uuid
from sqlalchemy import UUID, BigInteger, Boolean, Column, DateTime, Enum, ForeignKey, Integer, String
from src.models.company_user import User
from src.constants import ChartTypeEnum, FontSizeEnum, TicketPriorityEnum, TicketStatusEnum
from src.core.database import Base
from sqlalchemy.orm import relationship


class AppearanceThemeSettings(Base):
    __tablename__ = "appearance_theme_settings"
    id = Column(BigInteger, primary_key=True,autoincrement=True)
    primary_color = Column(String, nullable=False)
    background_color = Column(String, nullable=False)
    font_color = Column(String, nullable=False)
    font_size = Column(Enum(FontSizeEnum), nullable=False,default=FontSizeEnum.SMALL.value)#SMALL =0, MEDIUM=1, LARGE=2
    font_family = Column(String, nullable=False)
    company_id= Column(String, nullable=False,unique=True)

class IpAddress(Base):
    __tablename__="ip_addresses"
    id=Column(BigInteger,primary_key=True,autoincrement=True)
    company_id=Column(String,nullable=False)
    allowed_ipaddress=Column(String,nullable=False,default="*")



class Ticket(Base):
    __tablename__ = "tickets"

    id = Column(Integer, primary_key=True, autoincrement=True)
    ticket_id = Column(String, unique=True, index=True)

    company_id = Column(String, nullable=False)
    user_id = Column(String, nullable=False)
    email = Column(String, nullable=False)
    issue_type = Column(String, nullable=False)
    title = Column(String, nullable=False)
    description = Column(String, nullable=False)
    priority = Column(Enum(TicketPriorityEnum),default=TicketPriorityEnum.LOW, nullable=False)
    status = Column(Enum(TicketStatusEnum),default=TicketStatusEnum.OPEN, nullable=True)

    

    created_at = Column(
    DateTime(timezone=True),  # ensure column supports timezones
    default=datetime.utcnow,
    nullable=False
)

    updated_at = Column(
    DateTime(timezone=True),
default=datetime.utcnow,
    onupdate=datetime.utcnow,
    nullable=False
)
    # Relationship to attachments
    attachments = relationship("Attachment", back_populates="ticket", cascade="all, delete-orphan")

    def generate_ticket_id(self):
        if self.id is not None:
            self.ticket_id = f"TCKT-{str(self.id).zfill(6)}"


class Attachment(Base):
    __tablename__ = "attachments"

    id = Column(Integer, primary_key=True, autoincrement=True)
    ticket_id = Column(Integer, ForeignKey('tickets.id'), nullable=False)
    file_path = Column(String, nullable=False)  # Path to file or image
    uploaded_at = Column(DateTime, default=datetime.utcnow, nullable=False)

    # Relationship back to the ticket
    ticket = relationship("Ticket", back_populates="attachments")



# Optional: One row per user per notification type
class EmailNotificationSettings(Base):
    __tablename__ = "email_notification_settings"

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    user_id = Column(String, ForeignKey("users.id"), unique=True, nullable=False)

    transaction_alert = Column(Boolean, default=True)
    report_ready = Column(Boolean, default=True)
    system_update = Column(Boolean, default=True)
    weekly_summary = Column(Boolean, default=True)

    user = relationship("User", back_populates="email_settings")


User.email_settings = relationship(
        "EmailNotificationSettings", back_populates="user", uselist=False, cascade="all, delete-orphan"
    )



