"""add image ingestion templates

Revision ID: f0f6d7be45e3
Revises: dcbfdfafe16c
Create Date: 2021-07-28 14:24:40.213660

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "f0f6d7be45e3"
down_revision = "dcbfdfafe16c"
branch_labels = None
depends_on = None


content = """{
  "project_code": "{{projectCode}}",
  "band_code": "{{bands}}",
  "configurations": "{{configurations}}",
  "starttime": null,
  "endtime": null,
  "exposure_time": null,
  "rms_noise": null,
  "image_tags": "",
  "product_tags": "",
  "collection_name": "",
  "calibration_level": 2
}
"""

metadata_content = """{
  "fileSetIds": "{{sdmId}}",
  "workflowName": "std_cms_imaging",
  "systemId": "{{request_id}}",
  "creationTime": "{{created_at}}",
  "productLocator": "{{product_locator}}",
  "calProductLocator": "{{cal_locator}}",
  "projectMetadata": {
      "projectCode": "{{projectCode}}",
      "title": "{{title}}",
      "telescope": "{{telescope}}",
      "startTime": "{{startTime}}",
      "observer": "{{observer}}"
    },
  "destinationDirectory": "{{root_directory}}/{{relative_path}}",
  "calibrationSourceDirectory":"{{cms_path}}",
  "cmsName":"{{sdmId}}.ms"
}

"""

condor_content = """executable = ingest_image.sh
arguments = metadata.json

output = ingest.out
error = ingest.err
log = condor.log

SBIN_PATH = /lustre/aoc/cluster/pipeline/$ENV(CAPO_PROFILE)/workspaces/sbin
should_transfer_files = yes
transfer_input_files = $(SBIN_PATH)/pycapo, $(SBIN_PATH)/conveyor, $(SBIN_PATH)/ingest_envoy, $(SBIN_PATH)/ingest, $(SBIN_PATH)/image-product-collector.sh, ./metadata.json, ./aux_image_metadata.json

getenv = True
environment = "CAPO_PATH=/home/casa/capo"

queue


"""


old_metadata = """{
  "fileSetIds": "{{sdmId}}",
  "workflowName": "std_cms_imaging",
  "systemId": "{{request_id}}",
  "creationTime": "{{created_at}}",
  "productLocator": "{{product_locator}}",
  "projectMetadata": {
      "projectCode": "{{projectCode}}",
      "title": "{{title}}",
      "telescope": "{{telescope}}",
      "startTime": "{{startTime}}",
      "observer": "{{observer}}"
    },
  "destinationDirectory": "{{root_directory}}/{{relative_path}}",
  "calibrationSourceDirectory":"{{cms_path}}",
  "cmsName":"{{sdmId}}.ms"
}

"""

old_condor = """executable = ingest_image.sh
arguments = metadata.json

output = ingest.out
error = ingest.err
log = condor.log

SBIN_PATH = /lustre/aoc/cluster/pipeline/$ENV(CAPO_PROFILE)/workspaces/sbin
should_transfer_files = yes
transfer_input_files = $(SBIN_PATH)/pycapo, $(SBIN_PATH)/conveyor, $(SBIN_PATH)/ingest_envoy, $(SBIN_PATH)/ingest, $(SBIN_PATH)/image-product-collector.sh, ./metadata.json

getenv = True
environment = "CAPO_PATH=/home/casa/capo"

queue


"""


def upgrade():
    op.execute(
        f"""
        INSERT INTO workflow_templates (filename, content, workflow_name) 
        VALUES ('aux_image_metadata.json', E'{content}', 'ingest_image') 
        """
    )
    op.execute(
        f"""
        UPDATE workflow_templates
        SET content=E'{metadata_content}' WHERE filename='metadata.json' AND workflow_name = 'std_cms_imaging'
        """
    )
    op.execute(
        f"""
        UPDATE workflow_templates
        SET content=E'{condor_content}' WHERE filename='ingest_image.condor'
        """
    )


def downgrade():
    op.execute(
        """
        DELETE FROM workflow_templates WHERE filename='aux_image_metadata.json' AND workflow_name='ingest_image'
        """
    )
    op.execute(
        f"""
        UPDATE workflow_templates
        SET content=E'{old_metadata}' WHERE filename='metadata.json' AND workflow_name = 'std_cms_imaging'
        """
    )
    op.execute(
        f"""
        UPDATE workflow_templates
        SET content=E'{old_condor}' WHERE filename='ingest_image.condor'
        """
    )