# Generated manually for User Agreement / Privacy Policy

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
from django.utils import timezone


def seed_legal_documents(apps, schema_editor):
    LegalDocument = apps.get_model('accounts', 'LegalDocument')
    now = timezone.now()
    defaults_ua = {
        'title': 'Truck Me User Agreement',
        'body': (
            '<h1>Truck Me User Agreement</h1>'
            '<p>This User Agreement governs your use of the Truck Me platform '
            'as a shipper, transporter, or driver. By accepting, you confirm that you have '
            'read and agree to these terms.</p>'
            '<p>Version 1.0 — November 2026. Replace this placeholder with final legal text.</p>'
        ),
        'is_active': True,
        'effective_at': now,
    }
    defaults_pp = {
        'title': 'Truck Me Privacy Policy',
        'body': (
            '<h1>Truck Me Privacy Policy</h1>'
            '<p>This Privacy Policy describes how Truck Me collects, uses, and protects '
            'personal information. This document is view-only and does not require a signature.</p>'
            '<p>Version 1.0 — November 2026. Replace this placeholder with final legal text.</p>'
        ),
        'is_active': True,
        'effective_at': now,
    }
    LegalDocument.objects.get_or_create(
        doc_type='USER_AGREEMENT',
        version='1.0',
        locale='en',
        defaults=defaults_ua,
    )
    LegalDocument.objects.get_or_create(
        doc_type='PRIVACY_POLICY',
        version='1.0',
        locale='en',
        defaults=defaults_pp,
    )


def unseed_legal_documents(apps, schema_editor):
    LegalDocument = apps.get_model('accounts', 'LegalDocument')
    LegalDocument.objects.filter(version='1.0', locale='en').delete()


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('accounts', '0036_contactquery_and_queries_permissions'),
    ]

    operations = [
        migrations.AddField(
            model_name='userrole',
            name='user_agreement_accepted',
            field=models.BooleanField(default=False),
        ),
        migrations.AddField(
            model_name='userrole',
            name='user_agreement_accepted_at',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AddField(
            model_name='userrole',
            name='user_agreement_version',
            field=models.CharField(blank=True, default='', max_length=32),
        ),
        migrations.CreateModel(
            name='LegalDocument',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('doc_type', models.CharField(choices=[('USER_AGREEMENT', 'User Agreement'), ('PRIVACY_POLICY', 'Privacy Policy')], db_index=True, max_length=32)),
                ('version', models.CharField(max_length=32)),
                ('title', models.CharField(max_length=255)),
                ('body', models.TextField(help_text='HTML or plain text; same content for all mobile roles.')),
                ('locale', models.CharField(default='en', max_length=10)),
                ('is_active', models.BooleanField(db_index=True, default=False)),
                ('effective_at', models.DateTimeField(blank=True, null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
            ],
            options={
                'verbose_name': 'Legal document',
                'verbose_name_plural': 'Legal documents',
                'ordering': ['-effective_at', '-id'],
            },
        ),
        migrations.CreateModel(
            name='UserAgreementAcceptance',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('agreement_version', models.CharField(max_length=32)),
                ('accepted_at', models.DateTimeField()),
                ('ip_address', models.GenericIPAddressField(blank=True, null=True)),
                ('user_agent', models.CharField(blank=True, default='', max_length=512)),
                ('signature_file', models.FileField(blank=True, null=True, upload_to='agreements/signatures/%Y/%m/%d/')),
                ('full_name_at_accept', models.CharField(blank=True, default='', max_length=255)),
                ('email_at_accept', models.EmailField(blank=True, default='', max_length=254)),
                ('phone_at_accept', models.CharField(blank=True, default='', max_length=50)),
                ('account_type_at_accept', models.CharField(blank=True, default='', help_text='SHIPPER | TRANSPORTER | DRIVER | TRANSPORTER_DRIVER', max_length=32)),
                ('pdf_file', models.FileField(blank=True, null=True, upload_to='agreements/pdfs/%Y/%m/%d/')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('agreement_document', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='acceptances', to='accounts.legaldocument')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='agreement_acceptances', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'User agreement acceptance',
                'verbose_name_plural': 'User agreement acceptances',
                'ordering': ['-accepted_at', '-id'],
            },
        ),
        migrations.AddConstraint(
            model_name='legaldocument',
            constraint=models.UniqueConstraint(fields=('doc_type', 'version', 'locale'), name='uniq_legal_doc_type_version_locale'),
        ),
        migrations.AddConstraint(
            model_name='useragreementacceptance',
            constraint=models.UniqueConstraint(fields=('user', 'agreement_document'), name='uniq_user_agreement_document'),
        ),
        migrations.RunPython(seed_legal_documents, unseed_legal_documents),
    ]
