# Generated by Django 5.2 on 2025-05-01 11:40

from django.db import migrations, models
from pathlib import Path
import csv
import os


def load_combined_data(apps, schema_editor):
    CountryCode = apps.get_model('django_genc', 'CountryCode')
    
    # Get the path to the data file
    data_dir = Path(__file__).parent.parent / 'data'
    csv_file = data_dir / 'genc_iso_combined.csv'
    
    if not os.path.exists(csv_file):
        print(f'Warning: Data file not found at {csv_file}')
        return
        
    with open(csv_file, 'r') as f:
        reader = csv.DictReader(f)
        
        for row in reader:
            # Skip entries with no GENC code
            if not row['genc_code']:
                continue
                
            # Convert empty strings to None for optional fields
            iso_code = row['iso_code'] if row['iso_code'] and row['iso_code'] != '-' else None
            iso_code_3 = row['iso_code_3'] if row['iso_code_3'] else None
            iso_code_numeric = row['iso_code_numeric'] if row['iso_code_numeric'] else None
            iso_name = row['iso_name'] if row['iso_name'] else None
            comment = row['comment'] if row['comment'] else None
            genc_status = row['genc_status'] if row['genc_status'] else None
            
            CountryCode.objects.create(
                genc_code=row['genc_code'],
                genc_name=row['genc_name'],
                genc_status=genc_status,
                iso_code=iso_code,
                iso_code_3=iso_code_3,
                iso_code_numeric=iso_code_numeric,
                iso_name=iso_name,
                comment=comment
            )


class Migration(migrations.Migration):
    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='CountryCode',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('genc_code', models.CharField(max_length=3, unique=True, help_text='Three-digit GENC country code')),
                ('genc_name', models.CharField(max_length=100, help_text='Official GENC name')),
                ('genc_status', models.CharField(blank=True, max_length=20, null=True, help_text='GENC status (Exception, Extension, etc.)')),
                ('iso_code', models.CharField(blank=True, max_length=2, null=True, help_text='Two-digit ISO country code')),
                ('iso_code_3', models.CharField(blank=True, max_length=3, null=True, help_text='Three-digit ISO country code (if different from GENC)')),
                ('iso_code_numeric', models.CharField(blank=True, max_length=3, null=True, help_text='Numeric ISO country code')),
                ('iso_name', models.CharField(blank=True, max_length=100, null=True, help_text='Official ISO name')),
                ('comment', models.TextField(blank=True, null=True, help_text='Additional information about the country code')),
            ],
            options={
                'verbose_name': 'Country Code',
                'verbose_name_plural': 'Country Codes',
                'ordering': ['genc_name'],
            },
        ),
        migrations.RunPython(load_combined_data),
    ]
