# Generated by Django 6.0.1 on 2026-05-11 20:32

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('auth', '0012_alter_user_first_name_max_length'),
        ('organizations', '0001_initial'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='ReferenceFolder',
            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)),
                ('is_deleted', models.BooleanField(db_index=True, default=False)),
                ('deleted_at', models.DateTimeField(blank=True, null=True)),
                ('name', models.CharField(db_index=True, max_length=200)),
                ('name_ar', models.CharField(blank=True, max_length=200, verbose_name='Name (Arabic)')),
                ('description', models.TextField(blank=True)),
                ('description_ar', models.TextField(blank=True, verbose_name='Description (Arabic)')),
                ('icon', models.CharField(blank=True, help_text="Icon class (e.g., 'fa-folder', 'fa-file-pdf')", max_length=50)),
                ('color', models.CharField(blank=True, help_text="Hex color code (e.g., '#007bff')", max_length=7)),
                ('order', models.IntegerField(default=0, help_text='Display order within parent folder')),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('access_roles', models.ManyToManyField(blank=True, help_text='Roles that can access this folder (empty = all roles)', related_name='accessible_folders', to='auth.group')),
                ('deleted_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='deleted_%(class)s_set', to=settings.AUTH_USER_MODEL)),
                ('hospital', models.ForeignKey(help_text='Tenant hospital for this record', on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_related', to='organizations.hospital')),
                ('parent', models.ForeignKey(blank=True, help_text='Parent folder for nested structure', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='subfolders', to='references.referencefolder')),
            ],
            options={
                'verbose_name': 'Reference Folder',
                'verbose_name_plural': 'Reference Folders',
                'ordering': ['parent__order', 'order', 'name'],
            },
        ),
        migrations.CreateModel(
            name='ReferenceDocument',
            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)),
                ('is_deleted', models.BooleanField(db_index=True, default=False)),
                ('deleted_at', models.DateTimeField(blank=True, null=True)),
                ('title', models.CharField(db_index=True, max_length=500)),
                ('title_ar', models.CharField(blank=True, max_length=500, verbose_name='Title (Arabic)')),
                ('file', models.FileField(max_length=500, upload_to=apps.references.models.document_upload_path)),
                ('filename', models.CharField(help_text='Original filename', max_length=500)),
                ('file_type', models.CharField(blank=True, help_text='File extension/type (e.g., pdf, docx, xlsx)', max_length=50)),
                ('file_size', models.IntegerField(help_text='File size in bytes')),
                ('description', models.TextField(blank=True)),
                ('description_ar', models.TextField(blank=True, verbose_name='Description (Arabic)')),
                ('version', models.CharField(default='1.0', help_text='Document version (e.g., 1.0, 1.1, 2.0)', max_length=20)),
                ('is_latest_version', models.BooleanField(db_index=True, default=True, help_text='Is this the latest version?')),
                ('download_count', models.IntegerField(default=0, help_text='Number of downloads')),
                ('last_accessed_at', models.DateTimeField(blank=True, null=True)),
                ('is_published', models.BooleanField(db_index=True, default=True, help_text='Is this document visible to users?')),
                ('tags', models.CharField(blank=True, help_text='Comma-separated tags for search (e.g., policy, procedure, handbook)', max_length=500)),
                ('metadata', models.JSONField(blank=True, default=dict)),
                ('access_roles', models.ManyToManyField(blank=True, help_text='Roles that can access this document (empty = all roles)', related_name='accessible_documents', to='auth.group')),
                ('deleted_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='deleted_%(class)s_set', to=settings.AUTH_USER_MODEL)),
                ('hospital', models.ForeignKey(help_text='Tenant hospital for this record', on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_related', to='organizations.hospital')),
                ('parent_document', models.ForeignKey(blank=True, help_text='Previous version of this document', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='versions', to='references.referencedocument')),
                ('uploaded_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='uploaded_documents', to=settings.AUTH_USER_MODEL)),
                ('folder', models.ForeignKey(blank=True, help_text='Folder containing this document', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='documents', to='references.referencefolder')),
            ],
            options={
                'verbose_name': 'Reference Document',
                'verbose_name_plural': 'Reference Documents',
                'ordering': ['title', '-created_at'],
            },
        ),
        migrations.CreateModel(
            name='ReferenceDocumentAccess',
            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)),
                ('action', models.CharField(choices=[('view', 'Viewed'), ('download', 'Downloaded'), ('preview', 'Previewed')], db_index=True, max_length=20)),
                ('ip_address', models.GenericIPAddressField(blank=True, null=True)),
                ('user_agent', models.TextField(blank=True)),
                ('document', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='access_logs', to='references.referencedocument')),
                ('user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='document_accesses', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'Document Access Log',
                'verbose_name_plural': 'Document Access Logs',
                'ordering': ['-created_at'],
                'indexes': [models.Index(fields=['document', '-created_at'], name='references__documen_396fa2_idx'), models.Index(fields=['user', '-created_at'], name='references__user_id_56bf5f_idx'), models.Index(fields=['action', '-created_at'], name='references__action_b9899d_idx')],
            },
        ),
        migrations.AddIndex(
            model_name='referencefolder',
            index=models.Index(fields=['hospital', 'parent', 'order'], name='references__hospita_faa66f_idx'),
        ),
        migrations.AddIndex(
            model_name='referencefolder',
            index=models.Index(fields=['hospital', 'is_active'], name='references__hospita_6c43f3_idx'),
        ),
        migrations.AddIndex(
            model_name='referencedocument',
            index=models.Index(fields=['hospital', 'folder', 'is_latest_version'], name='references__hospita_36d516_idx'),
        ),
        migrations.AddIndex(
            model_name='referencedocument',
            index=models.Index(fields=['hospital', 'is_published'], name='references__hospita_413b58_idx'),
        ),
        migrations.AddIndex(
            model_name='referencedocument',
            index=models.Index(fields=['folder', 'title'], name='references__folder__e09b4c_idx'),
        ),
    ]
