from rest_framework.decorators import api_view, authentication_classes

from app.api import api
from app.api.response_builder import ResponseBuilder
from app.merchant.merchant import Merchant
from app.merchant.merchant_center import MerchantCenter
from app.merchant.merchant_product import MerchantProduct
from app.util import util
from app.util.pagination import get_page_request_fields, get_page
from backend.authentication import JWTOrAPIKeyAuthentication


def validate_search_merchant_inputs(merchant_slug):
    if not merchant_slug:
        return api.INVALID_INPUT

    return api.SUCCESS


@api_view(['GET'])
# @authentication_classes((JWTOrAPIKeyAuthentication,))
def get_merchants(request):
    response_builder = ResponseBuilder()

    client_id, client_secret = api.get_client_key_secret(request)
    merchant_slug = request.GET.get("merchant_slug")

    merchants = []
    if merchant_slug:
        merchant = Merchant.get_merchant_by_slug(merchant_slug)
        if merchant:
            merchants.append(merchant)

    if not merchants:
        merchants = Merchant.get_all_merchants(client_id, client_secret)

    serialized_merchants = Merchant.get_all_serialized(merchants)
    result = {
        "merchants": serialized_merchants,
    }

    return response_builder.success().ok_200().message('Merchants fetched').result_object(result).get_response()


def validate_search_merchant_product_inputs(merchant_id):
    if not merchant_id:
        return api.INVALID_INPUT

    return api.SUCCESS


@api_view(['GET', 'POST'])
# @authentication_classes((JWTOrAPIKeyAuthentication,))
def get_merchant_products(request):
    data = request.GET
    api_type = None
    if not data:
        data = request.data
        api_type = 'solar'
    response_builder = ResponseBuilder()

    center_id = data.get('center_id')
    merchant_id = data.get('merchant_id')

    status = validate_search_merchant_product_inputs(merchant_id)
    if util.is_status_failed(status):
        return response_builder.get_ok200_fail_response(status)

    merchant_products = MerchantProduct.get_merchant_products(merchant_id, center_id)

    serialized_merchant_products = MerchantProduct.get_all_serialized(merchant_products)
    if api_type == 'solar':
        serialized_merchant_products.append({'id': 0, 'name': 'Others'})
    result = {
        "results": serialized_merchant_products,
    }
    if api_type == 'solar':
        return response_builder.solar_success().ok_200().message('Merchant products').result_object(
            result).get_solar_response()

    return response_builder.success().ok_200().message('Merchant products').result_object(result).get_response()


def validate_search_merchant_center_inputs(merchant_id):
    if not merchant_id:
        return api.INVALID_INPUT

    return api.SUCCESS


@api_view(['GET', 'POST'])
# @authentication_classes((JWTOrAPIKeyAuthentication,))
def get_merchant_centers(request):
    data = request.GET
    if not data:
        data = request.data
    response_builder = ResponseBuilder()

    center_slug = data.get('center_slug')
    merchant_slug = data.get('merchant_slug')
    merchant_id = data.get('merchant_id')
    pagination = data.get('pagination', True)

    search_text = data.get('search_text')
    merchant_centers = []

    if search_text and len(search_text) > 2:
        merchant_centers = MerchantCenter.get_merchant_center_by_search_text(search_text, merchant_id)

    if center_slug:
        merchant_id = Merchant.get_merchant_by_slug(merchant_slug)
        merchant_center = MerchantCenter.get_merchant_center_by_slug(center_slug, merchant_id.id)
        if merchant_center:
            merchant_centers.append(merchant_center)

    if not merchant_centers:
        status = validate_search_merchant_center_inputs(merchant_id)
        if util.is_status_failed(status):
            return response_builder.get_ok200_fail_response(status)
        
        merchant_centers = MerchantCenter.get_merchant_centers(merchant_id)

    if pagination:
        page, offset, page_size = get_page_request_fields(request)
        paginated_prompts, page_info = get_page(merchant_centers, page, offset, page_size)

        serialized_merchant_centers = MerchantCenter.get_all_serialized(paginated_prompts)

        result = {
            "results": serialized_merchant_centers,
            **page_info
        }
        return response_builder.success().ok_200().message('Merchant Centers fetched').result_object(
            result).get_response()
    else:
        serialized_merchant_centers = MerchantCenter.get_all_serialized(merchant_centers)
        result = {
            "results": serialized_merchant_centers
        }

        return response_builder.solar_success().ok_200().message('Merchant Centers fetched').result_object(result).get_solar_response()
