# Third-party imports
import matplotlib.pyplot as plt
import numpy as np
from sklearn.cluster import DBSCAN, HDBSCAN, KMeans

# Local imports
from .models import ObjItem

def get_clusters(coords: list[tuple[float, float]]):    
    coords = np.radians(coords)
    lat, lon = coords[:, 0], coords[:, 1]
    db = DBSCAN(eps=0.06, min_samples=5, algorithm='ball_tree', metric='haversine')
    # db = HDBSCAN()
    cluster_labels = db.fit_predict(coords)
    plt.figure(figsize=(10, 8))
    unique_labels = set(cluster_labels)
    colors = plt.cm.tab10(np.linspace(0, 1, len(unique_labels)))

    for label, color in zip(unique_labels, colors):
        if label == -1:
            color = 'k'
            label_name = 'Шум'
        else:
            label_name = f'Кластер {label}'
        
        mask = cluster_labels == label
        plt.scatter(lon[mask], lat[mask], c=[color], label=label_name, s=30)
    
    plt.xlabel('Долгота')
    plt.ylabel('Широта')
    plt.title('Кластеризация геоданных с DBSCAN (метрика Хаверсина)')
    plt.legend()
    plt.grid(True)
    plt.show()
