from flask import Blueprint, request, jsonify
from models.user import Users
from utils.welcome_email import WelcomeMessage
from werkzeug.security import generate_password_hash, check_password_hash
from flask_jwt_extended import jwt_required, get_jwt_identity
from models.company_users import CompanyUsers

company_user_bp = Blueprint("company_user", __name__, url_prefix="/company_user")

@company_user_bp.route('/access/<int:id>', methods=["GET"])
@jwt_required()
def get_access(id):
    company_user_id = id
    if not company_user_id:
        return jsonify({"error":"Missing required fields"}), 400
    
    company_users = CompanyUsers()
    access = company_users.get_access_by_company_user_id(company_user_id)
    return jsonify(access)

@company_user_bp.route("/register_company_user", methods=["POST"])
@jwt_required()
def register_company_user():
    users = Users()
    data = request.get_json()
    name = data.get("name")
    email = data.get("email")
    company_id = data.get("company_id")
    password = data.get("password")

    if not name or not email or not password or not company_id:
        return jsonify({"error": "Missing required fields"}), 400

    existing_user = users.get_user_by_email(email)
    if existing_user:
        return jsonify({"error": "User already exists"}), 409

    password_hash = generate_password_hash(password)
    company_user_id = users.insert_company_user(name, email, password_hash, company_id)

    welcome_message = WelcomeMessage(name, email)
    welcome_message.send_email()

    return jsonify({"message": "User registered successfully!", "company_user_id":company_user_id}), 201

@company_user_bp.route('/access', methods=["POST"])
@jwt_required()
def create_access():
    data = request.get_json()
    company_user_id = data.get('company_user_id')
    orders_in = data.get("orders_in")
    orders_out = data.get("orders_out")
    addresses = data.get("addresses")
    clients = data.get("clients")
    transporters = data.get("transporters")
    report = data.get("report")
    company_users = CompanyUsers()
    # Use explicit None checks so that False/0 values are allowed
    if (
        company_user_id is None or
        orders_in is None or
        orders_out is None or
        addresses is None or
        clients is None or
        transporters is None or
        report is None
    ):
        return jsonify({"error": "Missing required fields"}), 400

    access_data = {
        'company_user_id': company_user_id,
        'clients': clients,
        'transporters': transporters,
        'destinations': addresses,
        'orders_in': orders_in,
        'orders_out': orders_out,
        'report': report
    }
    company_users.insert_company_user_access(access_data)

    return jsonify({"message": "Company User access inserted!"})

@company_user_bp.route('/update_access', methods=["PUT"])
@jwt_required()
def update_access():
    data = request.get_json()
    company_user_id = data.get('company_user_id')
    orders_in = data.get("orders_in")
    orders_out = data.get("orders_out")
    addresses = data.get("addresses")
    clients = data.get("clients")
    transporters = data.get("transporters")
    report = data.get("report")
    company_users = CompanyUsers()
    # Use explicit None checks so that False/0 values are allowed
    if (
        company_user_id is None or
        orders_in is None or
        orders_out is None or
        addresses is None or
        clients is None or
        transporters is None or
        report is None
    ):
        return jsonify({"error": "Missing required fields"}), 400

    access_data = {
        'company_user_id': company_user_id,
        'clients': clients,
        'transporters': transporters,
        'destinations': addresses,
        'orders_in': orders_in,
        'orders_out': orders_out,
        'report': report
    }
    company_users.update_company_user_access(access_data)

    return jsonify({"message": "Company User access updated!"})