# users:
# +----------+--------------+------+-----+---------+----------------+
# | Field    | Type         | Null | Key | Default | Extra          |
# +----------+--------------+------+-----+---------+----------------+
# | userID   | int(11)      | NO   | PRI | NULL    | auto_increment |
# | userName | varchar(255) | NO   |     | NULL    |                |
# +----------+--------------+------+-----+---------+----------------+
#
# posts:
# +----------+--------------+------+-----+---------+----------------+
# | Field    | Type         | Null | Key | Default | Extra          |
# +----------+--------------+------+-----+---------+----------------+
# | postID   | int(11)      | NO   | PRI | NULL    | auto_increment |
# | userID   | int(11)      | YES  | MUL | NULL    |                |
# | title    | varchar(255) | YES  |     | NULL    |                |
# | content  | text         | YES  |     | NULL    |                |
# | pubDate  | varchar(255) | YES  |     | NULL    |                |
# | editDate | varchar(255) | YES  |     | NULL    |                |
# +----------+--------------+------+-----+---------+----------------+

import mysql.connector
from datetime import datetime
import click # Used to write post content, it launches a text editor to type into

# Other python files #
import create_pages
import config
import rebuild

def newpost(db, userID, username, datetime):
	title = input("Title: ")
	content = click.edit()
	content = content.replace("'", "&#39")
	content = content.replace("\n", "\\n")
	print(content)
	datetime = datetime.now().strftime("%d%m%YZ%H%M%ST")

	cursor = db.cursor()
	cursor.execute(f"INSERT INTO posts (userID, title, content, pubDate, editDate) VALUES({userID}, '{title}', '{content}', '{datetime}', '{datetime}')")
	cursor.execute(f"SELECT * FROM posts ORDER BY postID Desc LIMIT 1")
	for x in cursor:
		postID = x[0]
	db.commit()
    # Rebuild the config
	rebuild.rebuild(db)