# Path: openprocurement/tender/openua/constants.py
# STATUS4ROLE = {
#     'complaint_owner': ['draft', 'answered', 'claim', 'pending', 'accepted', 'satisfied'],
#     'aboveThresholdReviewers': ['pending', 'accepted', 'stopping'],
#     'tender_owner': ['claim', 'pending', 'accepted', 'satisfied'],
# }


from openprocurement.tender.belowthreshold.views.award_complaint_document import TenderAwardComplaintDocumentResource
from openprocurement.api.utils import (
    context_unpack,
    json_view,
    update_file_content_type,
    upload_file,
    raise_operation_error,
    error_handler
)
from openprocurement.tender.core.utils import (
    save_tender,
    optendersresource,
    apply_patch
)
from openprocurement.api.validation import (
    validate_file_update,
    validate_file_upload,
    validate_patch_document_data,
)
from openprocurement.tender.openua.constants import STATUS4ROLE
# -*- coding: utf-8 -*-


@optendersresource(name='aboveThresholdUA:Tender Award Complaint Documents',
                   collection_path='/tenders/{tender_id}/awards/{award_id}/complaints/{complaint_id}/documents',
                   path='/tenders/{tender_id}/awards/{award_id}/complaints/{complaint_id}/documents/{document_id}',
                   procurementMethodType='aboveThresholdUA',
                   description="Tender award complaint documents")
class TenderUaAwardComplaintDocumentResource(TenderAwardComplaintDocumentResource):

    def validate_complaint_document(self, operation):
        """ TODO move validators
        This class is inherited in limited and openeu (qualification complaint) package, but validate_complaint_document function has different validators.
        For now, we have no way to use different validators on methods according to procedure type.
        """
        if operation == 'update' and self.request.authenticated_role != self.context.author:
            self.request.errors.add('url', 'role', 'Can update document only author')
            self.request.errors.status = 403
            raise error_handler(self.request.errors)
        if self.request.validated['tender_status'] not in ['active.qualification', 'active.awarded']:
            raise_operation_error(self.request, 'Can\'t {} document in current ({}) tender status'.format(operation, self.request.validated['tender_status']))
        if any([i.status != 'active' for i in self.request.validated['tender'].lots if i.id == self.request.validated['award'].lotID]):
            raise_operation_error(self.request, 'Can {} document only in active lot status'.format(operation))

# Path: openprocurement/tender/openua/constants.py
# STATUS4ROLE = {
#     'complaint_owner': ['draft', 'answered', 'claim', 'pending', 'accepted', 'satisfied'],
#     'aboveThresholdReviewers': ['pending', 'accepted', 'stopping'],
#     'tender_owner': ['claim', 'pending', 'accepted', 'satisfied'],
# }


# Path: openprocurement/tender/openua/views/award_complaint_document.py
from openprocurement.tender.belowthreshold.views.award_complaint_document import TenderAwardComplaintDocumentResource
from openprocurement.api.utils import (
    context_unpack,
    json_view,
    update_file_content_type,
    upload_file,
    raise_operation_error,
    error_handler
)
from openprocurement.tender.core.utils import (
    save_tender,
    optendersresource,
    apply_patch
)
from openprocurement.api.validation import (
    validate_file_update,
    validate_file_upload,
    validate_patch_document_data,
)
from openprocurement.tender.openua.constants import STATUS4ROLE

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


@optendersresource(name='aboveThresholdUA:Tender Award Complaint Documents',
                   collection_path='/tenders/{tender_id}/awards/{award_id}/complaints/{complaint_id}/documents',
                   path='/tenders/{tender_id}/awards/{award_id}/complaints/{complaint_id}/documents/{document_id}',
                   procurementMethodType='aboveThresholdUA',
                   description="Tender award complaint documents")
class TenderUaAwardComplaintDocumentResource(TenderAwardComplaintDocumentResource):

    def validate_complaint_document(self, operation):
        """ TODO move validators
        This class is inherited in limited and openeu (qualification complaint) package, but validate_complaint_document function has different validators.
        For now, we have no way to use different validators on methods according to procedure type.
        """
        if operation == 'update' and self.request.authenticated_role != self.context.author:
            self.request.errors.add('url', 'role', 'Can update document only author')
            self.request.errors.status = 403
            raise error_handler(self.request.errors)
        if self.request.validated['tender_status'] not in ['active.qualification', 'active.awarded']:
            raise_operation_error(self.request, 'Can\'t {} document in current ({}) tender status'.format(operation, self.request.validated['tender_status']))
        if any([i.status != 'active' for i in self.request.validated['tender'].lots if i.id == self.request.validated['award'].lotID]):
            raise_operation_error(self.request, 'Can {} document only in active lot status'.format(operation))

        if self.request.validated['complaint'].status not in STATUS4ROLE.get(self.request.authenticated_role, []):