import os

import django
from celery import Celery
from kombu import Queue

from fwd import settings

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fwd.settings")
django.setup()

app: Celery = Celery(
    'postman',
    broker=settings.BROKER_URL,
    include=['fwd_api.celery_worker.process_result_tasks', 'fwd_api.celery_worker.internal_task'],
    broker_transport_options={'confirm_publish': False},
)

app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks()

@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
    from fwd_api.bg_tasks.clean_local_files import clean_media
    sender.add_periodic_task(
        10.0, clean_media.s(), expires=120.0
    )


app.conf.update({
    'task_queues':
    [
        Queue('invoice_sap_rs'),
        Queue('invoice_fi_rs'),
        Queue('invoice_manulife_rs'),
        Queue('invoice_sbt_rs'),
        Queue('do_pdf'),
        Queue('upload_file_to_s3'),
        Queue('upload_obj_to_s3'),
        Queue('remove_local_file'),


    ],
    'task_routes': {
        'process_sap_invoice_result': {'queue': 'invoice_sap_rs'},
        'process_sap_invoice': {'queue': "invoice_sap"},
        'process_fi_invoice_result': {'queue': 'invoice_fi_rs'},
        'process_fi_invoice': {'queue': "invoice_fi"},    
        'process_manulife_invoice_result': {'queue': 'invoice_manulife_rs'},
        'process_manulife_invoice': {'queue': "invoice_manulife"},
        'process_sbt_invoice_result': {'queue': 'invoice_sbt_rs'},
        'process_sbt_invoice': {'queue': "invoice_sbt"},
        'do_pdf': {'queue': "do_pdf"},   
        'upload_file_to_s3': {'queue': "upload_file_to_s3"},   
        'upload_obj_to_s3': {'queue': "upload_obj_to_s3"},   
        'upload_file_to_s3': {'queue': "upload_file_to_s3"},   
        'remove_local_file': {'queue': "remove_local_file"},   
        }
})

if __name__ == "__main__":
    argv = [
        'worker',
        '--loglevel=INFO',
        '--pool=solo'  # Window opts
    ]
    app.worker_main(argv)
