import os
from django.db import models
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required, user_passes_test
from django.http import HttpResponseForbidden
from functools import wraps
from .models import Property, Appraisal, User, Project, Invoice


def group_required(*group_names):
    """Requires user membership in at least one of the groups passed in."""
    def in_groups(user):
        if user.is_authenticated and (user.is_superuser or bool(user.groups.filter(name__in=group_names))):
            return True
        return False
    return user_passes_test(in_groups, login_url='login')





@login_required

@group_required('Org admin')

def admin_dashboard(request):

    """Displays the admin dashboard."""

    return render(request, "core/admin_dashboard.html")



from django.http import HttpResponse







def health_check(request):



    return HttpResponse("OK")







def index(request):







    if request.user.is_authenticated:







        if request.user.groups.filter(name='Org admin').exists():







            return redirect('core:admin_dashboard')







        # elif request.user.groups.filter(name='Client').exists():







        #     return redirect('client_dashboard')







        else:







            # Fallback for users with no assigned group







            return redirect('login')







    return render(request, "core/index.html")

@login_required
def property_list(request):
    properties = Property.objects.all()
    statuses = Property.STATUS_CHOICES
    property_types = Property.PROPERTY_TYPE_CHOICES

    q = request.GET.get('q')
    status = request.GET.get('status')
    property_type = request.GET.get('property_type')

    if q:
        properties = properties.filter(
            models.Q(name__icontains=q) |
            models.Q(address__icontains=q) |
            models.Q(city__icontains=q) |
            models.Q(state__icontains=q) |
            models.Q(zip_code__icontains=q) |
            models.Q(description__icontains=q)
        )
    if status:
        properties = properties.filter(status=status)
    if property_type:
        properties = properties.filter(property_type=property_type)
    
    context = {
        'properties': properties,
        'statuses': statuses,
        'property_types': property_types,
    }

    return render(request, 'core/property_list.html', context)




@login_required
def appraisal_list(request):
    appraisals = Appraisal.objects.all()
    property_types = Property.PROPERTY_TYPE_CHOICES
    appraisers = User.objects.filter(groups__name='Internal')

    q = request.GET.get('q')
    property_type = request.GET.get('property_type')
    appraiser_id = request.GET.get('appraiser')
    start_date = request.GET.get('start_date')
    end_date = request.GET.get('end_date')

    if q:
        appraisals = appraisals.filter(
            models.Q(property__name__icontains=q) |
            models.Q(appraiser__username__icontains=q) |
            models.Q(notes__icontains=q)
        )
    if property_type:
        appraisals = appraisals.filter(property__property_type=property_type)
    if appraiser_id:
        appraisals = appraisals.filter(appraiser__id=appraiser_id)
    if start_date:
        appraisals = appraisals.filter(appraisal_date__gte=start_date)
    if end_date:
        appraisals = appraisals.filter(appraisal_date__lte=end_date)
    
    context = {
        'appraisals': appraisals,
        'property_types': property_types,
        'appraisers': appraisers
    }

    return render(request, 'core/appraisal_list.html', context)

@login_required
def user_list(request):
    users = User.objects.all()
    roles = User.ROLE_CHOICES

    q = request.GET.get('q')
    role = request.GET.get('role')

    if q:
        users = users.filter(
            models.Q(username__icontains=q) |
            models.Q(first_name__icontains=q) |
            models.Q(last_name__icontains=q) |
            models.Q(email__icontains=q)
        )
    if role:
        users = users.filter(role=role)
    
    context = {
        'users': users,
        'roles': roles,
    }

    return render(request, 'core/user_list.html', context)


@login_required
def project_list(request):
    projects = Project.objects.all()

    q = request.GET.get('q')
    start_date = request.GET.get('start_date')
    end_date = request.GET.get('end_date')

    if q:
        projects = projects.filter(
            models.Q(name__icontains=q) | models.Q(description__icontains=q)
        )
    if start_date:
        projects = projects.filter(start_date__gte=start_date)
    if end_date:
        projects = projects.filter(end_date__lte=end_date)
    
    context = {
        'projects': projects,
    }

    return render(request, 'core/project_list.html', context)


@login_required
def invoice_list(request):
    invoices = Invoice.objects.all()
    statuses = Invoice.STATUS_CHOICES

    q = request.GET.get('q')
    status = request.GET.get('status')
    start_date = request.GET.get('start_date')
    end_date = request.GET.get('end_date')

    if q:
        invoices = invoices.filter(
            models.Q(invoice_number__icontains=q) |
            models.Q(project__name__icontains=q)
        )
    if status:
        invoices = invoices.filter(status=status)
    if start_date:
        invoices = invoices.filter(due_date__gte=start_date)
    if end_date:
        invoices = invoices.filter(due_date__lte=end_date)
    
    context = {
        'invoices': invoices,
        'statuses': statuses,
    }

    return render(request, 'core/invoice_list.html', context)





