from datetime import datetime, timedelta

from course_manager import CourseManager
from login_manager import LoginManager, login_manager, db
import coursedb_manager
from usage_resource import UsageResource
from secret import sqlalchemy_url
from login import LoginResource

from flask import Flask
from flask_restful import Api

from flask_sqlalchemy import SQLAlchemy
from pymongo import MongoClient

## Setup
app = Flask(__name__)
api = Api(app)

login_manager.init_app(app)

app.config.update(
    SQLALCHEMY_DATABASE_URI = sqlalchemy_url,
    SQLALCHEMY_TRACK_MODIFICATIONS = True,
    SECRET_KEY = 'secret_xxx',
)

db.init_app(app)

mongo = MongoClient()

## API stuff

# CourseDB resources
api.add_resource(coursedb_manager.DepartmentResource, 
    '/<string:school>/courses',
    resource_class_kwargs={'client': mongo}
    )
api.add_resource(coursedb_manager.DepartmentListingResource, 
    '/<string:school>/courses/<string:dept>',
    resource_class_kwargs={'client': mongo}
    )
api.add_resource(coursedb_manager.CatalogCourseResource, 
    '/<string:school>/courses/<string:dept>/<int:num>',
    resource_class_kwargs={'client': mongo}
    )

# Login resources
api.add_resource(LoginManager.NewUserResource, '/useradd')
api.add_resource(LoginResource,   '/<string:school>/authorize')
api.add_resource(LoginManager.LogoutResource,  '/logout')
api.add_resource(LoginManager.DeleteUserResource, '/delete_user')

# How to use my lovely program
api.add_resource(UsageResource,   '/')

api.add_resource(CourseManager.ListStockYears,  
    '/<string:school>/stock_charts',
    resource_class_kwargs={'client': mongo}
    )
api.add_resource(CourseManager.ListStockCharts, 
    '/<string:school>/stock_charts/<string:year>',
    resource_class_kwargs={'client': mongo}
    )

api.add_resource(CourseManager.GetStockChart,   
    '/<string:school>/stock_charts/<string:year>/<string:major>',
    resource_class_kwargs={'client': mongo}
    )

api.add_resource(CourseManager.ListUserCharts,  '/<string:user>/charts')
api.add_resource(CourseManager.ChartResource,   '/<string:user>/charts/<string:chart>')
api.add_resource(CourseManager.CourseResource,  '/<string:user>/charts/<string:chart>/<int:c_id>')

# @app.before_first_request
# def create_database():
    # db.create_all()

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=4500)


@@ -665,16 +665,36 @@
 PI stuff
+%0AAPI_PREFIX = %22/api%22
 %0A%0A# Cour
@@ -759,33 +759,39 @@
 tResource, %0A    
-'
+f'%7Bapi%7D
 /%3Cstring:school%3E
@@ -909,33 +909,39 @@
 gResource, %0A    
-'
+f'%7Bapi%7D
 /%3Cstring:school%3E
@@ -1073,25 +1073,31 @@
 ource, %0A    
-'
+f'%7Bapi%7D
 /%3Cstring:sch
@@ -1230,80 +1230,27 @@
 ogin
-Manager.NewUserResource, '/useradd')%0Aapi.add_resource(LoginResource,   '
+Resource,   f'%7Bapi%7D
 /%3Cst
@@ -1325,82 +1325,22 @@
 e,  
-'/logout')%0Aapi.add_resource(LoginManager.DeleteUserResource, '/delete_user
+f'%7Bapi%7D/logout
 ')%0A%0A
@@ -1404,17 +1404,23 @@
 urce,   
-'
+f'%7Bapi%7D
 /')%0A%0Aapi
@@ -1465,25 +1465,31 @@
 ears,  %0A    
-'
+f'%7Bapi%7D
 /%3Cstring:sch
@@ -1607,25 +1607,31 @@
 harts, %0A    
-'
+f'%7Bapi%7D
 /%3Cstring:sch
@@ -1768,17 +1768,23 @@
    %0A    
-'
+f'%7Bapi%7D
 /%3Cstring
@@ -1935,17 +1935,23 @@
 harts,  
-'
+f'%7Bapi%7D
 /%3Cstring
@@ -2014,17 +2014,23 @@
 urce,   
-'
+f'%7Bapi%7D
 /%3Cstring
@@ -2108,17 +2108,23 @@
 ource,  
-'
+f'%7Bapi%7D
 /%3Cstring

Set up proper API url