# -*- coding: utf-8 -*-
from logging import getLogger
from openprocurement.api.models import get_now
from openprocurement.api.utils import (
    apply_patch,
    save_tender,
    opresource,
    json_view,
    context_unpack,
)
from openprocurement.api.validation import (
    validate_contract_data,
    validate_patch_contract_data,
)


LOGGER = getLogger(__name__)


def check_tender_status(request):
    tender = request.validated['tender']
    if tender.contracts and tender.contracts[-1].status == 'active':
        tender.status = 'complete'


@opresource(name='Tender Limited Contracts',
            collection_path='/tenders/{tender_id}/contracts',
            procurementMethodType='reporting',
            path='/tenders/{tender_id}/contracts/{contract_id}',
            description="Tender contracts")
class TenderAwardContractResource(object):

    def __init__(self, request, context):
        self.request = request
        self.db = request.registry.db

    @json_view(content_type="application/json", permission='create_contract', validators=(validate_contract_data,))
    def collection_post(self):
        """Post a contract for award
        """
        tender = self.request.validated['tender']
        if tender.status not in ['active', 'complete']:
            self.request.errors.add('body', 'data', 'Can\'t add contract in current ({}) tender status'.format(tender.status))
            self.request.errors.status = 403
            return
        contract = self.request.validated['contract']
        tender.contracts.append(contract)
        if save_tender(self.request):
            LOGGER.info('Created tender contract {}'.format(contract.id),
                        extra=context_unpack(self.request, {'MESSAGE_ID': 'tender_contract_create'}, {'contract_id': contract.id}))
            self.request.response.status = 201
            self.request.response.headers['Location'] = self.request.route_url('Tender Contracts', tender_id=tender.id, contract_id=contract['id'])
            return {'data': contract.serialize()}

    @json_view(permission='view_tender')
    def collection_get(self):
        """List contracts for award
        """
        return {'data': [i.serialize() for i in self.request.context.contracts]}

    @json_view(permission='view_tender')
    def get(self):
        """Retrieving the contract for award
        """
        return {'data': self.request.validated['contract'].serialize()}

    @json_view(content_type="application/json", permission='edit_tender', validators=(validate_patch_contract_data,))
    def patch(self):
        """Update of contract
        """
        if self.request.validated['tender_status'] not in ['active', 'complete']:
            self.request.errors.add('body', 'data', 'Can\'t update contract in current ({}) tender status'.format(self.request.validated['tender_status']))
            self.request.errors.status = 403
            return
        data = self.request.validated['data']
        if self.request.context.status == 'cancelled':
            self.request.errors.add('body', 'data', 'Can\'t update contract in current ({}) status'.format(self.request.context.status))
            self.request.errors.status = 403
            return
        if self.request.context.status != 'active' and 'status' in data and data['status'] == 'active':
            tender = self.request.validated['tender']
            award = [a for a in tender.awards if a.id == self.request.context.awardID][0]
            stand_still_end = award.complaintPeriod.endDate
            if stand_still_end > get_now():
                self.request.errors.add('body', 'data', 'Can\'t sign contract before stand-still period end ({})'.format(stand_still_end.isoformat()))
                self.request.errors.status = 403
                return

        contract_status = self.request.context.status
        apply_patch(self.request, save=False, src=self.request.context.serialize())
        if contract_status != self.request.context.status and contract_status != 'pending' and self.request.context.status != 'active':
            self.request.errors.add('body', 'data', 'Can\'t update contract status')
            self.request.errors.status = 403
            return

        check_tender_status(self.request)
        if save_tender(self.request):
            LOGGER.info('Updated tender contract {}'.format(self.request.context.id),
                        extra=context_unpack(self.request, {'MESSAGE_ID': 'tender_contract_patch'}))
            return {'data': self.request.context.serialize()}

@@ -330,16 +330,129 @@
 data,%0A)%0A
+from openprocurement.api.views.contract import TenderAwardContractResource as TenderLimitedAwardContractResource%0A
 %0A%0ALOGGER
@@ -958,128 +958,44 @@
 rce(
-object):%0A%0A    def __init__(self, request, context):%0A        self.request = request%0A        self.db = request.registry.db
+TenderLimitedAwardContractResource):
 %0A%0A  
@@ -2075,399 +2075,8 @@
 )%7D%0A%0A
-    @json_view(permission='view_tender')%0A    def collection_get(self):%0A        %22%22%22List contracts for award%0A        %22%22%22%0A        return %7B'data': %5Bi.serialize() for i in self.request.context.contracts%5D%7D%0A%0A    @json_view(permission='view_tender')%0A    def get(self):%0A        %22%22%22Retrieving the contract for award%0A        %22%22%22%0A        return %7B'data': self.request.validated%5B'contract'%5D.serialize()%7D%0A%0A
     

add inheritance from op.api