
from Models.UserModel import UserModel
from Models.PostModel import PostModel
from Models.MediaModel import MediaModel
from Models.ProjectModel import moderator_project
from Rest.Answer import Answer, MessageCodes
from app import db


class ReportREST:
    @staticmethod
    def post_report_user(user, user_to_report_id, message):
        answer = Answer()
        user_to_report = UserModel.query.get(user_to_report_id)
        if not user_to_report:
            return answer.add_message(MessageCodes.USER_NO_SUCH_USER)
        post = PostModel(channel=moderator_project().channel,
                         author=UserModel.matchinit_user(),
                         content=f'@{user.username} reported @{user_to_report.username}:\n"{message}"')
        db.session.add(post)
        db.session.commit()
        return answer.add_message(MessageCodes.WORKED)

    @staticmethod
    def post_report_post(user, post_to_report_id, message):
        answer = Answer()
        post_to_report = PostModel.query.get(post_to_report_id)
        if not post_to_report:
            return answer.add_message(MessageCodes.CHANNEL_NO_SUCH_POST)
        post = PostModel(channel=moderator_project().channel,
                         author=UserModel.matchinit_user(),
                         content=f'@{user.username} reported a post of @{post_to_report.author.username}:\n' +
                                 f'id: {post_to_report_id}\n' +
                                 f'"{message}"\n' +
                                 f'the content of this post was:\n' +
                                 f'"{post_to_report.content}"')
        db.session.add(post)
        db.session.commit()
        return answer.add_message(MessageCodes.WORKED)

    @staticmethod
    def post_report_media(user, media_to_report_id, message):
        answer = Answer()
        media_to_report = MediaModel.query.get(media_to_report_id)
        if not media_to_report:
            return answer.add_message(MessageCodes.PROJECT_NO_SUCH_MEDIA)
        post = PostModel(channel=moderator_project().channel,
                         author=UserModel.matchinit_user(),
                         content=f'@{user.username} reported a media of @{media_to_report.submitter.username}:\n' +
                                 f'id: {media_to_report_id}\n' +
                                 f'"{message}"\n\n' +
                                 f'The reported Media:\n' +
                                 f'title:\n' +
                                 f'"{media_to_report.title}"\n' +
                                 f'link:\n' +
                                 f'"{media_to_report.link}"\n' +
                                 f'description:\n' +
                                 f'"{media_to_report.description}"')
        db.session.add(post)
        db.session.commit()
        return answer.add_message(MessageCodes.WORKED)
