U
    [Mhj                     @   s6  d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ej
ej
ej
e ddlZddlmZmZ ddlZddlmZ e	  dd	 Zd
d Zedkr2ed ed edZesed ed ed ed edsed ede  ed ed ede  e  e  dS )z
PostgreSQL database setup script for the timesheet application
This script creates the necessary tables in your existing PostgreSQL database
    N)create_enginetext)sessionmaker)load_dotenv)Baseengine)
UserCreatec            	   
   C   s  t d z(t .} | td}| d }t d|  W 5 Q R X t d tjjtd t d t	ddtd	}| }zz|tj }t d
| d |dkrt d tdddd}t||}t d|j d|j d t d nt d W 5 |
  X t d t d t d t d t d t d W n\ tk
r } z<t d|  t d t d t d t d  td! W 5 d"}~X Y nX d"S )#z1Set up the database tables and create a test userz;Setting up PostgreSQL database for Timesheet application...zSELECT version()r   zConnected to PostgreSQL: zCreating database tables...)bindu    ✅ Tables created successfully!F)Z
autocommitZ	autoflushr	   Found z existing users in databasezCreating test user...Zadminzadmin@timesheet.comZadmin123)usernameZemailZpasswordu   ✅ Test user created: z (ID: )z6You can login with username: admin, password: admin123zUsers already exist in databaseu,   
🎉 Database setup completed successfully!z
Next steps:z@1. Update your .env file with your PostgreSQL connection string:zJ   DATABASE_URL=postgresql://username:password@localhost/your_timesheet_dbz22. Start the backend server: python run_backend.pyz83. Start the frontend: npm start (in frontend directory)u   ❌ Error setting up database: z
Troubleshooting:z"1. Make sure PostgreSQL is runningz'2. Check your DATABASE_URL in .env filez=3. Ensure the database exists and you have proper permissions   N)printr   connectexecuter   Zfetchoner   ZmetadataZ
create_allr   closeZquerymodelsZUsercountr   crudZcreate_userr   id	Exceptionsysexit)	connresultversionZSessionLocalZdbZexisting_usersZ	test_userusere r   ./setup_postgres.pysetup_database   sN    


r    c               
      s  t d zt } | td}dd | D }|rt dt| d |D ]}t d|  qPdd	g  fd
d|D }|rt d|  td}| dkrt d t	
d nt d W 5 Q R X W n0 tk
r } zt d|  W 5 d}~X Y nX dS )z/Check what tables already exist in the databasezChecking existing tables...z
                SELECT table_name 
                FROM information_schema.tables 
                WHERE table_schema = 'public'
                ORDER BY table_name
            c                 S   s   g | ]}|d  qS )r   r   ).0rowr   r   r   
<listcomp>\   s     z)check_existing_tables.<locals>.<listcomp>r
   z existing tables:z  - ZusersZactivity_recordsc                    s   g | ]}| kr|qS r   r   )r!   tZ
our_tablesr   r   r#   e   s      z!
Timesheet tables already exist: zBDo you want to continue? This will not drop existing data. (y/N): yzSetup cancelled.r   zNo tables found in databasez*Warning: Could not check existing tables: N)r   r   r   r   r   Zfetchallleninputlowerr   r   r   )r   r   ZtablestableZexisting_our_tablesZresponser   r   r%   r   check_existing_tablesN   s(    
r+   __main__z3PostgreSQL Database Setup for Timesheet Applicationz2==================================================ZDATABASE_URLu3   ❌ DATABASE_URL not found in environment variableszAPlease create a .env file with your PostgreSQL connection string:zCDATABASE_URL=postgresql://username:password@localhost/your_databaser   Z
postgresqluE   ❌ DATABASE_URL does not appear to be a PostgreSQL connection stringzCurrent DATABASE_URL: zGExpected format: postgresql://username:password@localhost/database_namezDatabase URL: )__doc__osr   Z
sqlalchemyr   r   Zsqlalchemy.ormr   Zdotenvr   pathappenddirnameabspath__file__r   Zdatabaser   r   r   Zschemasr   r    r+   __name__r   getenvZdatabase_urlr   
startswithr   r   r   r   <module>   s<   8%



