#!/usr/bin/env python3
"""
Organiza lugares salvos em formato legível
"""

import json
import re
from pathlib import Path

def is_address(text):
    """Detecta se um texto parece ser um endereço"""
    address_patterns = [
        r'\d{1,5}.*(?:Street|Avenue|Road|Boulevard|Lane|Drive|Way|Place|Court|Florida|Paraguay)',
        r'[A-Z][a-z]+\s+\d{1,5}',  # Nome + número
        r'\d{1,5}\s+[A-Z][a-z]+',  # Número + nome
    ]
    for pattern in address_patterns:
        if re.search(pattern, text, re.IGNORECASE):
            return True
    return False

def categorize_text(text):
    """Categoriza texto extraído"""
    categories = {
        'Estabelecimentos': r'(Store|Shop|Restaurant|Hotel|Cafe|Bar|Pharmacy|Bank|Mall|Market|Shopping)',
        'Serviços': r'(Service|Therapy|Massage|Medical|Hospital|Clinic)',
        'Locais': r'(Avenue|Street|Road|Boulevard|Beach|Park|Center)',
        'URLs': r'https?://|\.com|\.io|\.net',
    }

    for cat, pattern in categories.items():
        if re.search(pattern, text, re.IGNORECASE):
            return cat
    return 'Outros'

def clean_text(text):
    """Limpa texto removendo caracteres especiais"""
    # Remover caracteres de controle e códigos binários
    text = re.sub(r'[^\x20-\x7E]', '', text)
    # Remover sequências de códigos
    text = re.sub(r'\b[A-Z]{2,4}\d+\b', '', text)
    # Remover múltiplos espaços
    text = re.sub(r'\s+', ' ', text)
    return text.strip()

def organize_places():
    input_file = Path.home() / 'maps_decoded_places.json'
    output_file = Path.home() / 'maps_places_organized.txt'
    output_json = Path.home() / 'maps_places_organized.json'

    with open(input_file, 'r', encoding='utf-8') as f:
        places = json.load(f)

    organized = []

    for i, place in enumerate(places, 1):
        texts = place.get('texts_found', [])

        # Filtrar e categorizar
        addresses = [t for t in texts if is_address(t)]
        establishments = [t for t in texts if categorize_text(t) == 'Estabelecimentos']
        services = [t for t in texts if categorize_text(t) == 'Serviços']
        locations = [t for t in texts if categorize_text(t) == 'Locais']
        urls = [t for t in texts if categorize_text(t) == 'URLs']

        # Pegar nomes mais relevantes (sem códigos)
        relevant_names = []
        for text in texts:
            cleaned = clean_text(text)
            if len(cleaned) > 5 and not re.match(r'^[A-Z0-9]+$', cleaned):
                if categorize_text(cleaned) != 'URLs':
                    relevant_names.append(cleaned)

        # Remover duplicatas mantendo ordem
        relevant_names = list(dict.fromkeys(relevant_names[:5]))

        place_info = {
            'numero': i,
            'id': place.get('id', ''),
            'nomes': relevant_names,
            'enderecos': addresses[:3],
            'estabelecimentos': establishments[:3],
            'servicos': services[:3],
            'urls': urls[:2],
            'coordenadas': place.get('coord_pairs', [])[:2],
        }

        organized.append(place_info)

    # Salvar como texto
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write("=" * 80 + "\n")
        f.write(" LUGARES SALVOS DO APPLE MAPS - RESUMO ORGANIZADO\n")
        f.write("=" * 80 + "\n\n")

        for place in organized:
            f.write(f"📍 LUGAR #{place['numero']}\n")
            f.write("-" * 80 + "\n")

            if place['nomes']:
                f.write(f"\n🏷️  NOMES/TÍTULOS:\n")
                for name in place['nomes']:
                    f.write(f"   • {name}\n")

            if place['enderecos']:
                f.write(f"\n📮 ENDEREÇOS:\n")
                for addr in place['enderecos']:
                    f.write(f"   • {addr}\n")

            if place['estabelecimentos']:
                f.write(f"\n🏪 ESTABELECIMENTOS/CATEGORIAS:\n")
                for est in place['estabelecimentos']:
                    f.write(f"   • {est}\n")

            if place['servicos']:
                f.write(f"\n💼 SERVIÇOS:\n")
                for srv in place['servicos']:
                    f.write(f"   • {srv}\n")

            if place['urls']:
                f.write(f"\n🌐 URLs:\n")
                for url in place['urls']:
                    f.write(f"   • {url}\n")

            if place['coordenadas']:
                f.write(f"\n🌍 COORDENADAS:\n")
                for coord in place['coordenadas']:
                    lat = coord.get('latitude', 'N/A')
                    lng = coord.get('longitude', 'N/A')
                    f.write(f"   • Lat: {lat}, Lng: {lng}\n")

            f.write("\n" + "=" * 80 + "\n\n")

        # Estatísticas
        total_with_addresses = sum(1 for p in organized if p['enderecos'])
        total_with_coords = sum(1 for p in organized if p['coordenadas'])
        total_establishments = sum(1 for p in organized if p['estabelecimentos'])

        f.write("\n📊 ESTATÍSTICAS:\n")
        f.write("-" * 80 + "\n")
        f.write(f"Total de lugares: {len(organized)}\n")
        f.write(f"Com endereços: {total_with_addresses}\n")
        f.write(f"Com coordenadas: {total_with_coords}\n")
        f.write(f"Com tipo de estabelecimento: {total_establishments}\n")

    # Salvar JSON organizado
    with open(output_json, 'w', encoding='utf-8') as f:
        json.dump(organized, f, indent=2, ensure_ascii=False)

    print(f"✅ Resumo organizado salvo em:")
    print(f"   📄 Texto: {output_file}")
    print(f"   📄 JSON:  {output_json}")
    print(f"\n📊 Total de lugares: {len(organized)}")
    print(f"   • Com endereços: {total_with_addresses}")
    print(f"   • Com coordenadas: {total_with_coords}")
    print(f"   • Com tipo: {total_establishments}")

if __name__ == '__main__':
    organize_places()
