import os
from flask import Flask, jsonify, request
from flasgger import Swagger
from flask_sqlalchemy import SQLAlchemy

from models import db, User, TimeStamp


app = Flask(__name__)
api = Swagger(app)

app.config['SECRET_KEY'] = 'dksldkkwkdkllk' # todo: os.environ['TIMESTAMP_SECRET_KEY']
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///project.db"

@app.route("/")
def index_handler():
    return "hallo"


# USER SECTION

@app.route("/user/", methods=['POST'])
def create_user():
    """Endpoint returning success of operation
            ---
            parameters:
              - name: user id to delete
                in: path
                type: string
                required: true
              - name: user
                in: path
                type: string
                required: true
            definitions:
              Palette:
                type: object
                properties:
                  palette_name:
                    type: array
                    items:
                      $ref: '#/definitions/Color'
              Color:
                type: string
            responses:
              200:
                description: A list of colors (may be filtered by palette)
                schema:
                  $ref: '#/definitions/Palette'
                examples:
                  rgb: ['red', 'green', 'blue']
            """
    user = User(
        name=request.form["username"],
        email=request.form["email"],
    )
    db.session.add(user)
    db.session.commit()


@app.route("/user/<int:user_id>", methods=['GET'])
def get_user_by_id(user_id):
    """Endpoint returning user by id
            ---
            parameters:
              - name: user_id
                type: integer
                in: form
                required: true

            responses:
              200:
                description: A list of properties of a user by given id
            """
    user = db.get_or_404(User, user_id)
    return jsonify(str(user))


@app.route("/user/<int:user_id>", methods=['DELETE'])
def delete_user_by_id(user_id):
    """Endpoint returning success of operation
        ---
        parameters:
          - name: user id to delete
            in: path
            type: string
            enum: ['all', 'rgb', 'cmyk']
            required: true
            default: all
        definitions:
          Palette:
            type: object
            properties:
              palette_name:
                type: array
                items:
                  $ref: '#/definitions/Color'
          Color:
            type: string
        responses:
          200:
            description: A list of colors (may be filtered by palette)
            schema:
              $ref: '#/definitions/Palette'
            examples:
              rgb: ['red', 'green', 'blue']
        """
    return "Delete"


#END USER SECTION
#TIMESTAMP SECTION
@app.route("/timestamp/<int:user_id>", methods=['POST'])
def create_timestamp(user_id):
    """Endpoint returning success of operation
            ---
            parameters:
              - name: user id to delete
                in: path
                type: string
                enum: ['all', 'rgb', 'cmyk']
                required: true
                default: all
            definitions:
              Palette:
                type: object
                properties:
                  palette_name:
                    type: array
                    items:
                      $ref: '#/definitions/Color'
              Color:
                type: string
            responses:
              200:
                description: A list of colors (may be filtered by palette)
                schema:
                  $ref: '#/definitions/Palette'
                examples:
                  rgb: ['red', 'green', 'blue']
            """
    pass


@app.route("/timestamp/<int:user_id>", methods=['GET'])
def get_timestamps_by_id(user_id):
    """Endpoint returning user by id
            ---
            parameters:
              - name: user id
                in: path
                type: integer
                required: true
            definitions:
              Palette:
                type: object
                properties:
                  palette_name:
                    type: array
                    items:
                      $ref: '#/definitions/Color'
              Color:
                type: string
            responses:
              200:
                description: A list of properties of a user by given id
                schema:
                  $ref: '#/definitions/Palette'
                examples:
                  rgb: ['red', 'green', 'blue']
            """
    return str(user_id)


@app.route("/timestamp/<int:user_id>", methods=['DELETE'])
def delete_timestamps_by_id(user_id):
    """Endpoint returning success of operation
        ---
        parameters:
          - name: user id to delete
            in: path
            type: string
            enum: ['all', 'rgb', 'cmyk']
            required: true
            default: all
        definitions:
          Palette:
            type: object
            properties:
              palette_name:
                type: array
                items:
                  $ref: '#/definitions/Color'
          Color:
            type: string
        responses:
          200:
            description: A list of colors (may be filtered by palette)
            schema:
              $ref: '#/definitions/Palette'
            examples:
              rgb: ['red', 'green', 'blue']
        """
    return "Delete"

#END TIMESTAMP SECTION


db.init_app(app)

with app.app_context():
    db.create_all()

if __name__ == '__main__':
    app.run(debug=True)
