# Path: openprocurement/tender/openua/utils.py
# PKG = get_distribution(__package__)
# LOGGER = getLogger(PKG.project_name)
# def calculate_normalized_date(dt, tender, ceil=False):
# def check_bids(request):
# def check_complaint_status(request, complaint):
# def check_status(request):
# def add_next_award(request, reverse=False, awarding_criteria_key='amount'):
# 
# Path: openprocurement/tender/openua/validation.py
# def validate_submit_claim_time(request):
#     tender = request.context
#     claim_submit_time = request.content_configurator.tender_claim_submit_time
#     if get_now() > calculate_business_date(tender.tenderPeriod.endDate, -claim_submit_time, tender):
#         raise_operation_error(request, 'Can submit claim not later than {0.days} days before tenderPeriod end'.format(claim_submit_time))
# 
# Path: openprocurement/tender/openua/constants.py
# CLAIM_SUBMIT_TIME = timedelta(days=10)
# 
# COMPLAINT_SUBMIT_TIME = timedelta(days=4)


from openprocurement.tender.belowthreshold.views.complaint import TenderComplaintResource
from openprocurement.api.utils import (
    context_unpack,
    json_view,
    set_ownership,
    get_now,
    raise_operation_error
)
from openprocurement.tender.openua.utils import (
    check_tender_status,
)
from openprocurement.tender.core.validation import (
    validate_complaint_data,
    validate_submit_complaint_time,
    validate_patch_complaint_data,
    validate_complaint_operation_not_in_active_tendering,
    validate_update_complaint_not_in_allowed_complaint_status
)
from openprocurement.tender.core.utils import (
    save_tender,
    apply_patch,
    optendersresource,
    calculate_business_date
)
from openprocurement.tender.openua.validation import validate_submit_claim_time
from openprocurement.tender.openua.constants import (
    CLAIM_SUBMIT_TIME, COMPLAINT_SUBMIT_TIME
)
# -*- coding: utf-8 -*-


@optendersresource(name='aboveThresholdUA:Tender Complaints',
                   collection_path='/tenders/{tender_id}/complaints',
                   path='/tenders/{tender_id}/complaints/{complaint_id}',
                   procurementMethodType='aboveThresholdUA',
                   description="Tender complaints")
class TenderUaComplaintResource(TenderComplaintResource):

    def complaints_len(self, tender):
        return sum([len(i.complaints) for i in tender.awards], len(tender.complaints))

    @json_view(content_type="application/json", validators=(validate_complaint_data, validate_complaint_operation_not_in_active_tendering), permission='create_complaint')
    def collection_post(self):
        """Post a complaint
        """
        tender = self.context
        complaint = self.request.validated['complaint']
        complaint.date = get_now()
        if complaint.status == 'claim':

# Path: openprocurement/tender/openua/utils.py
# PKG = get_distribution(__package__)
# LOGGER = getLogger(PKG.project_name)
# def calculate_normalized_date(dt, tender, ceil=False):
# def check_bids(request):
# def check_complaint_status(request, complaint):
# def check_status(request):
# def add_next_award(request, reverse=False, awarding_criteria_key='amount'):
# 
# Path: openprocurement/tender/openua/validation.py
# def validate_submit_claim_time(request):
#     tender = request.context
#     claim_submit_time = request.content_configurator.tender_claim_submit_time
#     if get_now() > calculate_business_date(tender.tenderPeriod.endDate, -claim_submit_time, tender):
#         raise_operation_error(request, 'Can submit claim not later than {0.days} days before tenderPeriod end'.format(claim_submit_time))
# 
# Path: openprocurement/tender/openua/constants.py
# CLAIM_SUBMIT_TIME = timedelta(days=10)
# 
# COMPLAINT_SUBMIT_TIME = timedelta(days=4)


# Path: openprocurement/tender/openua/views/complaint.py
from openprocurement.tender.belowthreshold.views.complaint import TenderComplaintResource
from openprocurement.api.utils import (
    context_unpack,
    json_view,
    set_ownership,
    get_now,
    raise_operation_error
)
from openprocurement.tender.openua.utils import (
    check_tender_status,
)
from openprocurement.tender.core.validation import (
    validate_complaint_data,
    validate_submit_complaint_time,
    validate_patch_complaint_data,
    validate_complaint_operation_not_in_active_tendering,
    validate_update_complaint_not_in_allowed_complaint_status
)
from openprocurement.tender.core.utils import (
    save_tender,
    apply_patch,
    optendersresource,
    calculate_business_date
)
from openprocurement.tender.openua.validation import validate_submit_claim_time
from openprocurement.tender.openua.constants import (
    CLAIM_SUBMIT_TIME, COMPLAINT_SUBMIT_TIME
)

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


@optendersresource(name='aboveThresholdUA:Tender Complaints',
                   collection_path='/tenders/{tender_id}/complaints',
                   path='/tenders/{tender_id}/complaints/{complaint_id}',
                   procurementMethodType='aboveThresholdUA',
                   description="Tender complaints")
class TenderUaComplaintResource(TenderComplaintResource):

    def complaints_len(self, tender):
        return sum([len(i.complaints) for i in tender.awards], len(tender.complaints))

    @json_view(content_type="application/json", validators=(validate_complaint_data, validate_complaint_operation_not_in_active_tendering), permission='create_complaint')
    def collection_post(self):
        """Post a complaint
        """
        tender = self.context
        complaint = self.request.validated['complaint']
        complaint.date = get_now()
        if complaint.status == 'claim':

            validate_submit_claim_time(self.request)