o
    XJh.                     @   s   d Z ddlZddlmZ ddlmZmZmZmZ ddlm	Z	 ddl
Z
e	G dd dZe	G dd	 d	Ze	G d
d dZe	G dd dZG dd dZdS )zb
Database models for the MAB Recommendation System.
Defines the structure of all database tables.
    N)datetime)OptionalListDictAny)	dataclassc                   @   sT   e Zd ZU dZeed< eed< eed< eed< dZe	ed< de
eef fd	d
ZdS )
FeedbackDBz)User interaction feedback database model.user_id
product_id
event_typesessionTactive_userreturnc                 C   s(   | j | j| j| jr| j nd | jdS )N)r	   r
   r   r   r   )r	   r
   r   r   	isoformatr   self r   J/Users/divyeshpatel/Desktop/sahana/Recommender/recommender_rl/db/models.pyto_dict   s   zFeedbackDB.to_dictN)__name__
__module____qualname____doc__int__annotations__strr   r   boolr   r   r   r   r   r   r   r      s   
 r   c                   @   sp   e Zd ZU dZeed< eed< eed< eed< eed< dZe	e ed< dZ
e	e ed	< d
eeef fddZdS )ProductCatalogzProduct catalog database model.r
   product_name
brand_namecategorypriceNdescription	image_urlr   c                 C   s"   | j | j| j| j| j| j| jdS )Nr
   r   r   r    r!   r"   r#   r$   r   r   r   r   r   +   s   zProductCatalog.to_dict)r   r   r   r   r   r   r   floatr"   r   r#   r   r   r   r   r   r   r   r       s   
 r   c                   @   s   e Zd ZU dZeed< eed< dZee ed< dZ	ee ed< dZ
ee ed< dZee ed< dZee ed	< d
eeef fddZdS )UserDemographicsz-User demographics and profile database model.r	   nameNgenderageuser_cluster_id
created_atlast_activer   c                 C   sB   | j | j| j| j| j| jr| j nd | jr| j dS d dS )N)r	   r'   r(   r)   r*   r+   r,   )r	   r'   r(   r)   r*   r+   r   r,   r   r   r   r   r   B   s   zUserDemographics.to_dict)r   r   r   r   r   r   r   r(   r   r)   r*   r+   r   r,   r   r   r   r   r   r   r   r&   7   s   
 r&   c                   @   s`   e Zd ZU dZeed< eed< eed< dZee ed< dZ	ee
 ed< deeef fd	d
ZdS )SearchHistoryz#User search history database model.r	   query
session_idNr
   	timestampr   c                 C   s,   | j | j| j| j| jr| j dS d dS )N)r	   r.   r
   r/   r0   )r	   r.   r
   r/   r0   r   r   r   r   r   r   W   s   zSearchHistory.to_dict)r   r   r   r   r   r   r   r
   r   r0   r   r   r   r   r   r   r   r   r-   N   s   
 r-   c                   @   sX   e Zd ZdZddefddZdejfddZd	ejfd
dZ	dd Z
dd Zdd ZdS )DatabaseManagerz7Manages SQLite database connections and initialization.recommender_system.dbdb_pathc                 C   s
   || _ d S N)r3   )r   r3   r   r   r   __init__d   s   
zDatabaseManager.__init__r   c                 C   s   t | j}t j|_|S )z3Create a new database connection for thread safety.)sqlite3connectr3   ZRowZrow_factoryr   
connectionr   r   r   r7   g   s   zDatabaseManager.connectr9   c                 C   s   |r|   dS dS )z%Close a specific database connection.N)closer8   r   r   r   r:   m   s   zDatabaseManager.closec                 C   s   |   }| }|d |d |d |d |d |d |d |d |d	 |d
 |d |d |d |  td dS )zInitialize database tables.a8  
            CREATE TABLE IF NOT EXISTS feedback_db (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                user_id INTEGER NOT NULL,
                product_id TEXT NOT NULL,
                event_type TEXT NOT NULL,
                session DATETIME NOT NULL,
                active_user BOOLEAN DEFAULT 1,
                created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                FOREIGN KEY (user_id) REFERENCES user_demographics (user_id),
                FOREIGN KEY (product_id) REFERENCES product_catalog (product_id)
            )
        a  
            CREATE TABLE IF NOT EXISTS product_catalog (
                product_id INTEGER PRIMARY KEY,
                product_name TEXT NOT NULL,
                brand_name TEXT NOT NULL,
                category TEXT NOT NULL,
                price REAL NOT NULL,
                description TEXT,
                image_url TEXT,
                created_at DATETIME DEFAULT CURRENT_TIMESTAMP
            )
        a  
            CREATE TABLE IF NOT EXISTS user_demographics (
                user_id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT NOT NULL,
                gender TEXT,
                age TEXT,
                user_cluster_id INTEGER,
                created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                last_active DATETIME DEFAULT CURRENT_TIMESTAMP
            )
        a  
            CREATE TABLE IF NOT EXISTS search_history (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                user_id INTEGER NOT NULL,
                query TEXT NOT NULL,
                product_id INTEGER,
                session_id INTEGER NOT NULL,
                timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
                FOREIGN KEY (user_id) REFERENCES user_demographics (user_id),
                FOREIGN KEY (product_id) REFERENCES product_catalog (product_id)
            )
        zHCREATE INDEX IF NOT EXISTS idx_feedback_user_id ON feedback_db (user_id)zNCREATE INDEX IF NOT EXISTS idx_feedback_product_id ON feedback_db (product_id)zNCREATE INDEX IF NOT EXISTS idx_feedback_event_type ON feedback_db (event_type)zHCREATE INDEX IF NOT EXISTS idx_feedback_session ON feedback_db (session)zMCREATE INDEX IF NOT EXISTS idx_product_category ON product_catalog (category)zLCREATE INDEX IF NOT EXISTS idx_product_brand ON product_catalog (brand_name)zRCREATE INDEX IF NOT EXISTS idx_user_cluster ON user_demographics (user_cluster_id)zICREATE INDEX IF NOT EXISTS idx_search_user_id ON search_history (user_id)zOCREATE INDEX IF NOT EXISTS idx_search_session_id ON search_history (session_id)u,   ✅ Database tables initialized successfullyN)r7   cursorexecutecommitprint)r   connr;   r   r   r   init_databaser   s"   












zDatabaseManager.init_databasec                 C   s   |   | _| jS r4   )r7   _current_connectionr   r   r   r   	__enter__   s   
zDatabaseManager.__enter__c                 C   s   t | dr| | j d S d S )NrA   )hasattrr:   rA   )r   exc_typeexc_valexc_tbr   r   r   __exit__   s   
zDatabaseManager.__exit__N)r2   )r   r   r   r   r   r5   r6   
Connectionr7   r:   r@   rB   rG   r   r   r   r   r1   a   s    Kr1   )r   r6   r   typingr   r   r   r   dataclassesr   jsonr   r   r&   r-   r1   r   r   r   r   <module>   s    