"""俱乐部商品类型 / 考核标签筛选配置 — 后台 API。"""
from rest_framework.parsers import FormParser, JSONParser, MultiPartParser
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated

from backend.view import verify_kefu_permission

# 小程序配置 8080a 或 商品类型专区 7007a 均可上架/下架
CLUB_LEIXING_PERMS = ('8080a', '7007a')


def _has_club_leixing_perm(permissions):
    return any(p in permissions for p in CLUB_LEIXING_PERMS)


class ClubShangpinLeixingListView(APIView):
    """POST /jituan/houtai/club-leixing-list"""
    permission_classes = [IsAuthenticated]
    parser_classes = [JSONParser]

    def post(self, request):
        phone = (request.data.get('phone') or request.data.get('username') or '').strip()
        if not phone:
            return Response({'code': 401, 'msg': '缺少客服账号'})
        kefu, permissions = verify_kefu_permission(request, phone)
        if kefu is None:
            return permissions
        if not _has_club_leixing_perm(permissions):
            return Response({'code': 403, 'msg': '无权限管理小程序商品类型'})
        from jituan.services.club_shangpin_leixing import build_club_leixing_admin_list
        payload = build_club_leixing_admin_list(request)
        return Response({'code': 0, 'data': payload})


class ClubShangpinLeixingCatalogView(APIView):
    """POST /jituan/houtai/club-leixing-catalog"""
    permission_classes = [IsAuthenticated]
    parser_classes = [JSONParser]

    def post(self, request):
        phone = (request.data.get('phone') or request.data.get('username') or '').strip()
        if not phone:
            return Response({'code': 401, 'msg': '缺少客服账号'})
        kefu, permissions = verify_kefu_permission(request, phone)
        if kefu is None:
            return permissions
        if not _has_club_leixing_perm(permissions):
            return Response({'code': 403, 'msg': '无权限'})
        from jituan.services.club_shangpin_leixing import build_club_leixing_catalog
        return Response({'code': 0, 'data': build_club_leixing_catalog(request)})


class ClubShangpinLeixingEnableView(APIView):
    """POST /jituan/houtai/club-leixing-enable"""
    permission_classes = [IsAuthenticated]
    parser_classes = [JSONParser]

    def post(self, request):
        phone = (request.data.get('phone') or request.data.get('username') or '').strip()
        leixing_id = request.data.get('leixing_id')
        if not phone or not leixing_id:
            return Response({'code': 400, 'msg': '参数不完整'})
        kefu, permissions = verify_kefu_permission(request, phone)
        if kefu is None:
            return permissions
        if not _has_club_leixing_perm(permissions):
            return Response({'code': 403, 'msg': '无权限'})
        from jituan.services.club_shangpin_leixing import enable_leixing_for_club
        data, err = enable_leixing_for_club(request, int(leixing_id), request.data.get('paixu'))
        if err:
            return Response({'code': 400, 'msg': err})
        return Response({'code': 0, 'msg': '上架成功', 'data': data})


class ClubShangpinLeixingDisableView(APIView):
    """POST /jituan/houtai/club-leixing-disable"""
    permission_classes = [IsAuthenticated]
    parser_classes = [JSONParser]

    def post(self, request):
        phone = (request.data.get('phone') or request.data.get('username') or '').strip()
        leixing_id = request.data.get('leixing_id')
        if not phone or not leixing_id:
            return Response({'code': 400, 'msg': '参数不完整'})
        kefu, permissions = verify_kefu_permission(request, phone)
        if kefu is None:
            return permissions
        if not _has_club_leixing_perm(permissions):
            return Response({'code': 403, 'msg': '无权限'})
        from jituan.services.club_shangpin_leixing import disable_leixing_for_club
        data, err = disable_leixing_for_club(request, int(leixing_id))
        if err:
            return Response({'code': 400, 'msg': err})
        return Response({'code': 0, 'msg': '已下架', 'data': data})


class ClubShangpinLeixingIconView(APIView):
    """POST /jituan/houtai/club-leixing-icon"""
    permission_classes = [IsAuthenticated]
    parser_classes = [MultiPartParser, FormParser, JSONParser]

    def post(self, request):
        phone = (request.data.get('phone') or request.data.get('username') or '').strip()
        leixing_id = request.data.get('leixing_id')
        file_obj = request.FILES.get('file')
        if not phone or not leixing_id or not file_obj:
            return Response({'code': 400, 'msg': '参数不完整'})
        kefu, permissions = verify_kefu_permission(request, phone)
        if kefu is None:
            return permissions
        if '8080a' not in permissions:
            return Response({'code': 403, 'msg': '无权限'})
        from jituan.services.club_shangpin_leixing import upload_leixing_icon
        data, err = upload_leixing_icon(request, int(leixing_id), file_obj)
        if err:
            return Response({'code': 400, 'msg': err})
        return Response({'code': 0, 'msg': '上传成功', 'data': data})


class ClubShangpinLeixingShowPriceView(APIView):
    """POST /jituan/houtai/club-leixing-show-price  开关该类型强制展示订单价格"""
    permission_classes = [IsAuthenticated]
    parser_classes = [JSONParser]

    def post(self, request):
        phone = (request.data.get('phone') or request.data.get('username') or '').strip()
        leixing_id = request.data.get('leixing_id')
        if not phone or leixing_id in (None, ''):
            return Response({'code': 400, 'msg': '参数不完整'})
        kefu, permissions = verify_kefu_permission(request, phone)
        if kefu is None:
            return permissions
        if not _has_club_leixing_perm(permissions):
            return Response({'code': 403, 'msg': '无权限'})
        raw = request.data.get('show_price', request.data.get('enabled'))
        if isinstance(raw, str):
            show_price = raw.strip().lower() in ('1', 'true', 'yes', 'on')
        else:
            show_price = bool(raw)
        from jituan.services.club_shangpin_leixing import set_leixing_show_price
        data, err = set_leixing_show_price(request, int(leixing_id), show_price)
        if err:
            return Response({'code': 400, 'msg': err})
        return Response({
            'code': 0,
            'msg': '已开启价格展示' if data.get('show_price') else '已关闭价格展示',
            'data': data,
        })


class ClubKaoheChenghaoListView(APIView):
    """POST /jituan/houtai/club-kaohe-list"""
    permission_classes = [IsAuthenticated]
    parser_classes = [JSONParser]

    def post(self, request):
        phone = (request.data.get('phone') or request.data.get('username') or '').strip()
        if not phone:
            return Response({'code': 401, 'msg': '缺少客服账号'})
        kefu, permissions = verify_kefu_permission(request, phone)
        if kefu is None:
            return permissions
        if '8080a' not in permissions:
            return Response({'code': 403, 'msg': '无权限'})
        bankuai_id = request.data.get('bankuai_id')
        from jituan.services.club_kaohe_chenghao import build_club_kaohe_admin_list
        payload = build_club_kaohe_admin_list(request, bankuai_id=bankuai_id)
        return Response({'code': 0, 'data': payload})


class ClubKaoheChenghaoCatalogView(APIView):
    """POST /jituan/houtai/club-kaohe-catalog"""
    permission_classes = [IsAuthenticated]
    parser_classes = [JSONParser]

    def post(self, request):
        phone = (request.data.get('phone') or request.data.get('username') or '').strip()
        if not phone:
            return Response({'code': 401, 'msg': '缺少客服账号'})
        kefu, permissions = verify_kefu_permission(request, phone)
        if kefu is None:
            return permissions
        if '8080a' not in permissions:
            return Response({'code': 403, 'msg': '无权限'})
        bankuai_id = request.data.get('bankuai_id')
        from jituan.services.club_kaohe_chenghao import build_club_kaohe_catalog
        return Response({'code': 0, 'data': build_club_kaohe_catalog(request, bankuai_id=bankuai_id)})


class ClubKaoheChenghaoEnableView(APIView):
    """POST /jituan/houtai/club-kaohe-enable"""
    permission_classes = [IsAuthenticated]
    parser_classes = [JSONParser]

    def post(self, request):
        phone = (request.data.get('phone') or request.data.get('username') or '').strip()
        chenghao_id = request.data.get('chenghao_id')
        if not phone or not chenghao_id:
            return Response({'code': 400, 'msg': '参数不完整'})
        kefu, permissions = verify_kefu_permission(request, phone)
        if kefu is None:
            return permissions
        if '8080a' not in permissions:
            return Response({'code': 403, 'msg': '无权限'})
        from jituan.services.club_kaohe_chenghao import enable_chenghao_for_club
        data, err = enable_chenghao_for_club(request, int(chenghao_id))
        if err:
            return Response({'code': 400, 'msg': err})
        return Response({'code': 0, 'msg': '上架成功', 'data': data})


class ClubKaoheChenghaoDisableView(APIView):
    """POST /jituan/houtai/club-kaohe-disable"""
    permission_classes = [IsAuthenticated]
    parser_classes = [JSONParser]

    def post(self, request):
        phone = (request.data.get('phone') or request.data.get('username') or '').strip()
        chenghao_id = request.data.get('chenghao_id')
        if not phone or not chenghao_id:
            return Response({'code': 400, 'msg': '参数不完整'})
        kefu, permissions = verify_kefu_permission(request, phone)
        if kefu is None:
            return permissions
        if '8080a' not in permissions:
            return Response({'code': 403, 'msg': '无权限'})
        from jituan.services.club_kaohe_chenghao import disable_chenghao_for_club
        data, err = disable_chenghao_for_club(request, int(chenghao_id))
        if err:
            return Response({'code': 400, 'msg': err})
        return Response({'code': 0, 'msg': '已下架', 'data': data})
