from datetime import datetime

from sqlalchemy import Table
from sqlalchemy.dialects.postgresql import UUID
import uuid

import Models
from Models.AnalyticsModel import todays_analytics
from app import db

matching_table = Table('matching', Models.Base.metadata,
                       db.Column('user_id', db.ForeignKey('user.id')),
                       db.Column('match_id', db.ForeignKey('matches.id')))


class MatchModel(Models.Base):
    __tablename__ = 'matches'

    id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    users = db.relationship(
        "UserModel",
        secondary=matching_table,
        back_populates="matches"
    )
    date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow())

    def __init__(self, **kwargs):
        self.date = datetime.utcnow()
        super().__init__(**kwargs)
        todays_analytics().matches_count += 1
