# Copyright © 2021 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""s2i based launch script to run the service."""
import os
import time
import uuid
from datetime import datetime, timezone

from flask import Flask, current_app
from namex.models import Request, State, db
from namex.services import queue
from queue_common.messages import create_cloud_event_msg
from sqlalchemy import text

import config
from utils.logging import setup_logging

APP_CONFIG = config.get_named_config(os.getenv('FLASK_ENV', 'production'))


def create_app():
    """Return a configured Flask App using the Factory method."""
    app = Flask(__name__)
    app.config.from_object(APP_CONFIG)

    queue.init_app(app)
    db.init_app(app)

    register_shellcontext(app)

    return app


def register_shellcontext(app):
    """Register shell context objects."""
    def shell_context():
        """Shell context objects."""
        return {'app': app}

    app.shell_context_processor(shell_context)


def publish_email_message(payload: dict):
    """Publish the email message onto the NATS emailer subject."""
    subject = APP_CONFIG.NATS_EMAILER_SUBJECT
    current_app.logger.debug('publish to queue, subject:%s, event:%s', subject, payload)
    queue.publish_json_to_subject_sync(payload, subject)


def furnish_request_message(
        request: Request,
        option: str
):  # pylint: disable=redefined-outer-name
    """Send notification info to the mail queue."""
    current_app.logger.debug('Start of the furnishing of request for %s nrNum=%s', option, request.nrNum)
    payload = create_cloud_event_msg(
        msg_id=str(uuid.uuid4()),
        msg_type='bc.registry.names.request',
        source=f'/requests/{request.nrNum}',
        time=datetime.utcfromtimestamp(time.time()).replace(tzinfo=timezone.utc).isoformat(),
        identifier=request.nrNum,
        json_data_body={
            'request': {
                'nrNum': request.nrNum,
                'option': option
            }
        }
    )
    current_app.logger.debug('About to publish email for %s nrNum=%s', option, request.nrNum)
    publish_email_message(payload)

    if option == 'before-expiry':
        request.notifiedBeforeExpiry = True
    elif option == 'expired':
        request.notifiedExpiry = True
        request.stateCd = State.EXPIRED
    request.save_to_db()


def notify_nr_before_expiry():
    """Send nr before expiry."""
    try:
        current_app.logger.debug('entering notify_nr_before_expiry')

        where_clause = text(
            "expiration_date - interval '14 day' <= CURRENT_DATE AND expiration_date > CURRENT_DATE")
        requests = db.session.query(Request).filter(
            Request.stateCd.in_((State.APPROVED, State.CONDITIONAL)),
            Request.notifiedBeforeExpiry == False,  # noqa E712; pylint: disable=singleton-comparison
            where_clause
        ).all()
        for request in requests:
            furnish_request_message(request, 'before-expiry')
    except Exception as err:  # noqa B902; pylint: disable=W0703;
        current_app.logger.error(err)


def notify_nr_expired():
    """Send nr expired."""
    try:
        current_app.logger.debug('entering notify_nr_expired')

        where_clause = text('expiration_date <= CURRENT_DATE')
        requests = db.session.query(Request).filter(
            Request.stateCd.in_((State.APPROVED, State.CONDITIONAL)),
            Request.notifiedBeforeExpiry == True,  # noqa E712; pylint: disable=singleton-comparison
            Request.notifiedExpiry == False,  # noqa E712; pylint: disable=singleton-comparison
            where_clause
        ).all()
        for request in requests:
            furnish_request_message(request, 'expired')
    except Exception as err:  # noqa B902; pylint: disable=W0703;
        current_app.logger.error(err)


if __name__ == '__main__':
    setup_logging(
        os.path.join(os.path.abspath(os.path.dirname(__file__)), 'logging.conf'))

    application = create_app()
    with application.app_context():
        notify_nr_before_expiry()
        notify_nr_expired()

# Copyright © 2021 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""s2i based launch script to run the service."""
import os
import time
import uuid
from datetime import datetime, timezone

from flask import Flask, current_app
from namex.models import Request, State, db
from namex.services import queue
from queue_common.messages import create_cloud_event_msg
from sqlalchemy import text

import config
from utils.logging import setup_logging

APP_CONFIG = config.get_named_config(os.getenv('FLASK_ENV', 'production'))


def create_app():
    """Return a configured Flask App using the Factory method."""
    app = Flask(__name__)
    app.config.from_object(APP_CONFIG)

    queue.init_app(app)
    db.init_app(app)

    register_shellcontext(app)

    return app


def register_shellcontext(app):
    """Register shell context objects."""
    def shell_context():
        """Shell context objects."""
        return {'app': app}

    app.shell_context_processor(shell_context)


def publish_email_message(payload: dict):
    """Publish the email message onto the NATS emailer subject."""
    subject = APP_CONFIG.NATS_EMAILER_SUBJECT
    current_app.logger.debug('publish to queue, subject:%s, event:%s', subject, payload)
    queue.publish_json_to_subject_sync(payload, subject)


def furnish_request_message(
        request: Request,
        option: str
):  # pylint: disable=redefined-outer-name
    """Send notification info to the mail queue."""
    current_app.logger.debug('Start of the furnishing of request for %s nrNum=%s', option, request.nrNum)
    payload = create_cloud_event_msg(
        msg_id=str(uuid.uuid4()),
        msg_type='bc.registry.names.request',
        source=f'/requests/{request.nrNum}',
        time=datetime.utcfromtimestamp(time.time()).replace(tzinfo=timezone.utc).isoformat(),
        identifier=request.nrNum,
        json_data_body={
            'request': {
                'nrNum': request.nrNum,
                'option': option
            }
        }
    )
    current_app.logger.debug('About to publish email for %s nrNum=%s', option, request.nrNum)
    publish_email_message(payload)

    if option == 'before-expiry':
        request.notifiedBeforeExpiry = True
    elif option == 'expired':
        request.notifiedExpiry = True
        request.stateCd = State.EXPIRED
    request.save_to_db()


def notify_nr_before_expiry():
    """Send nr before expiry."""
    try:
        current_app.logger.debug('entering notify_nr_before_expiry')

        where_clause = text(
            "expiration_date - interval '14 day' <= CURRENT_DATE AND expiration_date > CURRENT_DATE")
        requests = db.session.query(Request).filter(
            Request.stateCd.in_((State.APPROVED, State.CONDITIONAL)),
            Request.notifiedBeforeExpiry == False,  # noqa E712; pylint: disable=singleton-comparison
            where_clause
        ).all()
        for request in requests:
            furnish_request_message(request, 'before-expiry')
    except Exception as err:  # noqa B902; pylint: disable=W0703;
        current_app.logger.error(err)


def notify_nr_expired():
    """Send nr expired."""
    try:
        current_app.logger.debug('entering notify_nr_expired')

        where_clause = text('expiration_date <= CURRENT_DATE')
        requests = db.session.query(Request).filter(
            Request.stateCd.in_((State.APPROVED, State.CONDITIONAL)),
            Request.notifiedBeforeExpiry == True,  # noqa E712; pylint: disable=singleton-comparison
            Request.notifiedExpiry == False,  # noqa E712; pylint: disable=singleton-comparison
            where_clause
        ).all()
        for request in requests:
            furnish_request_message(request, 'expired')
    except Exception as err:  # noqa B902; pylint: disable=W0703;
        current_app.logger.error(err)


if __name__ == '__main__':
    setup_logging(
        os.path.join(os.path.abspath(os.path.dirname(__file__)), 'logging.conf'))

    application = create_app()
    with application.app_context():
        notify_nr_before_expiry()
        notify_nr_expired()

# Copyright © 2021 Province of British Columbia # # Licensed under the Apache License, Version 2.0 (the 'License'); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # #     http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an 'AS IS' BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. s2i based launch script to run the service. Return a configured Flask App using the Factory method. Register shell context objects. Shell context objects. Publish the email message onto the NATS emailer subject. # pylint: disable=redefined-outer-name Send notification info to the mail queue. Send nr before expiry. # noqa E712; pylint: disable=singleton-comparison # noqa B902; pylint: disable=W0703; Send nr expired. # noqa E712; pylint: disable=singleton-comparison # noqa E712; pylint: disable=singleton-comparison # noqa B902; pylint: disable=W0703;