# Generated by Django 6.0.1 on 2026-05-29 15:18

import django.db.models.deletion
import uuid
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('integrations', '0002_initial'),
        ('organizations', '0005_alter_legacylocation_table_and_more'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='ExternalAPIKey',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('name', models.CharField(help_text="Label for this integration (e.g. 'MOH Portal', 'CHI Connector')", max_length=200)),
                ('key_hash', models.CharField(db_index=True, help_text='SHA-256 hash of the API key', max_length=128, unique=True)),
                ('key_prefix', models.CharField(help_text="First 8 chars of the key for identification (e.g. 'hh_live_')", max_length=12)),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('allowed_entities', models.JSONField(blank=True, default=list, help_text='List of entities this key can access: ["complaints","inquiries","observations","appreciations","suggestions"]')),
                ('rate_limit', models.IntegerField(default=60, help_text='Maximum requests per minute')),
                ('description', models.TextField(blank=True)),
                ('last_used_at', models.DateTimeField(blank=True, null=True)),
                ('expires_at', models.DateTimeField(blank=True, help_text='When this key expires (null = never)', null=True)),
                ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_external_api_keys', to=settings.AUTH_USER_MODEL)),
                ('hospital', models.ForeignKey(blank=True, help_text='Scope this key to a hospital (null = all hospitals)', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='external_api_keys', to='organizations.hospital')),
            ],
            options={
                'verbose_name': 'External API Key',
                'verbose_name_plural': 'External API Keys',
                'ordering': ['-created_at'],
            },
        ),
    ]
