import json

from flask import Blueprint, Response, jsonify, render_template, url_for
from StageHubAPI import Api, Resource, app, ma, SQLAlchemyAutoSchema
from StageHubAPI.model import Project
from StageHubAPI.response import ApiResponse

project_bp = Blueprint('project', __name__)
api = Api(project_bp)


class Schema(SQLAlchemyAutoSchema):
    class Meta:
        model = Project
        include_relationships = True
        include_fk = True
        load_instance = True


project_schema = Schema()
projects_schema = Schema(many=True)


class ProjectList(Resource):
    def get(self):
        return projects_schema.dump(Project.query.all()), 200


class ProjectByName(Resource):
    def get(self, name):
        p = Project.query.filter(Project.name.like(name)).first()
        return project_schema.dump(p)


class ProjectById(Resource):
    def get(self, id):
        p = Project.query.filter_by(id=id).first()
        return project_schema.dump(p)


api.add_resource(ProjectList, '/projects')
api.add_resource(ProjectByName, '/projects/<string:name>')
api.add_resource(ProjectById, '/projects/<int:id>')

# @project_bp.route('/projects/')
# def projects():
#     projects = Project.query.all()
#     r = ApiResponse(objects=projects)
#     r.message = 'success'
#     return r.as_response()

# @project_bp.route('/projects/<string:project_name>')
# def project(project_name):
#     p = Project.query.filter_by(name=project_name).first()
#     r = ApiResponse(objects=[p])
#     r.message = 'success'
#     return r.as_response()
