from flask import Flask
from flask import render_template, request, redirect, url_for
from werkzeug.security import generate_password_hash, check_password_hash
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Enum
import enum
import os


file_path = os.path.abspath(os.getcwd())+"/todo.db"
app  = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+file_path 
db = SQLAlchemy(app)
class Roles(enum.Enum):
    ADMIN = "admin"
    AUDITOR = "auditor"
    PROJECT_OWNER = "project_owner"
    BUYER = "buyer"

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer,primary_key = True)
    email = db.Column(db.String(200),unique = True, nullable = False)
    username = db.Column(db.String(200),unique = True,nullable = False)
    password = db.Column(db.String(200),nullable = False)
    role = db.Column(db.Enum(Roles), nullable=False)
    blockchain_address = db.Column(db.String(42),unique = True)
    # company_name = db.Column(db.String(150))
    # accredition_details = db.Column(db.String(255))
    
    def __init__(self,username,email,password,role,blockchain_address=None):
        self.username = username
        self.email = email
        self.password = set_password(password)
        self.role = role
        self.blockchain_address = blockchain_address
        # for key, value in kwargs.items():
            # setattr(self, key, value)
    
    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)
    def __repr__(self):
        return f"User: {self.username}, User_id: {self.id}, email: {self.email}, password: {self.password}, role: {self.role} blockchain_address: {self.blockchain_address}"





@app.route("/",methods = ["GET"])
def home():
    return {"message":"Flask is running"},200

@app.route("/getdata", methods=["GET"])
def getdata():
    # Query all users from the database
    data = User.query.all()
    
    # Check if users exist in the database
    if not data:
        return {"message": "No users found in the database"}, 404
    
    # Convert user data to a list of dictionaries
    user_list = [
        {
            'id': user.id,
            'username': user.username,
            'email': user.email,
            'password': user.password,
            'role': user.role,
            'blockchain_address': user.blockchain_address
        }
        for user in data
    ]
    
    # Return the list of users
    return {'users': user_list}, 200

@app.route("/setdate",methods = ["POST"])
def setdate():
    data = request.get_json()
    username = data.get('username')
    email = data.get('email')
    password = data.get('password')
    role = data.get('role')
    blockchain_address = data.get('blockchain_address')
    if not username or not email:
        return {'error': 'Teri mummy degi kya?'}, 400
    new_user = User(username=username,email=email,password = password,role = role,blockchain_address = blockchain_address)
    db.session.add(new_user)
    db.session.commit()
    return {"message":"mummy aa gyi"},200




if __name__ == '__main__':
    with app.app_context():
        db.create_all()
    app.run(debug=True)

❯ python user.py
Traceback (most recent call last):
  File "/home/devansh/dev/web3/antiC/backend/models/user.py", line 20, in <module>
    class User(db.Model):
  File "/home/devansh/dev/web3/antiC/backend/models/user.py", line 22, in User
    id = db.Column(db.Integer,primary_key = True)
         ^^^^^^^^^
  File "/home/devansh/.local/lib/python3.12/site-packages/flask_sqlalchemy/extension.py", line 982, in __getattr__
    if name in mod.__all__:
               ^^^^^^^^^^^
AttributeError: module 'sqlalchemy' has no attribute '__all__'. Did you mean: '__file__'?