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 Column, Integer, String, 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 = Column(Integer,primary_key = True)
    email = Column(String(200),unique = True, nullable = False)
    username = Column(String(200),unique = True,nullable = False)
    password = Column(String(200),nullable = False)
    role = Column(Enum(Roles), nullable=False)
    blockchain_address = Column(String(42),unique = True)
    # company_name = Column(String(150))
    # accredition_details = Column(String(255))
    
    def __init__(self,username,email,password,role,blockchain_address=None):
        self.username = username
        self.email = email
        self.password = self.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():
    data = User.query.all()
    
    if not data:
        return {"message": "No users found in the database"}, 404
    
    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)


  ~ ❯ curl -X POST http://localhost:5000/setdate -H "Content-Type: application/json" -d '{"username": "john_doe", "email": "john@example.com", "password": "123456", "role": "user", "blockchain_address": "0x12345..."}'