from typing import Literal
from pydantic import BaseModel, Field
from app.models.base_class import Base
from sqlalchemy.orm import Mapped,mapped_column,relationship,Session
from sqlalchemy import ForeignKey, Integer,VARCHAR,Enum, String

class InfoTechBase(BaseModel):
    
    infotech_id : int | None = Field(default=None)
    type : Literal["Cash Tills/POS Equipment", "Scanner", "Computer"]
    units : int | None = Field(default=None)
    cost_per_unit : int | None = Field(default=None)
    total : int | None = Field(default=None)
    store_formate_type : Literal["Store Formate A", "Store Formate B"] = "Store Formate A"
    category : Literal["Software", "Hardware", "Physical Component"]
    remark : str | None = Field(default=None)
    group_id : int | None = Field(default=None)
    

class TblInfoTech(Base):
    
    __tablename__ = "tbl_infotech"
    
    infotech_id : Mapped[int] = mapped_column("infotech_id", Integer, primary_key=True, autoincrement=True)
    type : Mapped[str] = mapped_column(Enum("Cash Tills/POS Equipment", "Scanner", "Computer"))
    units : Mapped[int] = mapped_column("units", Integer,nullable=True, server_default=None)
    cost_per_unit : Mapped[int] = mapped_column("cost_per_unit", Integer, nullable=True, server_default=None)
    total : Mapped[int] = mapped_column("total", Integer, nullable=True, server_default=None)
    store_formate_type : Mapped[str] = mapped_column(Enum("Store Formate A", "Store Formate B"))
    remark : Mapped[str] = mapped_column("remark", VARCHAR(255), nullable=True, server_default=None)
    category : Mapped[str] = mapped_column("category", Enum("Software", "Hardware", "Physical Component"))
    group_id : Mapped[int] = mapped_column("group_id", Integer, ForeignKey("tbl_group.group_id"), nullable=True)
    
    group = relationship("TblGroup", back_populates="info_tech")
    
    @classmethod
    def create(cls, data:InfoTechBase, db:Session) ->"TblInfoTech":
        data_dict = data.model_dump()
        new_data = cls(**data_dict)
        db.add(new_data)
        db.flush()
        return new_data
    
    @classmethod
    def get_info_tech_data(cls, infotech_get_id:int, db:Session) ->"TblInfoTech":
        get_data = db.query(cls).filter(cls.infotech_id == infotech_get_id).first()
        return get_data
    
    @classmethod
    def update_info_tech(cls, info_tech_update_id:int, data:InfoTechBase, db:Session) ->"TblInfoTech":
        update_data = db.query(cls).filter(cls.infotech_id == info_tech_update_id).first()
        data_dict = data.model_dump()
        for key, value in data_dict.items():
            if value is not None:
                setattr(update_data, key, value)
        db.commit()
        db.refresh(update_data)
        return update_data
    
    @classmethod
    def delete_info_tech(cls, info_tech_delete_id:int, db:Session) ->"TblInfoTech":
        delete_data = db.query(cls).filter(cls.infotech_id == info_tech_delete_id).first()
        if not delete_data:
            return False
        db.delete(delete_data)
        db.commit()
        return True
    
    
     
    

    