from flask import Blueprint, request, jsonify
from flask_jwt_extended import jwt_required, get_jwt_identity
from models.transporters import Transporters
from models.user import Users

transporters_bp = Blueprint("transporters", __name__, url_prefix="/transporters")

@transporters_bp.route("/", methods=["GET"])
@jwt_required()
def list_transporters():
    user_id = get_jwt_identity()
    users = Users()
    user = users.get_user_by_id(user_id)
    if user['user_role'] == 'company_user':
        user_id = user['company_id']
    transporters_db = Transporters()
    transporters = transporters_db.get_all_transporters_by_user(user_id)
    return jsonify(transporters), 200

@transporters_bp.route("/", methods=["POST"])
@jwt_required()
def create_transporter():
    transporters_db = Transporters()
    data = request.get_json()
    user_id = get_jwt_identity()
    users = Users()
    user = users.get_user_by_id(user_id)
    if user['user_role'] == 'company_user':
        user_id = user['company_id']
    transporter_id = transporters_db.create_transporter(
        user_id=user_id,
        name=data.get("name"),
        address=data.get("address"),
        register_number=data.get("register_number"),
        contact_person=data.get("contact_person"),
        phone=data.get("phone"),
        email=data.get("email"),
        vat = data.get("vat")
    )
    transporter = transporters_db.get_transporter_by_id(transporter_id)
    return jsonify(transporter), 201

@transporters_bp.route("/<int:transporter_id>", methods=["PUT"])
@jwt_required()
def update_transporter(transporter_id):
    transporters_db = Transporters()
    user_id = get_jwt_identity()
    users = Users()
    user = users.get_user_by_id(user_id)
    if user['user_role'] == 'company_user':
        user_id = user['company_id']
    data = request.get_json()
    transporter = transporters_db.get_transporter_by_id(transporter_id)
    if not transporter:
        return jsonify({"error": "Transporter not found"}), 404
    transporters_db.update_transporter(
        transporter_id=transporter_id,
        name=data.get("name"),
        address=data.get("address"),
        register_number=data.get("register_number"),
        contact_person=data.get("contact_person"),
        phone=data.get("phone"),
        email=data.get("email"),
        vat=data.get("vat")
    )
    updated_transporter = transporters_db.get_transporter_by_id(transporter_id)
    return jsonify(updated_transporter), 200

@transporters_bp.route("/<int:transporter_id>", methods=["DELETE"])
@jwt_required()
def delete_transporter(transporter_id):
    transporters_db = Transporters()
    user_id = get_jwt_identity()
    users = Users()
    user = users.get_user_by_id(user_id)
    if user['user_role'] == 'company_user':
        user_id = user['company_id']
    transporter = transporters_db.get_transporter_by_id(transporter_id)
    if not transporter:
        return jsonify({"error": "Transporter not found"}), 404
    transporters_db.delete_transporter(transporter_id)
    return jsonify({"message": "Transporter deleted"}), 200

@transporters_bp.route("/<int:transporter_id>", methods=["GET"])
@jwt_required()
def get_transporter(transporter_id):
    transporters_db = Transporters()
    transporter = transporters_db.get_transporter_by_id(transporter_id)
    if not transporter:
        return jsonify({"error": "Transporter not found"}), 404
    return jsonify(transporter), 200