from add_news_form import AddNewsForm
from db import DB
from flask import Flask, redirect, render_template, session
from login_form import LoginForm, SignUpForm
from news_model import NewsModel
from users_model import UsersModel

app = Flask(__name__)
app.config['SECRET_KEY'] = 'yandexlyceum_secret_key'
db = DB()
NewsModel(db.get_connection()).init_table()
UsersModel(db.get_connection()).init_table()


# http://127.0.0.1:8080/login

@app.route('/userbase')
def userbase():
    if 'username' not in session or session['username'] != 'god_himself':
        return redirect('/index')
    users = UsersModel(db.get_connection()).get_all()
    return render_template('index.html', news=users)

@app.route('/')
@app.route('/index')
def index():
    '''if 'username' not in session:
        username="Guest"'''
    news = NewsModel(db.get_connection()).get_all()
    return render_template('index.html', news=news)  # username=session['username']


@app.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user_name = form.username.data
        password = form.password.data
        user_model = UsersModel(db.get_connection())
        exists = user_model.exists(user_name, password)
        if exists[0]:
            session['username'] = user_name
            session['user_id'] = exists[1]
        else:
            return redirect("/signup")
        return redirect("/index")
    return render_template('login.html', title='Авторизация', form=form)


@app.route('/logout')
def logout():
    session.pop('username', 0)
    session.pop('user_id', 0)
    return redirect('/index')


@app.route('/profile')
def profile():
    pass


@app.route('/signup', methods=['GET', 'POST'])
def signup():
    form = SignUpForm()
    if form.validate_on_submit():
        user_name = form.username.data
        password = form.password.data
        user_model = UsersModel(db.get_connection())
        # exists = user_model.exists(user_name, password)
        user_model.insert(user_name, password)
        return redirect("/login")
    return render_template("signup.html", title="Регистрация", form=form)


@app.route('/add_news', methods=['GET', 'POST'])
def add_news():
    if 'username' not in session:
        return redirect('/login')
    form = AddNewsForm()
    if form.validate_on_submit():
        title = form.title.data
        content = form.content.data
        author = form.author.data
        nm = NewsModel(db.get_connection())
        nm.insert(title, content, author, session['user_id'])
        return redirect("/index")
    return render_template('add_news.html', title='Добавление новости', form=form) # username=session['username'])


@app.route('/delete_news/<int:news_id>', methods=['GET'])
def delete_news(news_id):
    if 'username' not in session:
        return redirect('/login')
    nm = NewsModel(db.get_connection())
    nm.delete(news_id)
    return redirect("/index")


if __name__ == '__main__':
    app.run(port=8080, host='127.0.0.1', debug=True)


