from flask import Blueprint, render_template, request, redirect, url_for, jsonify
from flask_login import login_required, current_user
from model.gst_release import GSTReleasemodel
from model.Log import LogHelper

gst_release_bp = Blueprint('gst_release_bp', __name__)

# ------------------- Add GST Release -------------------
@gst_release_bp.route('/add_gst_release', methods=['GET', 'POST'])
@login_required
def add_gst_release():
    gst_releases_dict = GSTReleasemodel.fetch_all_gst_releases()
    gst_releases = [
        [
            g['GST_Release_Id'], g['PMC_No'], g['invoice_no'],
            g['Basic_Amount'], g['Final_Amount'], g['Total_Amount'], g['UTR']
        ] for g in gst_releases_dict
    ] if gst_releases_dict else []

    if request.method == 'POST':
        pmc_no = request.form['PMC_No']
        invoice_no = request.form['invoice_No']
        basic_amount = request.form['basic_amount']
        final_amount = request.form['final_amount']
        total_amount = request.form['total_amount']
        utr = request.form['utr']
        contractor_id = request.form['subcontractor_id']

        LogHelper.log_action("Add GST Release", f"User {current_user.id} added GST release '{pmc_no}'")
        GSTReleasemodel.insert_gst_release(pmc_no, invoice_no, basic_amount, final_amount, total_amount, utr, contractor_id)

        return redirect(url_for('gst_release_bp.add_gst_release'))

    return render_template('add_gst_release.html', gst_releases=gst_releases, invoices=[])


# ------------------- Edit GST Release -------------------
@gst_release_bp.route('/edit_gst_release/<int:gst_release_id>', methods=['GET', 'POST'])
@login_required
def edit_gst_release(gst_release_id):
    gst_release_data = GSTReleasemodel.fetch_gst_release_by_id(gst_release_id)
    if not gst_release_data:
        return "GST Release not found", 404

    if request.method == 'POST':
        pmc_no = request.form['PMC_No']
        invoice_no = request.form['invoice_No']
        basic_amount = request.form['basic_amount']
        final_amount = request.form['final_amount']
        total_amount = request.form['total_amount']
        utr = request.form['utr']

        LogHelper.log_action("Edit GST Release", f"User {current_user.id} edited GST release '{pmc_no}'")
        GSTReleasemodel.update_gst_release(gst_release_id, pmc_no, invoice_no, basic_amount, final_amount, total_amount, utr)

        return redirect(url_for('gst_release_bp.add_gst_release'))

    return render_template('edit_gst_release.html', gst_release_data=gst_release_data, invoices=[])


# ------------------- Delete GST Release -------------------
@gst_release_bp.route('/delete_gst_release/<int:gst_release_id>', methods=['GET', 'POST'])
@login_required
def delete_gst_release(gst_release_id):
    success, utr = GSTReleasemodel.delete_gst_release(gst_release_id)
    if not success:
        return jsonify({"message": "GST Release not found or failed to delete", "status": "error"}), 404

    LogHelper.log_action("Delete GST Release", f"User {current_user.id} deleted GST release '{gst_release_id}'")
    return jsonify({"message": f"GST Release {gst_release_id} deleted successfully.", "status": "success"}), 200