# Generated by Django 4.2.11 on 2024-04-29 19:51

from django.db import migrations, models
import django.db.models.deletion
import json
import os


def load_initial_data(apps, schema_editor):
    # Ruta al archivo initial_data.json
    initial_data_path = os.path.join(os.path.dirname(__file__), 'initial_data.json')
    
    if os.path.exists(initial_data_path):
        with open(initial_data_path, 'r') as f:
            data = json.load(f)
        
        # Obtener los modelos necesarios
        Municipality = apps.get_model('locations', 'Municipality')
        State = apps.get_model('locations', 'State')
        Address = apps.get_model('locations', 'Address')
        
        # Cargar los datos en los modelos
        for item in data:
            if item['model'] == 'locations.municipality':
                Municipality.objects.create(**item['fields'])
            elif item['model'] == 'locations.state':
                State.objects.create(**item['fields'])
            elif item['model'] == 'locations.address':
                Address.objects.create(**item['fields'])
    else:
        print(f"Error: No se encontró el archivo {initial_data_path}")

class Migration(migrations.Migration):

    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name="State",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("name", models.CharField(max_length=60)),
            ],
        ),
        migrations.CreateModel(
            name="Municipality",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("name", models.CharField(max_length=60)),
                (
                    "state",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="municipalities",
                        to="locations.state",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="Address",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "created",
                    models.DateTimeField(
                        auto_now_add=True,
                        help_text="Date time on which the object was created.",
                        verbose_name="created at",
                    ),
                ),
                (
                    "modified",
                    models.DateTimeField(
                        auto_now=True,
                        help_text="Date time on which the object was last modified.",
                        verbose_name="modified at",
                    ),
                ),
                ("address", models.CharField(max_length=150)),
                (
                    "municipality",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        to="locations.municipality",
                    ),
                ),
                (
                    "state",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        to="locations.state",
                    ),
                ),
            ],
            options={
                "ordering": ["-created", "-modified"],
                "get_latest_by": "created",
                "abstract": False,
            },
        ),
        migrations.RunPython(load_initial_data),
    ]
