## not in use as of now


from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render,redirect
from django.contrib import messages
import logging
from django.contrib.auth.models import User
from baseapp.internal import localfunc
from django.contrib.auth import authenticate, login, logout
logger = logging.getLogger('django')
from .internal import  initalrun
from datetime import datetime as dt
from .models import  country_state,  tags, patient, employee,disease,task,treatment,Zone,gender,Master_GST,District_State,Zone_Type,Zone_System,ID_type, User_Group


def error_500_view(request):
    exception = ''
    messages.error(request, exception +'Please check the Error, if it repeats contact Site Administrator.')
    print(request.build_absolute_uri())
    return redirect (request.build_absolute_uri()) # HttpResponseRedirect (request.build_absolute_uri())   
 
def get_db_models(request):    return initalrun.get_db_model_details_main()
def get_genScript(request, appname ='appname'):    return initalrun.get_db_model_genAI(request, appname)
def get_genScript_main(request):    return  HttpResponse(initalrun.get_db_model_genAI_main(request))
def update_inital_view2(request):    return HttpResponse('Process Completed. Please find below details.\n' + initalrun.update_datasets_models())
def html_backup(request):    return HttpResponse('Process Completed. Please find below details.\n' + initalrun.get_HTML_Backup())
def html_update(request):    return HttpResponse('Process Completed. Please find below details.\n' + initalrun.get_HTML_update())
def export_all_models(request):     return HttpResponse(initalrun.export_all_models())

def create_users_test(request):
    for obj_emply in employee.objects.all().order_by('-pk'):
        try:  # if entry has employee field
            if obj_emply.temp_password : str_newpass = obj_emply.temp_password 
            else: 
                str_newpass = localfunc.get_password(8)
                obj_emply.temp_password = str_newpass
                obj_emply.save(obj_emply)
            myuser = User.objects.create_user(obj_emply.str_username, obj_emply.email ,str_newpass)
            myuser.is_staff = True
            if obj_emply.pk ==1 : 
                myuser.is_superuser = True   # First Employee Number is system Admin 
                login(request, myuser) 
            myuser.save()  
            print('user Account is created for ', obj_emply.str_username, obj_emply.email ,str_newpass)
        except Exception as err:               messages.error(request, 'Employee details not created - ' +  str(obj_emply.pk) +  ' - ' + str(err), 'danger')  
    return HttpResponseRedirect('/')





# def create_users_bulk(request):
#     int_counter = 1 
#     str_status = 'Please find the below status <br>'
#     obj_employees =  employee.objects.all()
#     for obj_ug in User_Group.objects.all():
#         if int_counter !=1 : 
#             obj_emply = obj_employees.filter(pk = int_counter)#.update(pk = int_counter)
#             int_counter += 1 
#             try:  # if entry has employee field
#                 str_newpass = localfunc.get_password(8)
#                 myuser = User.objects.create_user(obj_emply.str_username, obj_emply.email ,str_newpass)
#                 str_status += "Employee login details <br> username: " + obj_emply.str_username
#                 str_status += '<br> password: '+ str_newpass
#                 str_status += '<br> UserGroup: ' + obj_ug.name
#                 myuser.is_staff = True
#                 myuser.save()  
#             except Exception as err:   messages.error(request, 'Employee details not created - ' +  str(int_counter) +  ' - ' + str(err), 'danger')  






