from flask import Flask, render_template, request, jsonify
from werkzeug.utils import secure_filename
from pathlib import Path
import os
import sqlite3

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = Path('./static/uploads')
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)

DB_PATH = 'letter_data.db'


def query_db(query, args=(), one=False):
    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row
    cur = conn.cursor()
    cur.execute(query, args)
    rv = cur.fetchall()
    conn.commit()
    conn.close()
    return (rv[0] if rv else None) if one else rv


@app.route('/')
def index():
    senders = query_db('SELECT * FROM sender')
    recipients = query_db('SELECT * FROM recipient')
    states = query_db('SELECT * FROM state')
    return render_template('index.html', senders=senders, recipients=recipients, states=states)


@app.route('/generate', methods=['POST'])
def generate():
    sender_id = request.form['sender']
    recipient_id = request.form['recipient']
    text = request.form['text']
    sender = query_db('SELECT * FROM sender WHERE id = ?', [sender_id], one=True)
    recipient = query_db('SELECT recipient.*, state.name as state_name FROM recipient LEFT JOIN state ON recipient.state_id = state.id WHERE recipient.id = ?', [recipient_id], one=True)
    return render_template('letter.html', sender=sender, recipient=recipient, text=text)


@app.route('/api/senders', methods=['POST'])
def api_add_sender():
    name = request.form['name']
    kuralie = request.form['kuralie']
    siegel = request.files['logo']
    signatur = request.files['signature']

    siegel_filename = secure_filename(siegel.filename)
    siegel.save(app.config['UPLOAD_FOLDER'] / siegel)

    sig_filename = secure_filename(signatur.filename)
    signatur.save(app.config['UPLOAD_FOLDER'] / sig_filename)

    query_db('INSERT INTO sender (name, kuralie, siegel, signatur) VALUES (?, ?, ?, ?)',
             [name, kuralie, siegel_filename, sig_filename])
    return jsonify(success=True)


@app.route('/api/recipients', methods=['POST'])
def api_add_recipient():
    name = request.form['name']
    kuralie = request.form['kuralie']
    state_id = request.form['state_id'] or None
    query_db('INSERT INTO recipient (name, kuralie, state_id) VALUES (?, ?, ?)', [name, kuralie, state_id])
    return jsonify(success=True)


@app.route('/api/states', methods=['POST'])
def api_add_state():
    name = request.form['name']
    query_db('INSERT INTO state (name) VALUES (?)', [name])
    return jsonify(success=True)


@app.route('/api/senders', methods=['GET'])
def api_get_senders():
    senders = query_db('SELECT id, name FROM sender')
    return jsonify([dict(row) for row in senders])


@app.route('/api/senders/<int:sender_id>', methods=['GET'])
def api_get_sender(sender_id):
    sender = query_db('SELECT * FROM sender WHERE id = ?', [sender_id], one=True)
    return jsonify(dict(sender))


@app.route('/api/recipients', methods=['GET'])
def api_get_recipients():
    state_id = request.args.get('state_id')
    if state_id and state_id != 'all':
        recipients = query_db('SELECT id, name FROM recipient WHERE state_id = ?', [state_id])
    else:
        recipients = query_db('SELECT id, name FROM recipient')
    return jsonify([dict(row) for row in recipients])


@app.route('/api/recipients/<int:recipient_id>', methods=['GET'])
def api_get_recipient(recipient_id):
    recipient = query_db('SELECT * FROM recipient WHERE id = ?', [recipient_id], one=True)
    return jsonify(dict(recipient))


@app.route('/api/states', methods=['GET'])
def api_get_states():
    states = query_db('SELECT id, name FROM state')
    return jsonify([dict(row) for row in states])


@app.route('/api/states/<int:state_id>', methods=['GET'])
def api_get_state(state_id):
    state = query_db('SELECT * FROM state WHERE id = ?', [state_id], one=True)
    return jsonify(dict(state))


@app.route('/api/senders/<int:sender_id>', methods=['PUT'])
def api_edit_sender(sender_id):
    name = request.form['name']
    kuralie = request.form['kuralie']
    siegel = request.files['siegel']
    signatur = request.files['signatur']

    siegel_filename = secure_filename(siegel.filename) if siegel else None
    sig_filename = secure_filename(signatur.filename) if signatur else None

    if siegel:
        siegel.save(app.config['UPLOAD_FOLDER'] / siegel_filename)
    if signatur:
        signatur.save(app.config['UPLOAD_FOLDER'] / sig_filename)

    query_db('UPDATE sender SET name = ?, kuralie = ?, siegel = ?, signatur = ? WHERE id = ?',
             [name, kuralie, siegel_filename, sig_filename, sender_id])
    return jsonify(success=True)


@app.route('/api/recipients/<int:recipient_id>', methods=['PUT'])
def api_edit_recipient(recipient_id):
    name = request.form['name']
    kuralie = request.form['kuralie']
    state_id = request.form['state_id']

    query_db('UPDATE recipient SET name = ?, kuralie = ?, state_id = ? WHERE id = ?',
             [name, kuralie, state_id, recipient_id])
    return jsonify(success=True)


@app.route('/api/states/<int:state_id>', methods=['PUT'])
def api_edit_state(state_id):
    name = request.form['name']
    query_db('UPDATE state SET name = ? WHERE id = ?', [name, state_id])
    return jsonify(success=True)


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