from pkg_resources import require
import graphene

from ...unurshop.package import models, PackageStatus
from ..core.mutations import BaseBulkMutation, ModelBulkDeleteMutation
from ...order.models import FulfillmentLine

class GaduurBulkDelete(ModelBulkDeleteMutation):
    class Arguments:
        ids = graphene.List(
            graphene.ID, required=True, description="List of gaduur IDs to delete."
        )

    class Meta:
        description = "Deletes gaduur."
        model = models.GaduurPackage
        permissions = ("page.manage_pages",)

class PackageBulkDelete(ModelBulkDeleteMutation):
    class Arguments:
        ids = graphene.List(
            graphene.ID, required=True, description="list of package IDs to delete"
        )
    class Meta:
        description="Delete packages"
        model = models.Package
        permissions = ("page.manage_pages")



class PackageBulkUstatus(BaseBulkMutation):
    class Arguments:
        ids = graphene.List(
            graphene.ID, required=True, description="List of packages IDs to status change."
        )
        ustatus = graphene.String(required=True, description="package status")

    class Meta:
        description = "ustatus packages."
        model = models.Package
        permissions = ("page.manage_pages",)


    @classmethod
    def bulk_action(cls, queryset, ustatus):
        queryset.update(ustatus=ustatus)
        fline_ids = []
        for p in queryset.all():
            for line in p.lines.all():
                if line.fulfillmentline:
                    fline_ids.append(line.fulfillmentline.pk)
        
        qs = FulfillmentLine.objects.filter(pk__in=fline_ids)
        qs.update(ustatus=ustatus)

class PackageLineBulkDelete(ModelBulkDeleteMutation):
    class Arguments:
        ids = graphene.List(
            graphene.ID, required=True, description=" ustgah package lines"
        )
    class Meta:
        description="Delete pacagelines"
        model = models.PackageLine

    @classmethod
    def clean_instance(cls, info, instance):
        if instance.fulfillmentline:
            instance.fulfillmentline.ustatus = PackageStatus.ATUK
            instance.fulfillmentline.save()

            fulfillment = instance.fulfillmentline.fulfillment
            fulfillment.usatus = PackageStatus.ATUK
            fulfillment.save()

from pkg_resources import require
import graphene

from ...unurshop.package import models, PackageStatus
from ..core.mutations import BaseBulkMutation, ModelBulkDeleteMutation
from ...order.models import FulfillmentLine

class GaduurBulkDelete(ModelBulkDeleteMutation):
    class Arguments:
        ids = graphene.List(
            graphene.ID, required=True, description="List of gaduur IDs to delete."
        )

    class Meta:
        description = "Deletes gaduur."
        model = models.GaduurPackage
        permissions = ("page.manage_pages",)

class PackageBulkDelete(ModelBulkDeleteMutation):
    class Arguments:
        ids = graphene.List(
            graphene.ID, required=True, description="list of package IDs to delete"
        )
    class Meta:
        description="Delete packages"
        model = models.Package
        permissions = ("page.manage_pages")



class PackageBulkUstatus(BaseBulkMutation):
    class Arguments:
        ids = graphene.List(
            graphene.ID, required=True, description="List of packages IDs to status change."
        )
        ustatus = graphene.String(required=True, description="package status")

    class Meta:
        description = "ustatus packages."
        model = models.Package
        permissions = ("page.manage_pages",)


    @classmethod
    def bulk_action(cls, queryset, ustatus):
        queryset.update(ustatus=ustatus)
        fline_ids = []
        for p in queryset.all():
            for line in p.lines.all():
                if line.fulfillmentline:
                    fline_ids.append(line.fulfillmentline.pk)
        
        qs = FulfillmentLine.objects.filter(pk__in=fline_ids)
        qs.update(ustatus=ustatus)

class PackageLineBulkDelete(ModelBulkDeleteMutation):
    class Arguments:
        ids = graphene.List(
            graphene.ID, required=True, description=" ustgah package lines"
        )
    class Meta:
        description="Delete pacagelines"
        model = models.PackageLine

    @classmethod
    def clean_instance(cls, info, instance):
        if instance.fulfillmentline:
            instance.fulfillmentline.ustatus = PackageStatus.ATUK
            instance.fulfillmentline.save()

            fulfillment = instance.fulfillmentline.fulfillment
            fulfillment.usatus = PackageStatus.ATUK
            fulfillment.save()
