import json, os

from django.contrib.gis.geos import Point

from agenda_culturel.models import ReferenceLocation

def run():
    input_file = os.path.dirname(__file__) + os.path.sep + "communes.json"
    data = []
    with open(input_file, 'r') as file:
        data = json.load(file)
    
    # remove all locations
    ReferenceLocation.objects.all().delete()

    objs = [ReferenceLocation(location=Point(c["geo_point_2d"]["lon"], c["geo_point_2d"]["lat"]),
        name=c["com_name"],
        main=c["main"] if "main" in c else 0,
        suggested_distance=c["suggested"] if "suggested" in c else None) for c in data]

    objs = ReferenceLocation.objects.bulk_create(objs, ignore_conflicts=True)
    

