from django.shortcuts import render

# Create your views here.

from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
import json
from archive.utils import move_to_archive_by_id

@csrf_exempt
def archive_listing(request):
    """API do archiwizacji ogłoszenia"""
    if request.method == "POST":
        try:
            data = json.loads(request.body)
            listing_id = data.get("listing_id")

            if not listing_id:
                return JsonResponse({"error": "Brak listing_id"}, status=400)

            result = move_to_archive_by_id(listing_id)
            return JsonResponse(result)

        except json.JSONDecodeError:
            return JsonResponse({"error": "Błędny format JSON"}, status=400)

    return JsonResponse({"error": "Metoda niedozwolona"}, status=405)
