# controllers/subcontractor_controller.py

from flask import Blueprint, render_template, request, redirect, url_for, jsonify
from flask_login import login_required
from model.Subcontractor import Subcontractor
from model.Log import LogHelper 
subcontractor_bp = Blueprint('subcontractor', __name__)

# Simple replacements for missing HtmlHelper and ResponseHandler
class HtmlHelper:
    @staticmethod
    def json_response(data, status=200):
        return jsonify(data), status

class ResponseHandler:
    @staticmethod
    def fetch_failure(entity):
        return {"status": "error", "message": f"Failed to fetch {entity}"}

    @staticmethod
    def add_failure(entity):
        return {"status": "error", "message": f"Failed to add {entity}"}

    @staticmethod
    def update_failure(entity):
        return {"status": "error", "message": f"Failed to update {entity}"}

    @staticmethod
    def delete_failure(entity):
        return {"status": "error", "message": f"Failed to delete {entity}"}


@subcontractor_bp.route('/subcontractor', methods=['GET', 'POST'])
@login_required
def subcontract():
    subcontractor = []

    if request.method == 'GET':
        subcontractor, error = Subcontractor.get_all_subcontractors()
        if error:
            return HtmlHelper.json_response(ResponseHandler.fetch_failure("Subcontractor"), 500)

    if request.method == 'POST':
        contractor_data = {
            'Contractor_Name': request.form['Contractor_Name'],
            'Address': request.form['Address'],
            'Mobile_No': request.form['Mobile_No'],
            'PAN_No': request.form['PAN_No'],
            'Email': request.form['Email'],
            'Gender': request.form['Gender'],
            'GST_Registration_Type': request.form['GST_Registration_Type'],
            'GST_No': request.form['GST_No'],
            'Contractor_password': request.form['Contractor_password'],
        }
        error = Subcontractor.save_subcontractor(contractor_data)
        if error:
            return HtmlHelper.json_response(ResponseHandler.add_failure("Subcontractor"), 500)
        subcontractor, _ = Subcontractor.get_all_subcontractors()

    return render_template('add_subcontractor.html', subcontractor=subcontractor)


@subcontractor_bp.route('/edit_subcontractor/<int:id>', methods=['GET', 'POST'])
@login_required
def edit_subcontractor(id):
    subcontractor, error = Subcontractor.get_subcontractor_by_id(id)
    if error:
        return HtmlHelper.json_response(ResponseHandler.fetch_failure("Subcontractor"), 500)
    if not subcontractor:
        return HtmlHelper.json_response(ResponseHandler.fetch_failure("Subcontractor"), 404)

    if request.method == 'POST':
        updated_data = {
            'Contractor_Name': request.form['Contractor_Name'],
            'Address': request.form['Address'],
            'Mobile_No': request.form['Mobile_No'],
            'PAN_No': request.form['PAN_No'],
            'Email': request.form['Email'],
            'Gender': request.form['Gender'],
            'GST_Registration_Type': request.form['GST_Registration_Type'],
            'GST_No': request.form['GST_No'],
            'Contractor_password': request.form['Contractor_password'],
        }
        error = Subcontractor.update_subcontractor(id, updated_data)
        if error:
            return HtmlHelper.json_response(ResponseHandler.update_failure("Subcontractor"), 500)
        return redirect(url_for('subcontractor.subcontract'))

    return render_template('edit_subcontractor.html', subcontractor=subcontractor)


@subcontractor_bp.route('/deleteSubContractor/<int:id>', methods=['GET', 'POST'])
@login_required
def deleteSubContractor(id):
    error, affected_rows = Subcontractor.delete_subcontractor(id)
    if error:
        return HtmlHelper.json_response(ResponseHandler.delete_failure("Subcontractor"), 500)
    if affected_rows == 0:
        return HtmlHelper.json_response(ResponseHandler.fetch_failure("Subcontractor not deleted"), 404)
    return redirect(url_for('subcontractor.subcontract'))