import json
import os
from datetime import datetime

import boto3

# 環境変数
SFN_STATE_MACHINE_ARN=os.environ["SFN_STATE_MACHINE_ARN"]
CLUSTER_NAME = os.environ["CLUSTER_NAME"]
TASK_NAME = os.environ["TASK_NAME"]
CONTAINER_NAME = os.environ["CONTAINER_NAME"]
SUBNET_ID_AP_NORTHEAST_1A = os.environ["SUBNET_ID_AP_NORTHEAST_1A"]
SUBNET_ID_AP_NORTHEAST_1D = os.environ["SUBNET_ID_AP_NORTHEAST_1D"]
SECURITY_GROUP_ID_ECSALL = os.environ["SECURITY_GROUP_ID_ECSALL"]
SECURITY_GROUP_ID_ECSDATAIMPORT = os.environ["SECURITY_GROUP_ID_ECSDATAIMPORT"]
MODE = os.environ["MODE"]

# クラス変数
sfn_client = boto3.client('stepfunctions')


def lambda_handler(event, context):
    print(f'{datetime.now():%Y-%m-%d %H:%M:%S} Info I-1 駆動処理開始')

    # イベント情報を取得する
    s3_event = event["Records"][0]["s3"]
    event_bucket_name = s3_event["bucket"]["name"]
    event_object_key = s3_event["object"]["key"]
    event_file_name = os.path.basename(event_object_key)
    event_data_source_name = os.path.dirname(event_object_key).split('/')[0]
    print(f'{datetime.now():%Y-%m-%d %H:%M:%S} Info I-2 バケット名：{event_bucket_name}')
    print(f'{datetime.now():%Y-%m-%d %H:%M:%S} Info I-3 ファイル名：{event_file_name}')
    print(f'{datetime.now():%Y-%m-%d %H:%M:%S} Info I-4 データソース名：{event_data_source_name}')

    # StepFunctionsを起動する
    response = sfn_client.start_execution(
        stateMachineArn=SFN_STATE_MACHINE_ARN,
        input=json.dumps(
            {
                'InputParams': {
                    'ClusterName': CLUSTER_NAME,
                    'TaskName': TASK_NAME,
                    'SubNetIdApNorthEast1A': SUBNET_ID_AP_NORTHEAST_1A,
                    'SubNetIdApNorthEast1D': SUBNET_ID_AP_NORTHEAST_1D,
                    'SecurityGroupIdECSAll': SECURITY_GROUP_ID_ECSALL,
                    'SecurityGroupIdECSDataImport': SECURITY_GROUP_ID_ECSDATAIMPORT,
                    'ContainerName': CONTAINER_NAME,
                    'BucketName': event_bucket_name,
                    'TargetKey': event_object_key,
                    'DataSourceName': event_data_source_name,
                    'FileName': event_file_name,
                    'Mode': MODE
                }
            }
        )
    )
    print(f'{datetime.now():%Y-%m-%d %H:%M:%S} Info I-5 StepFunctions起動レスポンス：{str(response)}')

    print(f'{datetime.now():%Y-%m-%d %H:%M:%S} Info I-6 駆動処理終了')



# ローカルでのデバック実行用。
# 利用する際にコメントを外してください
# if __name__ == '__main__':
#     lambda_handler({"Records": [{
#         # バケット名やファイルキーはよしなに変えてください。
#         "s3": {
#             "bucket": {
#                 "name": "test-shimoda-bucket"
#             },
#             "object": {
#                 "key": "crm/target/CRM_Account_20221116111800.csv"
#             }
#         }
#         }]}, {})
