# -*- coding: utf-8 -*-

from __future__ import division

from django.conf import settings
from django.db import transaction
from django_logit import logit
from django_response import response
from TimeConvert import TimeConvert as tc

from mch.models import ConsumeInfoSubmitLogInfo, AdministratorInfo
from utils.error.errno_utils import AdministratorStatusCode, ProductBrandStatusCode, ProductCouponStatusCode, ProductMachineStatusCode


WECHAT = settings.WECHAT


@logit
def querysn(request):
    brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
    admin_id = request.POST.get('admin_id', '')
    model_id = request.POST.get('model_id', '')
    sn = request.POST.get('sn', '')

    if brand_id != settings.KODO_DEFAULT_BRAND_ID:
        return response(ProductBrandStatusCode.BRAND_NOT_MATCH)

    if not AdministratorInfo.objects.filter(admin_id=admin_id, admin_type=AdministratorInfo.MAINTENANCE, user_status=AdministratorInfo.ACTIVATED, status=True).exists():
        return response(AdministratorStatusCode.MAINTENANCE_NOT_FOUND)

    log = ConsumeInfoSubmitLogInfo.objects.filter(brand_id=brand_id, model_id=model_id, serialNo=sn, submit_during_activity=True).order_by('has_used', '-pk').first()

    if not log:
        log = ConsumeInfoSubmitLogInfo.objects.filter(brand_id=brand_id, model_id=model_id, serialNo=sn).order_by('-submit_during_activity', 'dupload').first()

    if not log:
        return response(ProductMachineStatusCode.SN_NOT_FOUND)

    return response(200, 'Query SN Success', u'查询序列号成功', data=log.data)


@logit
def queryusedsn(request):
    brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
    admin_id = request.POST.get('admin_id', '')

    if brand_id != settings.KODO_DEFAULT_BRAND_ID:
        return response(ProductBrandStatusCode.BRAND_NOT_MATCH)

    if not AdministratorInfo.objects.filter(admin_id=admin_id, admin_type=AdministratorInfo.MAINTENANCE, user_status=AdministratorInfo.ACTIVATED, status=True).exists():
        return response(AdministratorStatusCode.MAINTENANCE_NOT_FOUND)

    logs = ConsumeInfoSubmitLogInfo.objects.filter(brand_id=brand_id, admin_id=admin_id, has_used=True).order_by('-used_at')
    logs = [log.data for log in logs]

    return response(200, 'Query Used SN Success', u'查询核销序列号成功', data={
        'logs': logs,
    })


@logit
@transaction.atomic
def usecoupon(request):
    brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
    admin_id = request.POST.get('admin_id', '')
    model_id = request.POST.get('model_id', '')
    sn = request.POST.get('sn', '')

    if brand_id != settings.KODO_DEFAULT_BRAND_ID:
        return response(ProductBrandStatusCode.BRAND_NOT_MATCH)

    if not AdministratorInfo.objects.filter(
        admin_id=admin_id,
        admin_type=AdministratorInfo.MAINTENANCE,
        user_status=AdministratorInfo.ACTIVATED,
        status=True
    ).exists():
        return response(AdministratorStatusCode.MAINTENANCE_NOT_FOUND)

    log = ConsumeInfoSubmitLogInfo.objects.filter(
        brand_id=brand_id,
        model_id=model_id,
        serialNo=sn,
        submit_during_activity=True,
        has_used=False
    ).first()
    if not log:
        return response(ProductMachineStatusCode.SN_NOT_FOUND)

    logdata = log.data
    if logdata.get('final_coupon_info', {}).get('coupon_has_expired', True):
        return response(ProductCouponStatusCode.COUPON_HAS_EXPIRED)

    log.has_used = True
    log.used_at = tc.utc_datetime()
    log.save()

    return response(200, 'Use Coupon Success', u'核销优惠券成功')
