from .connection import PhaseDBConnection

class BaseModel:
    """
    Base class for all PhaseDB models.
    Requires a PhaseDBConnection object.
    """
    table_name = ""
    columns = []  # List of column names for insert/update

    def __init__(self, db: PhaseDBConnection):
        self.db = db
        # Ensure table exists
        if self.table_name and self.columns:
            col_defs = [(col, "TEXT") for col in self.columns]  # default to TEXT
            self.db.create_table(self.table_name, col_defs)

    def insert(self, **kwargs):
        """
        Insert a row using kwargs for columns.
        """
        return self.db.insert(self.table_name, kwargs)

    def select(self, where=None):
        """
        Select rows from the table.
        where = tuple: ("column = ?", (value,))
        """
        return self.db.select(self.table_name, where)

    def update(self, updates, where=None):
        """
        Update rows in the table.
        updates = dict of column:value
        """
        return self.db.update(self.table_name, updates, where)

    def delete(self, where=None):
        """
        Delete rows from the table.
        where = tuple: ("column = ?", (value,))
        """
        return self.db.delete(self.table_name, where)


# Example usage: define a model
class User(BaseModel):
    table_name = "users"
    columns = ["name", "coins", "level"]

class Inventory(BaseModel):
    table_name = "inventory"
    columns = ["user_id", "item_name", "amount"]