from uuid import UUID

from django.http import HttpResponse
from ninja import Router
from epapi.v1.interfaces.iuclid.projections import get_pathway_for_iuclid_export

from .serializers.i6z import I6ZSerializer
from .serializers.pathway_mapper import PathwayMapper

router = Router(tags=["iuclid"])


@router.get("/pathway/{uuid:pathway_uuid}/export/iuclid")
def export_pathway_iuclid(request, pathway_uuid: UUID):
    export = get_pathway_for_iuclid_export(request.user, pathway_uuid)
    bundle = PathwayMapper().map(export)
    i6z_bytes = I6ZSerializer().serialize(bundle)
    return HttpResponse(
        i6z_bytes,
        content_type="application/zip",
        headers={"Content-Disposition": f'attachment; filename="pathway-{pathway_uuid}.i6z"'},
    )
