from datetime import datetime

from sqlalchemy import Column, Integer, String, Boolean, Float, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class Game(Base):
    __tablename__ = 'game'
    game_id = Column(String, primary_key=True)
    linked_game = Column(String)
    timer = Column(Integer)
    rounds = Column(Integer)
    country_lock = Column(String)
    generation_method = Column(String)
    rule_set = Column(String)
    coordinates = relationship("Coordinate", lazy=True, order_by="Coordinate.round_number")
    players = relationship("Player", lazy=True, backref="game")


class Player(Base):
    __tablename__ = "player"
    player_id = Column(Integer, primary_key=True, autoincrement=True)
    game_id = Column(String, ForeignKey("game.game_id"))
    player_name = Column(String)
    guesses = relationship("Guess", lazy=True, order_by="Guess.round_number")


class Coordinate(Base):
    __tablename__ = "coordinate"
    game_id = Column(String, ForeignKey("game.game_id"), primary_key=True)
    round_number = Column(Integer, primary_key=True, autoincrement=False)
    country_code = Column(String)
    latitude = Column(Float)
    longitude = Column(Float)


class Guess(Base):
    __tablename__ = "guess"
    player_id = Column(Integer, ForeignKey("player.player_id"), primary_key=True, autoincrement=False)
    round_number = Column(Integer, primary_key=True, autoincrement=False)
    latitude = Column(Float)
    longitude = Column(Float)
    country_code = Column(String)
    round_score = Column(Integer)
    time_remaining = Column(Float)
    created_at = Column(DateTime, default=datetime.utcnow)
