from pydantic import BaseModel, Field
from sqlalchemy.orm import Mapped, mapped_column, relationship, Session
from sqlalchemy import Integer, Enum, ForeignKey, VARCHAR
from enum import Enum as PyEnum
from app.models.base_class import Base

class PhysicalStoreFormatsBase(BaseModel):
    physical_id : int | None = Field(default=None)
    metric : str | None = Field(default=None)
    y1_y2 : int | None = Field(default=None)
    y3_y4 : int | None = Field(default=None)
    y5_y6 : int | None = Field(default=None)
    group_id : int | None = Field(default=None)

class MetricEnum(PyEnum):
    INVESTMENT_AND_CAPITAL_EXPENSES = "Investment & Capital Expenses"
    PRE_OPERATING_EXPENSES = "Pre-Operating Expenses"
    RENTAL_ADVANCE_DEPOSIT = "Rental Advance (Deposit)"
    RENT = "Rent"
    INVENTORY = "Inventory"
    BILLS_PER_MONTH = "Bills per month"
    AVERAGE_PRICE_PER_ITEM = "Average Price per item"
    OTHER_INCOME = "Other Income"
    UTILITIES = "Utilities"
    CORPORATE_EMPLOYEE_SALARY = "Corporate Employee Salary"
    CONCEPT_EMPLOYEE_SALARY = "Concept Employee Salary"
    STORE_LEVEL_EMPLOYEE_SALARY = "Store Level Employee Salary"
    LOGISTICS_AND_TRANSPORTATION = "Logistics and Transportation"
    
class TblPhysicalStoreFormats(Base):
    __tablename__ = "tbl_physical_store_formats"
    physical_id : Mapped[int] = mapped_column("physical_id", Integer, primary_key=True, autoincrement=True)
    metric : Mapped[str] = mapped_column("metric", VARCHAR(255), Enum(MetricEnum))
    y1_y2 : Mapped[int] = mapped_column("y1_y2", Integer, nullable=True, server_default=None)
    y3_y4 : Mapped[int] = mapped_column("y3_y4", Integer, nullable=True, server_default=None)
    y5_y6 : Mapped[int] = mapped_column("y5_y6", Integer, nullable=True, server_default=None)
    group_id : Mapped[int] = mapped_column("group_id", ForeignKey("tbl_group.group_id"))
    
    group = relationship("TblGroup", back_populates="physical_store_formats")
    
    @classmethod
    def create(cls, data:PhysicalStoreFormatsBase, db:Session) ->"TblPhysicalStoreFormats":
        create_data = cls(**data.model_dump())
        db.add(create_data)
        db.flush()
        return create_data
    
    @classmethod
    def get(cls, group_id:int, db:Session) ->"TblPhysicalStoreFormats":
        get_data = db.query(cls).filter(cls.group_id == group_id).all()
        return get_data
    
    @classmethod
    def update(cls, physical_id:int, data:PhysicalStoreFormatsBase, db:Session) ->"TblPhysicalStoreFormats":
        update_data = db.query(cls).filter(cls.physical_id == physical_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(cls, physical_id:int, db:Session) ->"TblPhysicalStoreFormats":
        delete_data = db.query(cls).filter(cls.physical_id == physical_id).first()
        if not delete_data:
            return False
        db.delete(delete_data)
        db.commit()
        return True

    