# Generated by Django 2.2.7 on 2019-12-18 10:26

import uuid

import django.contrib.gis.db.models.fields
import django.contrib.postgres.fields
import django.contrib.postgres.fields.jsonb
import django.contrib.postgres.functions
import django.core.serializers.json
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone

import mds.models
import rest_framework.utils.encoders


class Migration(migrations.Migration):

    replaces = [
        ("mds", "0001_initial"),
        ("mds", "0002_area_color"),
        ("mds", "0003_record_timestamp"),
        ("mds", "0004_eventrecord_point"),
        ("mds", "0005_auto_20190122_1300"),
        ("mds", "0006_provider_device_category"),
        ("mds", "0007_provider_api"),
        ("mds", "0008_provider_authentication"),
        ("mds", "0009_provider_agency_api_authentication"),
        ("mds", "0010_device_dn_battery_pct"),
        ("mds", "0011_added_index_to_polygon_and_area_models"),
        ("mds", "0012_update_to_mds_0_3_0"),
        ("mds", "0013_provider_last_start_time_polled"),
        ("mds", "0014_non_null_labels"),
        ("mds", "0015_null_dn_status"),
        ("mds", "0016_agency_api_configuration"),
        ("mds", "0017_change_bike_to_bicycle_in_choices_field"),
        ("mds", "0018_allow_blank_provider_base_api_url"),
        ("mds", "0019_delete_creation_deletion_date_fields_in_area_and_polygon"),
        ("mds", "0020_fields_consistency"),
        ("mds", "0021_rename_event_source"),
        ("mds", "0022_provider_colors"),
        ("mds", "0023_relabel_api_fields"),
        ("mds", "0024_provider_device_category_deprecation"),
        ("mds", "0025_remove_provider_device_category"),
        ("mds", "0026_alter_polygon_to_multipolygon"),
        ("mds", "0027_eventrecord_first_saved_at"),
        ("mds", "0028_pre_provider_operator"),
        ("mds", "0029_post_provider_operator_fill"),
        ("mds", "0030_post_provider_operator_default"),
        ("mds", "0031_post_eventrecord_saved_at_index"),
        ("mds", "0032_pre_device_saved_at"),
        ("mds", "0033_post_fill_device_saved_at"),
        ("mds", "0034_post_default_device_saved_at"),
        ("mds", "0035_pre_add_fields"),
        ("mds", "0036_pre_save_provider_cursors"),
        ("mds", "0037_remove_provider_last_start_time_polled"),
        ("mds", "0038_eventrecord_event_type_reason"),
        ("mds", "0039_alter_field_event_type_event_record"),
        ("mds", "0040_change_event_record_event_type_options"),
        ("mds", "0041_map_old_to_new_event_records"),
        ("mds", "0042_revert_0039_event_type"),
        ("mds", "0043_policy"),
        ("mds", "0044_add_compliance_list"),
        ("mds", "0045_add_fixed_price_on_policy_model"),
        ("mds", "0046_compliance_uniqueness"),
        ("mds", "0047_add_daily_penalty_20190924_0933"),
        ("mds", "0048_add_compliance_lag"),
        ("mds", "0049_pre_add_policy_config"),
        ("mds", "0050_pre_migrate_to_policy_config"),
        ("mds", "0051_fill_compliance_lag"),
        ("mds", "0052_required_compliance_lag"),
        ("mds", "0053_post_remove_policy_custom_fields"),
        ("mds", "0054_pre_compliance_saved_at_nullable"),
        ("mds", "0055_post_fill_compliance_saved_at"),
        ("mds", "0056_post_compliance_saved_at_not_nullable"),
        ("mds", "0057_pre_policy_geographies_add"),
        ("mds", "0058_post_policy_geograpies_fill"),
        ("mds", "0059_post_policy_config_encoder"),
    ]

    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name="Polygon",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4, primary_key=True, serialize=False
                    ),
                ),
                (
                    "label",
                    mds.models.UnboundedCharField(
                        blank=True, db_index=True, default=""
                    ),
                ),
                (
                    "geom",
                    django.contrib.gis.db.models.fields.MultiPolygonField(srid=4326),
                ),
                (
                    "properties",
                    django.contrib.postgres.fields.jsonb.JSONField(
                        default=dict, encoder=rest_framework.utils.encoders.JSONEncoder
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="Provider",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4, primary_key=True, serialize=False
                    ),
                ),
                ("name", mds.models.UnboundedCharField(blank=True, default="")),
                (
                    "logo_b64",
                    mds.models.UnboundedCharField(blank=True, default=None, null=True),
                ),
                ("operator", models.BooleanField(default=True)),
                (
                    "base_api_url",
                    mds.models.UnboundedCharField(
                        blank=True, default="", verbose_name="Base Provider API URL"
                    ),
                ),
                (
                    "oauth2_url",
                    mds.models.UnboundedCharField(
                        blank=True, default="", verbose_name="OAuth2 URL (if different)"
                    ),
                ),
                (
                    "api_authentication",
                    django.contrib.postgres.fields.jsonb.JSONField(
                        blank=True,
                        default=mds.models.provider_api_authentication_default,
                        verbose_name="Provider API Authentication",
                    ),
                ),
                (
                    "api_configuration",
                    django.contrib.postgres.fields.jsonb.JSONField(
                        blank=True,
                        default=mds.models.provider_api_configuration_default,
                        verbose_name="Provider API Configuration",
                    ),
                ),
                (
                    "last_event_time_polled",
                    models.DateTimeField(
                        blank=True,
                        null=True,
                        verbose_name="Last event time polled (start_time)",
                    ),
                ),
                (
                    "last_recorded_polled",
                    models.DateTimeField(
                        blank=True,
                        null=True,
                        verbose_name="Last recorded polled (start_recorded)",
                    ),
                ),
                (
                    "last_skip_polled",
                    models.PositiveIntegerField(
                        blank=True,
                        null=True,
                        verbose_name="Last skip polled (total_events)",
                    ),
                ),
                (
                    "agency_api_authentication",
                    django.contrib.postgres.fields.jsonb.JSONField(
                        blank=True,
                        default=mds.models.agency_api_authentication_default,
                        verbose_name="Agency API Authentication",
                    ),
                ),
                (
                    "agency_api_configuration",
                    django.contrib.postgres.fields.jsonb.JSONField(
                        blank=True,
                        default=mds.models.agency_api_configuration_default,
                        verbose_name="Agency API Configuration",
                    ),
                ),
                (
                    "colors",
                    django.contrib.postgres.fields.jsonb.JSONField(
                        blank=True,
                        default=dict,
                        validators=[mds.models.colors_validator],
                        verbose_name="colors for distinguishing provider icons",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="Policy",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4, primary_key=True, serialize=False
                    ),
                ),
                ("name", mds.models.UnboundedCharField()),
                ("description", mds.models.UnboundedCharField(blank=True, default="")),
                ("start_date", models.DateTimeField()),
                ("end_date", models.DateTimeField(blank=True, null=True)),
                ("published_date", models.DateTimeField(blank=True, null=True)),
                (
                    "rules",
                    django.contrib.postgres.fields.jsonb.JSONField(
                        default=list,
                        encoder=django.core.serializers.json.DjangoJSONEncoder,
                    ),
                ),
                (
                    "config",
                    django.contrib.postgres.fields.jsonb.JSONField(
                        default=dict,
                        encoder=django.core.serializers.json.DjangoJSONEncoder,
                    ),
                ),
                (
                    "geographies",
                    django.contrib.postgres.fields.jsonb.JSONField(
                        blank=True,
                        default=dict,
                        encoder=django.core.serializers.json.DjangoJSONEncoder,
                        null=True,
                    ),
                ),
                (
                    "prev_policies",
                    models.ManyToManyField(
                        blank=True,
                        related_name="_policy_prev_policies_+",
                        to="mds.Policy",
                    ),
                ),
                ("providers", models.ManyToManyField(blank=True, to="mds.Provider")),
            ],
            options={"verbose_name_plural": "policies"},
        ),
        migrations.CreateModel(
            name="Device",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4, primary_key=True, serialize=False
                    ),
                ),
                (
                    "registration_date",
                    models.DateTimeField(default=django.utils.timezone.now),
                ),
                ("identification_number", mds.models.UnboundedCharField()),
                (
                    "category",
                    mds.models.UnboundedCharField(
                        choices=[
                            ("bicycle", "Bicycle"),
                            ("scooter", "Scooter"),
                            ("car", "Car"),
                        ]
                    ),
                ),
                ("model", mds.models.UnboundedCharField(blank=True, default="")),
                (
                    "propulsion",
                    django.contrib.postgres.fields.ArrayField(
                        base_field=mds.models.UnboundedCharField(
                            choices=[
                                ("human", "Human"),
                                ("electric_assist", "Electric Assist"),
                                ("electric", "Electric"),
                                ("combustion", "Combustion"),
                            ]
                        ),
                        size=None,
                    ),
                ),
                ("year_manufactured", models.IntegerField(blank=True, null=True)),
                ("manufacturer", mds.models.UnboundedCharField(blank=True, default="")),
                ("saved_at", models.DateTimeField(auto_now=True, db_index=True)),
                ("dn_battery_pct", models.FloatField(blank=True, null=True)),
                (
                    "dn_gps_point",
                    django.contrib.gis.db.models.fields.PointField(
                        blank=True, null=True, srid=4326
                    ),
                ),
                ("dn_gps_timestamp", models.DateTimeField(blank=True, null=True)),
                (
                    "dn_status",
                    mds.models.UnboundedCharField(
                        choices=[
                            ("available", "Available"),
                            ("reserved", "Reserved"),
                            ("unavailable", "Unavailable"),
                            ("removed", "Removed"),
                            ("trip", "Trip"),
                            ("elsewhere", "Elsewhere"),
                            ("inactive", "Inactive"),
                            ("unknown", "Unknown"),
                        ],
                        default="unknown",
                    ),
                ),
                (
                    "provider",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="devices",
                        to="mds.Provider",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="Area",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4, primary_key=True, serialize=False
                    ),
                ),
                (
                    "label",
                    mds.models.UnboundedCharField(
                        blank=True, db_index=True, default=""
                    ),
                ),
                (
                    "color",
                    mds.models.UnboundedCharField(
                        default="#FFFFFF", help_text="hexa representation"
                    ),
                ),
                (
                    "area_type",
                    mds.models.UnboundedCharField(
                        choices=[
                            ("unrestricted", "Unrestricted"),
                            ("restricted", "Restricted"),
                            ("preferred_pick_up", "Preferred pick up"),
                            ("preferred_drop_off", "Preferred drop off"),
                        ],
                        default="unrestricted",
                    ),
                ),
                (
                    "polygons",
                    models.ManyToManyField(
                        blank=True, related_name="areas", to="mds.Polygon"
                    ),
                ),
                (
                    "providers",
                    models.ManyToManyField(
                        blank=True, related_name="areas", to="mds.Provider"
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="EventRecord",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("timestamp", models.DateTimeField(db_index=True)),
                (
                    "point",
                    django.contrib.gis.db.models.fields.PointField(
                        blank=True, null=True, srid=4326
                    ),
                ),
                (
                    "saved_at",
                    models.DateTimeField(
                        db_index=True,
                        default=django.contrib.postgres.functions.TransactionNow,
                    ),
                ),
                ("first_saved_at", models.DateTimeField(blank=True, null=True)),
                (
                    "source",
                    mds.models.UnboundedCharField(
                        choices=[
                            ("agency_api", "Agency API"),
                            ("provider_api", "Provider API"),
                        ],
                        default="agency_api",
                    ),
                ),
                (
                    "event_type",
                    mds.models.UnboundedCharField(
                        choices=[
                            ("register", "Register"),
                            ("service_start", "Service start"),
                            ("service_end", "Service end"),
                            ("provider_drop_off", "Provider drop off"),
                            ("provider_pick_up", "Provider pick up"),
                            ("city_pick_up", "City pick up"),
                            ("reserve", "Reserve"),
                            ("cancel_reservation", "Cancel reservation"),
                            ("trip_start", "Trip start"),
                            ("trip_enter", "Trip enter"),
                            ("trip_leave", "Trip leave"),
                            ("trip_end", "Trip end"),
                            ("deregister", "Deregister"),
                            ("telemetry", "Received telemetry"),
                            ("battery_charged", "Battery charged"),
                            ("rebalance_drop_off", "Rebalance drop off"),
                            ("maintenance_drop_off", "Maintenance drop off"),
                            ("low_battery", "Low battery"),
                            ("maintenance", "Maintenance"),
                            ("rebalance_pick_up", "Rebalance pick up"),
                            ("maintenance_pick_up", "Maintenance pick up"),
                        ]
                    ),
                ),
                (
                    "event_type_reason",
                    mds.models.UnboundedCharField(
                        blank=True,
                        choices=[
                            ("low_battery", "Low battery"),
                            ("maintenance", "Maintenance"),
                            ("compliance", "Compliance"),
                            ("off_hours", "Off hours"),
                            ("rebalance", "Rebalance"),
                            ("charge", "Charge"),
                            ("missing", "Missing"),
                            ("decommissioned", "Decommissioned"),
                        ],
                        null=True,
                    ),
                ),
                (
                    "properties",
                    django.contrib.postgres.fields.jsonb.JSONField(
                        default=dict, encoder=rest_framework.utils.encoders.JSONEncoder
                    ),
                ),
                (
                    "device",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="event_records",
                        to="mds.Device",
                    ),
                ),
            ],
            options={"unique_together": {("device", "timestamp")}},
        ),
        migrations.CreateModel(
            name="Compliance",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4, primary_key=True, serialize=False
                    ),
                ),
                ("rule", models.UUIDField()),
                ("geography", models.UUIDField()),
                ("start_date", models.DateTimeField()),
                ("end_date", models.DateTimeField(blank=True, null=True)),
                ("lag", models.DurationField()),
                ("saved_at", models.DateTimeField(auto_now=True, db_index=True)),
                (
                    "policy",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="compliances",
                        to="mds.Policy",
                    ),
                ),
                (
                    "vehicle",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="compliances",
                        to="mds.Device",
                    ),
                ),
            ],
            options={
                "unique_together": {
                    ("policy", "rule", "geography", "vehicle", "start_date", "lag")
                },
            },
        ),
    ]
