from app.models import Merchant, MerchantLocation, MerchantLocationBank, VendorApiCredentials, Vendor
from app.util.util import get_or_none


class MerchantAccessor:
    @classmethod
    def get_all_merchants(cls, client_id, client_secret):
        if not client_id:
            return Merchant.objects.filter(deleted_at=None, pre_status=0, status='active', is_guest_active=1,)
        vendor_id = VendorApiCredentials.objects.filter(key=client_id, secret=client_secret).first().vendor_id
        vendor_merchants = Vendor.objects.filter(id=vendor_id,).first().merchant_id
        if vendor_merchants:
            merchants = vendor_merchants.split(',')
        else:
            return ''
        return Merchant.objects.filter(deleted_at=None, pre_status=0, status='active', is_guest_active=1, id__in=merchants)

    @classmethod
    def get_merchant(cls, merchant_id):
        return get_or_none(Merchant, id=merchant_id)

    @classmethod
    def get_merchant_by_slug(cls, merchant_slug):
        return get_or_none(Merchant, slug=merchant_slug, deleted_at=None, pre_status=0, status='active', is_guest_active=1)

    @classmethod
    def get_merchant_location(cls, merchant_id):
        return MerchantLocation.objects.filter(merchant_id=merchant_id, deleted_at=None).order_by('-created_at').first()

    @classmethod
    def has_merchant_location_bank(cls, location_id):
        return MerchantLocationBank.objects.filter(location_id=location_id).exists()
