# Generated by Django 5.2.7 on 2025-12-17 19:22

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

def migrate_clients_forward(apps, schema_editor):
    Project = apps.get_model('core', 'Project')
    Client = apps.get_model('core', 'Client')
    for project in Project.objects.all():
        client_name = getattr(project, 'client_temp', None)
        if client_name:
            client, created = Client.objects.get_or_create(name=client_name)
            project.client = client
            project.save()

def migrate_clients_backward(apps, schema_editor):
    # This is a one-way migration, but we can try to restore the old values
    Project = apps.get_model('core', 'Project')
    for project in Project.objects.all():
        if project.client:
            project.client_temp = project.client.name
            project.save()

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0001_initial'),
    ]

    operations = [
        # 1. Create the new Client model
        migrations.CreateModel(
            name='Client',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=200, verbose_name='Client Name')),
                ('email', models.EmailField(blank=True, max_length=200, null=True, verbose_name='Email')),
                ('phone', models.CharField(blank=True, max_length=20, null=True, verbose_name='Phone')),
            ],
        ),

        # 2. Rename the existing 'client' CharField to a temporary field
        migrations.RenameField(
            model_name='project',
            old_name='client',
            new_name='client_temp',
        ),

        # 3. Add the new 'client' ForeignKey field, allowing it to be null for now
        migrations.AddField(
            model_name='project',
            name='client',
            field=models.ForeignKey(
                to='core.Client',
                on_delete=django.db.models.deletion.CASCADE,
                related_name='projects',
                verbose_name='Client',
                null=True, # Allow null during migration
                blank=True
            ),
        ),

        # 4. Run the Python script to migrate the data
        migrations.RunPython(migrate_clients_forward, migrate_clients_backward),

        # 5. Remove the temporary CharField
        migrations.RemoveField(
            model_name='project',
            name='client_temp',
        ),

        # 6. Alter other fields (translations)
        migrations.AlterField(
            model_name='project',
            name='end_date',
            field=models.DateField(verbose_name='End Date'),
        ),
        migrations.AlterField(
            model_name='project',
            name='name',
            field=models.CharField(max_length=200, verbose_name='Project Name'),
        ),
        migrations.AlterField(
            model_name='project',
            name='start_date',
            field=models.DateField(verbose_name='Start Date'),
        ),
        migrations.AlterField(
            model_name='project',
            name='status',
            field=models.CharField(choices=[('planning', 'Planning'), ('in_progress', 'In Progress'), ('completed', 'Completed')], default='planning', max_length=20, verbose_name='Status'),
        ),
    ]