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

import apps.recepciones.models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ("catalogos", "0001_initial"),
        ("boletas", "0001_initial"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="Recepcion",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("created_at", models.DateTimeField(auto_now_add=True, editable=False)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "estado",
                    models.CharField(
                        choices=[
                            ("identificacion", "Identificación"),
                            ("camion_registrado", "Camión registrado"),
                            ("peso_lleno", "Peso lleno registrado"),
                            ("muestreo_pendiente", "Muestreo pendiente"),
                            ("peso_vacio", "Peso vacío registrado"),
                            ("cerrada", "Cerrada"),
                            ("rechazada", "Rechazada"),
                            ("escalada", "Escalada"),
                            ("borrador", "Borrador"),
                        ],
                        default="identificacion",
                        max_length=20,
                    ),
                ),
                ("placas_camion", models.CharField(blank=True, max_length=20)),
                ("chofer_nombre", models.CharField(blank=True, max_length=100)),
                ("chofer_telefono", models.CharField(blank=True, max_length=20)),
                ("chofer_firma_en_dispositivo", models.BooleanField(default=False)),
                ("observaciones", models.TextField(blank=True)),
                ("fecha_cierre", models.DateTimeField(blank=True, null=True)),
                ("firmas", models.JSONField(blank=True, default=dict)),
                ("alerta", models.JSONField(blank=True, default=dict)),
                (
                    "boleta",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="recepcion",
                        to="boletas.boleta",
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="%(app_label)s_%(class)s_created",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "updated_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="%(app_label)s_%(class)s_updated",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "permissions": [
                    ("process", "Puede procesar recepciones"),
                    ("close", "Puede cerrar recepciones"),
                ],
            },
        ),
        migrations.CreateModel(
            name="Deduccion",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("valor_deducido", models.DecimalField(decimal_places=2, max_digits=10)),
                (
                    "tipo_deduccion",
                    models.CharField(
                        choices=[("porcentaje", "Porcentaje"), ("fijo", "Fijo")],
                        max_length=10,
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "parametro",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        to="catalogos.parametro",
                    ),
                ),
                (
                    "recepcion",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="deducciones",
                        to="recepciones.recepcion",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="FotoRecepcion",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "tipo",
                    models.CharField(
                        choices=[
                            ("frontal", "Frontal"),
                            ("lateral", "Lateral"),
                            ("cargamento", "Cargamento"),
                            ("boleta_fisica", "Boleta física"),
                            ("otro", "Otro"),
                        ],
                        max_length=20,
                    ),
                ),
                (
                    "imagen",
                    models.ImageField(upload_to=apps.recepciones.models.foto_upload_path),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "recepcion",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="fotos",
                        to="recepciones.recepcion",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="Muestreo",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "tipo_muestreo",
                    models.CharField(
                        blank=True,
                        choices=[
                            ("aleatorio", "Aleatorio"),
                            ("estratificado", "Estratificado"),
                            ("manual", "Manual"),
                        ],
                        max_length=20,
                        null=True,
                    ),
                ),
                (
                    "estado",
                    models.CharField(
                        choices=[
                            ("pendiente", "Pendiente"),
                            ("tomado", "Muestra tomada"),
                            ("en_laboratorio", "En laboratorio"),
                            ("completado", "Completado"),
                        ],
                        default="pendiente",
                        max_length=20,
                    ),
                ),
                ("fecha_asignacion", models.DateTimeField(auto_now_add=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "recepcion",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="muestreo",
                        to="recepciones.recepcion",
                    ),
                ),
                (
                    "tecnico_asignado",
                    models.ForeignKey(
                        blank=True,
                        limit_choices_to={"groups__name": "Técnicos"},
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="recepciones_muestreos",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="PesoRecepcion",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "tipo",
                    models.CharField(choices=[("lleno", "Lleno"), ("vacio", "Vacío")], max_length=10),
                ),
                ("valor", models.DecimalField(decimal_places=2, max_digits=10)),
                ("es_automatico", models.BooleanField(default=True)),
                ("motivo_manual", models.TextField(blank=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "recepcion",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="pesos",
                        to="recepciones.recepcion",
                    ),
                ),
            ],
        ),
        migrations.AddConstraint(
            model_name="pesorecepcion",
            constraint=models.UniqueConstraint(
                fields=("recepcion", "tipo"),
                name="uq_peso_por_recepcion_tipo",
            ),
        ),
    ]
