from django.db.models import Q
from django.shortcuts import get_object_or_404, render

from .models import Category, Claim


def _is_htmx_partial(request):
    return request.headers.get('X-Requested-With') == 'XMLHttpRequest'


def home(request):
    query = request.GET.get('q', '').strip()
    categories = Category.objects.all()

    if query:
        categories = categories.filter(
            Q(name__icontains=query) | Q(description__icontains=query)
        )

    context = {
        'categories': categories,
        'query': query,
        'total_claims': Claim.objects.count(),
    }
    if _is_htmx_partial(request):
        return render(request, 'claims/partials/category_list.html', context)
    return render(request, 'claims/home.html', context)


def category_detail(request, slug):
    category = get_object_or_404(Category, slug=slug)
    query = request.GET.get('q', '').strip()
    status = request.GET.get('status', '').strip()

    claims = category.claims.select_related('category').prefetch_related('tags')

    if query:
        claims = claims.filter(
            Q(title__icontains=query)
            | Q(short_answer__icontains=query)
            | Q(tags__name__icontains=query)
        ).distinct()

    if status and status in dict(Claim.Status.choices):
        claims = claims.filter(status=status)

    context = {
        'category': category,
        'claims': claims,
        'query': query,
        'status': status,
        'status_choices': Claim.Status.choices,
    }
    if _is_htmx_partial(request):
        return render(request, 'claims/partials/claim_list.html', context)
    return render(request, 'claims/category_detail.html', context)


def claim_detail(request, slug):
    claim = get_object_or_404(
        Claim.objects.select_related('category').prefetch_related(
            'tags', 'sources', 'counter_arguments', 'evidence_files'
        ),
        slug=slug,
    )
    related_claims = (
        Claim.objects.filter(category=claim.category)
        .exclude(pk=claim.pk)
        .select_related('category')[:3]
    )
    context = {
        'claim': claim,
        'related_claims': related_claims,
    }
    return render(request, 'claims/claim_detail.html', context)


def search(request):
    query = request.GET.get('q', '').strip()
    category_slug = request.GET.get('kategorie', '').strip()
    status = request.GET.get('status', '').strip()

    claims = Claim.objects.select_related('category').prefetch_related('tags')

    if query:
        claims = claims.filter(
            Q(title__icontains=query)
            | Q(short_answer__icontains=query)
            | Q(detailed_explanation__icontains=query)
            | Q(tags__name__icontains=query)
        ).distinct()

    if category_slug:
        claims = claims.filter(category__slug=category_slug)

    if status and status in dict(Claim.Status.choices):
        claims = claims.filter(status=status)

    categories = Category.objects.all()

    context = {
        'claims': claims if query or category_slug or status else None,
        'query': query,
        'category_slug': category_slug,
        'status': status,
        'categories': categories,
        'status_choices': Claim.Status.choices,
        'result_count': claims.count() if query or category_slug or status else 0,
    }
    if _is_htmx_partial(request):
        return render(request, 'claims/partials/search_results.html', context)
    return render(request, 'claims/search.html', context)
