from django.shortcuts import render,HttpResponse,redirect
from django.conf import settings
# Create your views here.
# Create your views here.
from django.contrib import messages
from visitors_app.models import user,roles,countries,states,cities,location,gate_pass_template
from django.contrib.auth.hashers import check_password
from visitors_app.context_processors import my_constants
from django.shortcuts import get_object_or_404
from django.http import JsonResponse
from visitors_app.views import date_time
from django.db import connection
from django.template import engines
from django.views.decorators.http import require_POST


def admin_location_add(request):
    username = request.session.get('admin')
    if not username :
        return redirect('admin')
    countrie = countries.objects.all()
    gate_pass_template_id_all = gate_pass_template.objects.all()
    if request.method == 'POST':
        country_id = request.POST['country_id']
        state_id = request.POST['state_id']
        city_id = request.POST['city_id']
        location_name = request.POST['location_name']
        address = request.POST['address']
        status =request.POST['status']
        gate_pass_template_ides = request.POST['gate_pass_template_id']
        created_at = date_time()
        locationes = location(country_id=country_id, state_id=state_id, city_id=city_id,location_name=location_name,address=address,gate_pass_template_id=gate_pass_template_ides,status=status,created_at=created_at)
        locationes.save()
        messages.success(request, "location successfully updated.", extra_tags="success")
        return redirect('admin_location')
    return render(request, 'dashboard/admin_dashboard/admin_location/admin_location_add.html', {'countrie':countrie,'gate_pass_template_id_all':gate_pass_template_id_all})


def admin_location_ajax_load_states(request):
    username = request.session.get('admin')
    if not username :
        return redirect('admin')
    country_id = request.GET.get('country_id')

    state = states.objects.filter(country_id=country_id).all()

    return JsonResponse(list(state.values('id', 'name')), safe=False)


def admin_location_ajax_load_cities(request):
    username = request.session.get('admin')
    if not username :
        return redirect('admin')
    state_id = request.GET.get('state_id')
    city = cities.objects.filter(state_id=state_id).all()
    return JsonResponse(list(city.values('id', 'name')), safe=False)


def admin_location(request):
    username = request.session.get('admin')
    if not username :
        return redirect('admin')
    
    return render(request, 'dashboard/admin_dashboard/admin_location/admin_location_list.html')

def admin_location_ajax_page_ajax(request):
    constants = my_constants(request)
    username = request.session.get('admin')
    if not username:
        return redirect('admin')
    
    database_name = constants['database_name']
    user_data = constants.get('user_data', {})
    user_id = user_data.get('id')
    start = int(request.POST.get('start', 0))
    length = int(request.POST.get('length', 10))
    search_value = request.POST.get('search[value]', '')

    search = ''
    search_params = []

    if search_value:
        search = f"AND (countries.name LIKE %s OR states.name LIKE %s OR cities.name LIKE %s OR location.location_name LIKE %s)"
        search_params = [f"%{search_value}%"] * 4

    # First SQL query to get the count of records
    count_query = f"""
        SELECT
            COUNT(*) AS appointment_count
        FROM
            {database_name}.location
        INNER JOIN
            {database_name}.countries AS countries ON countries.id = location.country_id
        INNER JOIN
            {database_name}.states AS states ON states.id = location.state_id
        INNER JOIN
            {database_name}.cities AS cities ON cities.id = location.city_id
        WHERE
            1 = 1 {search};
    """
    
    with connection.cursor() as cursor:
        cursor.execute(count_query, search_params)
        database_all_data = cursor.fetchone()
    
    recordsTotal = database_all_data[0] if database_all_data else 0

    # Second SQL query to get detailed records
    data_query = f"""
        SELECT
            location.*,
            countries.name AS countries_name,
            states.name AS states_name,
            cities.name AS cities_name
        FROM
            {database_name}.location
        INNER JOIN
            {database_name}.countries AS countries ON countries.id = location.country_id
        INNER JOIN
            {database_name}.states AS states ON states.id = location.state_id
        INNER JOIN
            {database_name}.cities AS cities ON cities.id = location.city_id
        WHERE
            1 = 1 {search}
        ORDER BY
            location.id DESC
        LIMIT %s OFFSET %s;
    """
    
    with connection.cursor() as cursor:
        cursor.execute(data_query, search_params + [length, start])
        database_all_data = cursor.fetchall()
        columns = [col[0] for col in cursor.description]
        all_coupon_dtl = [dict(zip(columns, row)) for row in database_all_data]
    
    return JsonResponse({"recordsTotal": recordsTotal, "recordsFiltered": recordsTotal, 'data': all_coupon_dtl})
    
    
    
def admin_location_delete(request,id):
    username = request.session.get('admin')
    if not username :
        return redirect('admin')
    all_location = get_object_or_404(location,id=id)
    all_location.delete()
    data = {
        'status': 1,
        'message': f'Location Deleted Successfully.',
    }
    messages.success(request, f"successfully Location Deleted!", extra_tags="success")
    return JsonResponse(data)


def admin_location_edit(request,id):
    constants = my_constants(request)
    username = request.session.get('admin')
    if not username :
        return redirect('admin')

    user_data = constants.get('admin_data', {})
    user_id = user_data.get('id')
    all_location = get_object_or_404(location,id=id)
    all_country = countries.objects.all()
    all_gate_pass_template = gate_pass_template.objects.all()
    state = states.objects.filter(id=all_location.state_id).first()
    citie = cities.objects.filter(id=all_location.city_id).first()
    gate_pass_template_id_selected = gate_pass_template.objects.filter(id=all_location.gate_pass_template_id).first()
    if request.method == "POST":
        all_location.country_id = request.POST['country_id']
        all_location.state_id = request.POST['state_id']
        all_location.city_id = request.POST['city_id']
        all_location.location_name = request.POST['location_name']
        all_location.address = request.POST['address']
        all_location.status = request.POST['status']
        all_lcation.gate_pass_template_id = request.POST['gate_pass_template_id']
        all_location.created_at = date_time()
        all_location.save()
        messages.success(request, "Location successfully updated.", extra_tags="success")
        return redirect('admin_location')
    return render(request, 'dashboard/admin_dashboard/admin_location/admin_location_edit.html', {
        'all_location': all_location,
        'all_country': all_country,
        'all_gate_pass_template':all_gate_pass_template,
        'state': state,
        'cities': citie,
        'gate_pass_template_id_selected':gate_pass_template_id_selected
        
    })
    
def get_gate_pass_templates(request):
    constants = my_constants(request)
    location_id = request.GET.get('location_id')
    templates = gate_pass_template.objects.all()
    selected_template_id = None

    if location_id:
        try:
            loc = location.objects.get(id=location_id)
            selected_template_id = loc.gate_pass_template_id
        except location.DoesNotExist:
            pass  # You can optionally handle this error

    data = []
    for template in templates:
        django_engine = engines['django']
        template_string = template.gate_pass_template
        if '{% load static %}' not in template_string:
            template_string = "{% load static %}" + template_string
        template_context = {
            'formatted_gate_pass_number': '001',
            'check_in_time': '',
            'check_out_time': '',
            'visitors_type': 'demo',
            'visitors_name': f"{'Dhruv'} {'Parikh'}",
            'visitors_image':constants.get('DEFAULT_IMAGE', ''),
            'visitors_address': 'demo',
            'visitors_mobile': '7600524348',
            'employee_name': 'Demo',
            'company_name': 'Demo company',
            'department_name': 'department_name',
            'location_name': 'location_name',
            'location_address':'location_address',
            'appointment': 'appointment',
            'GET_PASS_IMAGE': constants.get('GET_PASS_IMAGE', '')
        }

        rendered_gate_pass_template = django_engine.from_string(template_string).render(template_context)
        
        data.append({
            'id': template.id,
            'gate_pass_name': template.gate_pass_name,
            'template_html': rendered_gate_pass_template,
        })

    return JsonResponse({
        'templates': data,
        'selected_template_id': selected_template_id
    })
@require_POST
def save_location_gate_pass(request):
    location_id = request.POST.get('location_id')
    template_id = request.POST.get('template_id')

    if not (location_id and template_id):
        return JsonResponse({'success': False, 'error': 'Missing parameters'})

    try:
        locations = location.objects.get(id=location_id)
        template = gate_pass_template.objects.get(id=template_id)

        locations.gate_pass_template_id = template.id
        locations.save()

        return JsonResponse({'success': True})
    except location.DoesNotExist:
        return JsonResponse({'success': False, 'error': 'Location not found'})
    except gate_pass_template.DoesNotExist:
        return JsonResponse({'success': False, 'error': 'Template not found'})