from flask import Flask,render_template,request,session,redirect,url_for,flash
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
from werkzeug.security import generate_password_hash,check_password_hash
from flask_login import login_user,logout_user,login_manager,LoginManager
from flask_login import login_required,current_user
from flask_mail import Mail
import json


with open('config.json','r') as c:
    params = json.load(c)["params"]

# MY db connection
local_server= True
app = Flask(__name__)
app.secret_key='ananthakrishnan'



# this is for getting unique user access
login_manager=LoginManager(app)
login_manager.login_view='login'


# SMTP MAIL SERVER SETTINGS

app.config.update(
    MAIL_SERVER='smtp.gmail.com',
    MAIL_PORT='465',
    MAIL_USE_SSL=True,
    MAIL_USERNAME=params['gmail-user'],
    MAIL_PASSWORD=params['<PASSWORD>']
)
mail = Mail(app)


@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))






app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:@localhost/dp_hms'
db=SQLAlchemy(app)




# class Test(db.Model):
    #id= db.Column(db.Integer,primary_key=True)
    #name= db.Column(db.String(100))
    #email= db.Column(db.String(100))


class User(UserMixin,db.Model):
    id=db.Column(db.Integer,primary_key=True)
    username=db.Column(db.String(50))
    email=db.Column(db.String(50),unique=True)
    password=db.Column(db.String(1000))
    role=db.Column(db.String(50))

    



class Contacts(UserMixin,db.Model):
    cid=db.Column(db.Integer,primary_key=True)
    email=db.Column(db.String(50))
    name=db.Column(db.String(50))
    description=db.Column(db.String(1000))  

class Patients(db.Model):
    pid=db.Column(db.Integer,primary_key=True)
    email=db.Column(db.String(50))
    name=db.Column(db.String(50))
    gender=db.Column(db.String(50))
    slot=db.Column(db.String(50))
    disease=db.Column(db.String(50))
    time=db.Column(db.String(50),nullable=False)
    date=db.Column(db.String(50),nullable=False)
    dept=db.Column(db.String(50))
    number=db.Column(db.String(50))

class Doctors(db.Model):
    did=db.Column(db.Integer,primary_key=True)
    email=db.Column(db.String(50))
    doctorname=db.Column(db.String(50))
    dept=db.Column(db.String(50))

class Trigr(db.Model):
    tid=db.Column(db.Integer,primary_key=True)
    pid=db.Column(db.Integer)
    email=db.Column(db.String(50))
    name=db.Column(db.String(50))
    action=db.Column(db.String(50))
    timestamp=db.Column(db.String(50))





@app.route('/')
def index():
    return render_template('index.html')


# @app.route('/test')
# def test():
    # try:
     #    Test.query.all()
     #   return 'My db is connected'
    # except:
     #   return 'My db not connected'





@app.route('/patients',methods=['POST','GET'])
@login_required
def patient():
    doct=db.engine.execute("SELECT * FROM `doctors`")

    if request.method=="POST":
        email=request.form.get('email')
        name=request.form.get('name')
        gender=request.form.get('gender')
        slot=request.form.get('slot')
        disease=request.form.get('disease')
        time=request.form.get('time')
        date=request.form.get('date')
        dept=request.form.get('dept')
        number=request.form.get('number')
        subject="HOSPITAL MANAGEMENT SYSTEM"
        query=db.engine.execute(f"INSERT INTO `patients` (`email`,`name`,	`gender`,`slot`,`disease`,`time`,`date`,`dept`,`number`) VALUES ('{email}','{name}','{gender}','{slot}','{disease}','{time}','{date}','{dept}','{number}')")

# mail starts from here

        # mail.send_message(subject, sender=params['gmail-user'], recipients=[email],body=f"YOUR bOOKING IS CONFIRMED THANKS FOR CHOOSING US \nYour Entered Details are :\nName: {name}\nSlot: {slot}")



        flash("Booking Confirmed","info")


    return render_template('patient.html',doct=doct)













@app.route('/doctors',methods=['POST','GET'])
@login_required
def doctors():
    
    if request.method=="POST":

        email=request.form.get('email')
        doctorname=request.form.get('doctorname')
        dept=request.form.get('dept')

        query=db.engine.execute(f"INSERT INTO `doctors` (`email`,`doctorname`,`dept`) VALUES ('{email}','{doctorname}','{dept}')")
        flash("Information is Stored","primary")

    return render_template('doctor.html')


    






@app.route('/contacts',methods=['POST','GET'])
def contacts():
    
    if request.method=="POST":

        email=request.form.get('email')
        name=request.form.get('doctorname')
        description=request.form.get('description')


        query=db.engine.execute(f"INSERT INTO `contacts` (`email`,`name`,`description`) VALUES ('{email}','{name}','{description}')")
        flash("Information is Stored , Thank you for contacting us  ","primary")

    return render_template('contact.html')


    












@app.route('/doctorsdetail')
def doctorsdetail():  
    query=db.engine.execute(f"SELECT * FROM `doctors`")
    return render_template('doctorsdetail.html',query=query)

@app.route('/patientsdetail')
def patientsdetail():  
    query=db.engine.execute(f"SELECT * FROM `patients`")
    return render_template('patientsdetail.html',query=query)


@app.route('/userdetail')
def userdetail():  
    query=db.engine.execute(f"SELECT * FROM `user`")
    return render_template('userdetail.html',query=query)



















@app.route('/about')
def about(): 
    return render_template('about.html')







@app.route('/bookings')
@login_required
def bookings(): 
    em=current_user.email
    query=db.engine.execute(f"SELECT * FROM `patients` WHERE email='{em}'")
    return render_template('booking.html',query=query)


@app.route("/edit/<string:pid>",methods=['POST','GET'])
@login_required
def edit(pid):
    posts=Patients.query.filter_by(pid=pid).first()
    if request.method=="POST":
        email=request.form.get('email')
        name=request.form.get('name')
        gender=request.form.get('gender')
        slot=request.form.get('slot')
        disease=request.form.get('disease')
        time=request.form.get('time')
        date=request.form.get('date')
        dept=request.form.get('dept')
        number=request.form.get('number')
        db.engine.execute(f"UPDATE `patients` SET `email` = '{email}', `name` = '{name}', `gender` = '{gender}', `slot` = '{slot}', `disease` = '{disease}', `time` = '{time}', `date` = '{date}', `dept` = '{dept}', `number` = '{number}' WHERE `patients`.`pid` = {pid}")
        flash("Slot is Updates","success")
        return redirect('/bookings')
    
    return render_template('edit.html',posts=posts)


@app.route("/delete/<string:pid>",methods=['POST','GET'])
@login_required
def delete(pid):
    db.engine.execute(f"DELETE FROM `patients` WHERE `patients`.`pid`={pid}")
    flash("Slot Deleted Successful","danger")
    return redirect('/bookings')






@app.route('/signup',methods=['POST','GET'])
def signup():
    if request.method == "POST":
        username=request.form.get('username')
        email=request.form.get('email')
        password=request.form.get('password')
        role=request.form.get('role')
        user=User.query.filter_by(email=email).first()
        if user:
            flash("Email Already Exist","warning")
            return render_template('/signup.html')
        encpassword=generate_password_hash(password)

        
        new_user=db.engine.execute(f"INSERT INTO `user` (`username`,`email`,`password`, `role`) VALUES ('{username}','{email}','{encpassword}','{role}')")

        # this is method 2 to save data in db
        # newuser=User(username=username,email=email,password=<PASSWORD>)
        # db.session.add(newuser)
        # db.session.commit()
        flash("Signup Succes Please Login","success")
        return render_template('login.html')

          

    return render_template('signup.html')





@app.route('/login',methods=['POST','GET'])
def login():
    if request.method == "POST":
        email=request.form.get('email')
        password=request.form.get('password')
        role=request.form.get('role')
        user=User.query.filter_by(email=email).first()

        if user and check_password_hash(user.password,password):
            if role == user.role:
                login_user(user)
                flash("Login Success","primary")
                return redirect(url_for('index'))
            else:
                flash("Invalid Role","warning")  
        else:
            flash("invalid credentials","danger")
            return render_template('login.html')    





    return render_template('login.html')






   



@app.route('/logout')
@login_required
def logout():
    logout_user()
    flash("Logout SuccessFul","warning")
    return redirect(url_for('index'))




@app.route('/test')
def test():
    try:
        Patients.query.all()
        return 'My database is Connected'
    except:
        return 'My db is not Connected'
    

@app.route('/details')
def details():
    # posts=Trigr.query.all()
    posts=db.engine.execute("SELECT * FROM `trigr`")
    return render_template('trigers.html',posts=posts)


@app.route('/search',methods=['POST','GET'])
@login_required
def search():
    if request.method=="POST":
        query=request.form.get('search')
        dept=Doctors.query.filter_by(dept=query).first()
        name=Doctors.query.filter_by(doctorname=query).first()
        if name:

            flash("Doctor is Available","info")
        else:

            flash("Doctor is Not Available","danger")
    return render_template('index.html')










app.run(debug=True)    

