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)

class LocationsGeojson(models.Model):
    location_id = models.CharField(max_length=255, primary_key=True)
    location_name = models.CharField(max_length=255)
    location_lat = models.FloatField()
    location_lon = models.FloatField()
    location_type = models.CharField(max_length=255)
    parent_location_id = models.CharField(max_length=255, blank=True, null=True)
    wheelchair_boarding = models.BooleanField(blank=True, null=True)

class BookingRule(models.Model):
    booking_rule_id = models.CharField(max_length=255, primary_key=True)
    trip = models.ForeignKey(Trip, on_delete=models.CASCADE)
    start_time = models.TimeField(blank=True, null=True)
    end_time = models.TimeField(blank=True, null=True)
    booking_type = models.CharField(max_length=255)
    rule_criteria = models.TextField(blank=True, null=True)
    booking_rule_instructions = models.TextField(blank=True, null=True)

class Translation(models.Model):
    table_name = models.CharField(max_length=255)
    field_name = models.CharField(max_length=255)
    language = models.CharField(max_length=2)
    translation = models.TextField()

class Attribution(models.Model):
    attribution_id = models.CharField(max_length=255, primary_key=True)
    agency = models.ForeignKey(Agency, on_delete=models.CASCADE)
    attribution_name = models.CharField(max_length=255)
    attribution_url = models.URLField()
    attribution_email = models.EmailField(blank=True, null=True)
    attribution_phone = models.CharField(max_length=50, blank=True, null=True)

class LocationGroup(models.Model):
    location_group_id = models.CharField(max_length=255, primary_key=True)
    location_group_name = models.CharField(max_length=255)
    location_group_type = models.CharField(max_length=255)

class LocationGroupStop(models.Model):
    location_group = models.ForeignKey(LocationGroup, on_delete=models.CASCADE)
    stop = models.ForeignKey(Stop, on_delete=models.CASCADE)

class RouteNetwork(models.Model):
    route_network_id = models.CharField(max_length=255, primary_key=True)
    route_network_name = models.CharField(max_length=255)

class Network(models.Model):
    network_id = models.CharField(max_length=255, primary_key=True)
    network_name = models.CharField(max_length=255)

class StopArea(models.Model):
    stop_area_id = models.CharField(max_length=255, primary_key=True)
    stop_area_name = models.CharField(max_length=255)
    stop_area_description = models.TextField(blank=True, null=True)

class Area(models.Model):
    area_id = models.CharField(max_length=255, primary_key=True)
    area_name = models.CharField(max_length=255)
    area_description = models.TextField(blank=True, null=True)

class FareMedium(models.Model):
    fare_media_id = models.CharField(max_length=255, primary_key=True)
    fare_media_name = models.CharField(max_length=255)
    fare_media_description = models.TextField(blank=True, null=True)

class FareProduct(models.Model):
    fare_product_id = models.CharField(max_length=255, primary_key=True)
    fare_product_name = models.CharField(max_length=255)
    fare_product_description = models.TextField(blank=True, null=True)

class FareLegRule(models.Model):
    fare_leg_rule_id = models.CharField(max_length=255, primary_key=True)
    fare_leg_rule_name = models.CharField(max_length=255)
    fare_leg_rule_description = models.TextField(blank=True, null=True)

class FareTransferRule(models.Model):
    fare_transfer_rule_id = models.CharField(max_length=255, primary_key=True)
    fare_transfer_rule_name = models.CharField(max_length=255)
    fare_transfer_rule_description = models.TextField(blank=True, null=True)

class Timeframe(models.Model):
    time_frame_id = models.CharField(max_length=255, primary_key=True)
    start_date = models.DateField()
    end_date = models.DateField()
    start_time = models.TimeField()
    end_time = models.TimeField()

