from flask import Blueprint, request, jsonify
from models.destinations import Destinations
from flask_jwt_extended import jwt_required, get_jwt_identity
from utils.maps import AdressCoordinates
from models.user import Users

destinations_bp = Blueprint("destinations", __name__, url_prefix="/destinations")

@destinations_bp.route("/", methods=["GET"])
@jwt_required()
def list_destinations():
    destinations_db = Destinations()
    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']
    destinations = destinations_db.get_all_by_user(user_id)
    return jsonify([dict(d) for d in destinations]), 200

@destinations_bp.route("/", methods=["POST"])
@jwt_required()
def create_destination():
    destinations_db = Destinations()
    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()
    destination_id = destinations_db.create(user_id, data.get("name"), data.get("address"))
    # coordinates = AdressCoordinates(data.get("address"))
    # lat_log = coordinates.open_Maps_by_address()
    # if lat_log:
    #     latitude = lat_log['latitude']
    #     longitude = lat_log['longitude']
    #     destinations_db.add_gps_coordinates(destination_id, latitude, longitude)
    return jsonify({"id": destination_id, "message": "Destination created"}), 201

@destinations_bp.route("/<int:id>", methods=["PUT"])
@jwt_required()
def update_destination(id):
    destinations_db = Destinations()
    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()
    destinations_db.update(id, user_id, data.get("name"), data.get("address"))
    coordinates = AdressCoordinates(data.get("address"))
    lat_log = coordinates.open_Maps_by_address()
    if lat_log:
        latitude = lat_log['latitude']
        longitude = lat_log['longitude']
        destinations_db.add_gps_coordinates(id, latitude, longitude)

    return jsonify({"message": "Destination updated"}), 200

@destinations_bp.route("/<int:id>", methods=["DELETE"])
@jwt_required()
def delete_destination(id):
    destinations_db = Destinations()
    success = destinations_db.delete(id)
    if not success:
        return jsonify({"message": "Destination not found or unauthorized"}), 404
    return "", 204


# New route to update GPS coordinates of a destination
@destinations_bp.route("/<int:id>/coordinates", methods=["PUT"])
@jwt_required()
def update_coordinates(id):
    destinations_db = Destinations()
    data = request.get_json()
    latitude = data.get("latitude")
    longitude = data.get("longitude")

    if latitude is None or longitude is None:
        return jsonify({"message": "Latitude and longitude are required"}), 400

    success = destinations_db.add_gps_coordinates(id, latitude, longitude)
    if not success:
        return jsonify({"message": "Failed to update coordinates"}), 404

    return jsonify({"message": "Coordinates updated"}), 200