# Generated by Django 5.2 on 2025-08-14 16:16

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


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0084_linkshortener_user'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='AgentFileSpace',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('name', models.CharField(help_text='Human-friendly name for this filespace', max_length=128)),
                ('description', models.TextField(blank=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('owner_user', models.ForeignKey(help_text='Owning user; access for agents is managed via the access table.', on_delete=django.db.models.deletion.CASCADE, related_name='agent_filespaces', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='AgentFileSpaceAccess',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('role', models.CharField(choices=[('OWNER', 'Owner'), ('WRITER', 'Writer'), ('READER', 'Reader')], default='OWNER', max_length=16)),
                ('is_default', models.BooleanField(default=False, help_text="Whether this is the agent's default working-set filespace.")),
                ('granted_at', models.DateTimeField(auto_now_add=True)),
                ('agent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='filespace_access', to='api.persistentagent')),
                ('filespace', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='access', to='api.agentfilespace')),
            ],
            options={
                'ordering': ['-granted_at'],
            },
        ),
        migrations.AddField(
            model_name='agentfilespace',
            name='agents',
            field=models.ManyToManyField(blank=True, related_name='filespaces', through='api.AgentFileSpaceAccess', to='api.persistentagent'),
        ),
        migrations.CreateModel(
            name='AgentFsNode',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('node_type', models.CharField(choices=[('dir', 'Directory'), ('file', 'File')], max_length=8)),
                ('name', models.CharField(help_text='Directory or file name (no path separators)', max_length=255)),
                ('path', models.TextField(blank=True, help_text='Cached absolute path within the filespace for quick lookups and UI.')),
                ('content', models.FileField(blank=True, help_text='Binary content for files. Empty for directories.', null=True, upload_to=api.models.agent_fs_upload_to)),
                ('size_bytes', models.PositiveBigIntegerField(blank=True, null=True)),
                ('mime_type', models.CharField(blank=True, max_length=127)),
                ('checksum_sha256', models.CharField(blank=True, max_length=64)),
                ('is_deleted', models.BooleanField(db_index=True, default=False)),
                ('deleted_at', models.DateTimeField(blank=True, null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('created_by_agent', models.ForeignKey(blank=True, help_text='Agent that created this node, if applicable.', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_nodes', to='api.persistentagent')),
                ('filespace', models.ForeignKey(help_text='The filesystem root this node belongs to.', on_delete=django.db.models.deletion.CASCADE, related_name='nodes', to='api.agentfilespace')),
                ('parent', models.ForeignKey(blank=True, help_text='Parent directory; null means the node is at the filespace root.', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='api.agentfsnode')),
            ],
            options={
                'ordering': ['node_type', 'name'],
            },
        ),
        migrations.AddIndex(
            model_name='agentfilespaceaccess',
            index=models.Index(fields=['agent', 'is_default'], name='afs_access_default_idx'),
        ),
        migrations.AddIndex(
            model_name='agentfilespaceaccess',
            index=models.Index(fields=['filespace', 'role'], name='afs_access_role_idx'),
        ),
        migrations.AddConstraint(
            model_name='agentfilespaceaccess',
            constraint=models.UniqueConstraint(fields=('filespace', 'agent'), name='unique_agent_filespace_access'),
        ),
        migrations.AddIndex(
            model_name='agentfilespace',
            index=models.Index(fields=['owner_user', '-created_at'], name='afs_owner_recent_idx'),
        ),
        migrations.AddIndex(
            model_name='agentfilespace',
            index=models.Index(fields=['name'], name='afs_name_idx'),
        ),
        migrations.AddConstraint(
            model_name='agentfilespace',
            constraint=models.UniqueConstraint(fields=('owner_user', 'name'), name='unique_filespace_per_user_name'),
        ),
        migrations.AddIndex(
            model_name='agentfsnode',
            index=models.Index(fields=['filespace', 'parent', 'node_type', 'name'], name='fs_list_idx'),
        ),
        migrations.AddIndex(
            model_name='agentfsnode',
            index=models.Index(fields=['filespace', 'path'], name='fs_path_idx'),
        ),
        migrations.AddIndex(
            model_name='agentfsnode',
            index=models.Index(fields=['node_type'], name='fs_type_idx'),
        ),
        migrations.AddIndex(
            model_name='agentfsnode',
            index=models.Index(fields=['created_at'], name='fs_created_idx'),
        ),
        migrations.AddConstraint(
            model_name='agentfsnode',
            constraint=models.UniqueConstraint(fields=('filespace', 'parent', 'name'), name='unique_name_per_directory'),
        ),
        migrations.AddConstraint(
            model_name='agentfsnode',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('content__isnull', True), ('node_type', 'dir')), ('node_type', 'file'), _connector='OR'), name='chk_dir_has_no_content'),
        ),
    ]
