from django.db import models

class Agency(models.Model):
    agency_id = models.CharField(max_length=255, primary_key=True)
    agency_name = models.CharField(max_length=255)
    agency_url = models.URLField()
    agency_timezone = models.CharField(max_length=255)
    agency_lang = models.CharField(max_length=2, blank=True, null=True)
    agency_phone = models.CharField(max_length=50, blank=True, null=True)
    agency_fare_url = models.URLField(blank=True, null=True)
    agency_email = models.EmailField(blank=True, null=True)

class Stop(models.Model):
    stop_id = models.CharField(max_length=255, primary_key=True)
    stop_code = models.CharField(max_length=50, blank=True, null=True)
    stop_name = models.CharField(max_length=255)
    stop_desc = models.TextField(blank=True, null=True)
    stop_lat = models.FloatField()
    stop_lon = models.FloatField()
    zone_id = models.CharField(max_length=255, blank=True, null=True)
    stop_url = models.URLField(blank=True, null=True)
    location_type = models.IntegerField(blank=True, null=True)
    parent_station = models.ForeignKey('self', on_delete=models.SET_NULL, blank=True, null=True)
    stop_timezone = models.CharField(max_length=255, blank=True, null=True)
    wheelchair_boarding = models.IntegerField(blank=True, null=True)
    level_id = models.CharField(max_length=255, blank=True, null=True)
    platform_code = models.CharField(max_length=50, blank=True, null=True)

class Route(models.Model):
    route_id = models.CharField(max_length=255, primary_key=True)
    agency = models.ForeignKey(Agency, on_delete=models.CASCADE, blank=True, null=True)
    route_short_name = models.CharField(max_length=50)
    route_long_name = models.CharField(max_length=255, blank=True, null=True)
    route_desc = models.TextField(blank=True, null=True)
    route_type = models.IntegerField(default=0)
    route_url = models.URLField(blank=True, null=True)
    route_color = models.CharField(max_length=6, blank=True, null=True)
    route_text_color = models.CharField(max_length=6, blank=True, null=True)
    route_sort_order = models.IntegerField(blank=True, null=True)
    continuous_pickup = models.IntegerField(blank=True, null=True)
    continuous_drop_off = models.IntegerField(blank=True, null=True)

class Trip(models.Model):
    trip_id = models.CharField(max_length=255, primary_key=True)
    route = models.ForeignKey(Route, on_delete=models.CASCADE)
    service_id = models.CharField(max_length=255)
    trip_headsign = models.CharField(max_length=255, blank=True, null=True)
    trip_short_name = models.CharField(max_length=255, blank=True, null=True)
    direction_id = models.IntegerField(blank=True, null=True)
    block_id = models.CharField(max_length=255, blank=True, null=True)
    shape_id = models.CharField(max_length=255, blank=True, null=True)
    wheelchair_accessible = models.IntegerField(blank=True, null=True)
    bikes_allowed = models.IntegerField(blank=True, null=True)

class StopTime(models.Model):
    trip = models.ForeignKey(Trip, on_delete=models.CASCADE)
    arrival_time = models.TimeField(blank=True, null=True)
    departure_time = models.TimeField(blank=True, null=True)
    stop = models.ForeignKey(Stop, on_delete=models.CASCADE)
    stop_sequence = models.IntegerField()
    stop_headsign = models.CharField(max_length=255, blank=True, null=True)
    pickup_type = models.IntegerField(blank=True, null=True)
    drop_off_type = models.IntegerField(blank=True, null=True)
    shape_dist_traveled = models.FloatField(blank=True, null=True)
    timepoint = models.IntegerField(blank=True, null=True)

    class Meta:
        unique_together = (('trip', 'stop_sequence'),)

class Calendar(models.Model):
    service_id = models.CharField(max_length=255, primary_key=True)
    monday = models.BooleanField()
    tuesday = models.BooleanField()
    wednesday = models.BooleanField()
    thursday = models.BooleanField()
    friday = models.BooleanField()
    saturday = models.BooleanField()
    sunday = models.BooleanField()
    start_date = models.DateField()
    end_date = models.DateField()

class CalendarDate(models.Model):
    service_id = models.CharField(max_length=255)
    date = models.DateField()
    exception_type = models.IntegerField()

    class Meta:
        unique_together = (('service_id', 'date'),)

class FareAttribute(models.Model):
    fare_id = models.CharField(max_length=255, primary_key=True)
    price = models.FloatField()
    currency_type = models.CharField(max_length=3)
    payment_method = models.IntegerField()
    transfers = models.IntegerField()
    agency = models.ForeignKey(Agency, on_delete=models.CASCADE, blank=True, null=True)
    transfer_duration = models.IntegerField(blank=True, null=True)

class FareRule(models.Model):
    fare = models.ForeignKey(FareAttribute, on_delete=models.CASCADE)
    route = models.ForeignKey(Route, on_delete=models.CASCADE, blank=True, null=True)
    origin_id = models.CharField(max_length=255, blank=True, null=True)
    destination_id = models.CharField(max_length=255, blank=True, null=True)
    contains_id = models.CharField(max_length=255, blank=True, null=True)

class Shape(models.Model):
    shape_id = models.CharField(max_length=255)
    shape_pt_lat = models.FloatField()
    shape_pt_lon = models.FloatField()
    shape_pt_sequence = models.IntegerField()
    shape_dist_traveled = models.FloatField(blank=True, null=True)

    class Meta:
        unique_together = (('shape_id', 'shape_pt_sequence'),)

class Frequency(models.Model):
    trip = models.ForeignKey(Trip, on_delete=models.CASCADE)
    start_time = models.TimeField()
    end_time = models.TimeField()
    headway_secs = models.IntegerField()
    exact_times = models.IntegerField(blank=True, null=True)

class Transfer(models.Model):
    from_stop = models.ForeignKey(Stop, on_delete=models.CASCADE, related_name='transfers_from')
    to_stop = models.ForeignKey(Stop, on_delete=models.CASCADE, related_name='transfers_to')
    transfer_type = models.IntegerField()
    min_transfer_time = models.IntegerField(blank=True, null=True)

    class Meta:
        unique_together = (('from_stop', 'to_stop'),)

class Pathway(models.Model):
    pathway_id = models.CharField(max_length=255, primary_key=True)
    from_stop = models.ForeignKey(Stop, on_delete=models.CASCADE, related_name='pathways_from')
    to_stop = models.ForeignKey(Stop, on_delete=models.CASCADE, related_name='pathways_to')
    pathway_mode = models.IntegerField()
    is_bidirectional = models.IntegerField()
    length = models.FloatField(blank=True, null=True)
    traversal_time = models.IntegerField(blank=True, null=True)
    stair_count = models.IntegerField(blank=True, null=True)
    max_slope = models.FloatField(blank=True, null=True)
    min_width = models.FloatField(blank=True, null=True)
    signposted_as = models.CharField(max_length=255, blank=True, null=True)
    reversed_signposted_as = models.CharField(max_length=255, blank=True, null=True)

class Level(models.Model):
    level_id = models.CharField(max_length=255, primary_key=True)
    level_index = models.FloatField()
    level_name = models.CharField(max_length=255, blank=True, null=True)

class FeedInfo(models.Model):
    feed_publisher_name = models.CharField(max_length=255)
    feed_publisher_url = models.URLField()
    feed_lang = models.CharField(max_length=255)
    default_lang = models.CharField(max_length=255, blank=True, null=True)
    feed_start_date = models.DateField(blank=True, null=True)
    feed_end_date = models.DateField(blank=True, null=True)
    feed_version = models.CharField(max_length=255, blank=True, null=True)
    feed_contact_email = models.EmailField(blank=True, null=True)
    feed_contact_url = models.URLField(blank=True, null=True)
    feed_id = models.BigAutoField(primary_key=True)

