import sqlite3
from db import db

class UserModel(db.Model):# class for info about the user
    __tablename__ ="users"
    print("made tablename users: location UserModel")
    id = db.Column(db.Integer, primary_key=True) #
    username = db.Column(db.String(80))
    password = db.Column(db.String(80))


    def __init__(self, _id, username, password):# constructor when class User is called
        self.id = _id
        self.username = username
        self.password = password

    @classmethod
    def find_by_username(cls, username):
        print("find_by_username")
        connection =sqlite3.connect('data.db')
        cursor = connection.cursor()

        query = "SELECT * FROM users WHERE username=?"
        result =cursor.execute(query, (username,))
        print("query executed")
        row = result.fetchone()
        if row:
            user = cls(*row)
        else:
            user=None

        connection.close()
        return user

    @classmethod
    def find_by_id(cls, _id):
        connection =sqlite3.connect('data.db')
        cursor = connection.cursor()
        print("find_by_id")
        query = "SELECT * FROM users WHERE id=?"
        result =cursor.execute(query, (_id,))
        print("query executed")
        row = result.fetchone()
        if row:
            user = cls(*row)
        else:
            user=None

        connection.close()
        return user
