# Generated by Django 5.2.17 on 2026-09-26 17:38

import django.contrib.postgres.constraints
import django.contrib.postgres.fields.ranges
import django.db.models.deletion
import django.utils.timezone
import uuid
from django.db import migrations, models
from django.contrib.postgres.operations import BtreeGistExtension


class Migration(migrations.Migration):

    dependencies = [
        ('customers', '0002_customeraddress_customercontact'),
        ('devices', '0002_purchase_warranty_evidence'),
    ]

    operations = [
        BtreeGistExtension(),
        migrations.CreateModel(
            name='CustomerDeviceRelationship',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('relationship_type', models.CharField(choices=[('OWNER', 'Owner')], default='OWNER', max_length=16)),
                ('started_at', models.DateTimeField(default=django.utils.timezone.now)),
                ('ended_at', models.DateTimeField(blank=True, null=True)),
                ('end_reason', models.CharField(blank=True, max_length=2000)),
                ('note', models.CharField(blank=True, max_length=2000)),
                ('customer', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='device_relationships', to='customers.customer')),
                ('device', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='customer_relationships', to='devices.device')),
            ],
            options={
                'ordering': ['-started_at', 'id'],
                'constraints': [models.CheckConstraint(condition=models.Q(('relationship_type', 'OWNER')), name='devices_relationship_type_valid'), models.CheckConstraint(condition=models.Q(('ended_at__isnull', True), ('ended_at__gte', models.F('started_at')), _connector='OR'), name='devices_owner_period_valid'), models.UniqueConstraint(condition=models.Q(('ended_at__isnull', True), ('relationship_type', 'OWNER')), fields=('device',), name='devices_one_current_owner'), django.contrib.postgres.constraints.ExclusionConstraint(condition=models.Q(('relationship_type', 'OWNER')), expressions=[('device', '='), (models.Func('started_at', 'ended_at', django.contrib.postgres.fields.ranges.RangeBoundary(), function='TSTZRANGE', output_field=django.contrib.postgres.fields.ranges.DateTimeRangeField()), '&&')], name='devices_owner_period_no_overlap')],
            },
        ),
    ]
