# Generated by Django 5.2 on 2025-09-26 13:35

import django.core.validators
from decimal import Decimal
from django.conf import settings
from django.db import migrations, models


def seed_task_credit_config(apps, schema_editor):
    TaskCreditConfig = apps.get_model('api', 'TaskCreditConfig')
    ToolCreditCost = apps.get_model('api', 'ToolCreditCost')

    default_cost = getattr(settings, 'CREDITS_PER_TASK', Decimal('0.4'))
    TaskCreditConfig.objects.update_or_create(
        singleton_id=1,
        defaults={'default_task_cost': default_cost},
    )

    overrides = getattr(settings, 'TOOL_CREDIT_COSTS', {}) or {}
    for name, raw_value in overrides.items():
        normalized = (name or '').strip().lower()
        if not normalized:
            continue
        try:
            value = raw_value if isinstance(raw_value, Decimal) else Decimal(str(raw_value))
        except Exception:
            continue
        ToolCreditCost.objects.update_or_create(
            tool_name=normalized,
            defaults={'credit_cost': value},
        )


def noop_reverse(apps, schema_editor):
    # Intentionally left blank; configuration rows may contain user edits.
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0142_remove_db_stripe_secret_keys'),
    ]

    operations = [
        migrations.CreateModel(
            name='TaskCreditConfig',
            fields=[
                ('singleton_id', models.PositiveSmallIntegerField(default=1, editable=False, primary_key=True, serialize=False)),
                ('default_task_cost', models.DecimalField(decimal_places=3, help_text='Default credit cost applied when no tool-specific override exists.', max_digits=12, validators=[django.core.validators.MinValueValidator(Decimal('0'))])),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
            ],
            options={
                'verbose_name': 'Task credit configuration',
                'verbose_name_plural': 'Task credit configuration',
            },
        ),
        migrations.CreateModel(
            name='ToolCreditCost',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('tool_name', models.CharField(help_text='Name of the tool (case-insensitive).', max_length=255, unique=True)),
                ('credit_cost', models.DecimalField(decimal_places=3, help_text='Credit cost charged when this tool is executed.', max_digits=12, validators=[django.core.validators.MinValueValidator(Decimal('0'))])),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
            ],
            options={
                'verbose_name': 'Tool credit cost',
                'verbose_name_plural': 'Tool credit costs',
                'ordering': ['tool_name'],
            },
        ),
        migrations.RunPython(seed_task_credit_config, noop_reverse),
    ]
