require_relative "../../shared/lib/jam_ruby/lib/lambda_function"

require 'cgi'
require 'fileutils'

module JamRuby
end

# Establish database connection
def establish_connection
    # Load database configuration
    db_config_path = File.join(Dir.pwd, 'config', 'database.yml')
    puts "db_config_path #{db_config_path}"
    db_config = YAML.load(ERB.new(File.read(db_config_path)).result)
    puts "db_config #{db_config}"
    environment = ENV['RAILS_ENV'] || 'production'


    # Establish connection
    ActiveRecord::Base.establish_connection(db_config[environment])
end

def lambda_handler(event:, context:)



    puts "event #{event}"
    invocation_id = event["invocationId"]
    invocation_schema_version = event["invocationSchemaVersion"]

    if invocation_schema_version != "2.0"
        puts "InformationSchemaVersion must be 2.0; found #{invocation_schema_version}"
        exit 1
    end

    results = []
    result_code = nil
    result_string = nil

    user_arguments = event["job"]["userArguments"]
    mode = user_arguments["mode"] if user_arguments
    if mode.nil?
        puts "Mode not specified as argument: #{user_arguments}"
        exit 1
    end

    task = event["tasks"][0]
    task_id = task["taskId"]
    obj_key = CGI.unescape(task["s3Key"])
    obj_version_id = task["s3VersionId"]
    bucket_name = task["s3Bucket"]

    if run(mode, bucket_name, obj_key, task_id) == true
        result_code = "Succeeded"
        result_string = "Imported #{obj_key}"
    else
        result_code = "Failed"
        result_string = "Failed to import #{obj_key}"
    end

    results.append(
      {
        "taskId": task_id,
        "resultCode": result_code,
        "resultString": result_string,
      }
    )

    {
      "invocationSchemaVersion": invocation_schema_version,
      "treatMissingKeysAs": "PermanentFailure",
      "invocationId": invocation_id,
      "results": results,
    }
end

def run(mode, bucket_name, obj_key, task_id)

    puts "mode=#{mode}"
    ENV['LD_LIBRARY_PATH'] = "/opt/lib:" + ENV['LD_LIBRARY_PATH'].to_s

    # Debugging: Print the library path to verify
    puts "LD_LIBRARY_PATH: #{ENV['LD_LIBRARY_PATH']}"

    zip_key = obj_key
    manifest_name = File.basename(zip_key)

    puts "Processing obj_key #{obj_key} manifest #{manifest_name}"

    tency_zips_bucket = ENV["TENCY_ZIPS_BUCKET"] #jamka
    tency_uploads_bucket = ENV["TENCY_JAMTRACKS_BUCKET"] # jamkazam-tency-202410

    tency_aws_region = ENV["AWS_REGION"] || "us-east-1"
    if tency_aws_region.nil?
        puts "tency_aws_region #{tency_aws_region}"
    end
    puts "Tency AWS Region '#{tency_aws_region}'"
    s3_host = ENV["S3_HOST"] || nil # "http://localhost:19090"

    tmp = ENV["MOUNT_PATH"] || "/tmp"

    if mode == "cleanup"
        FileUtils.rm_rf(tmp)
        return true
    end

    working_dir = File.join(tmp, mode, manifest_name)
    puts "Working_dir #{working_dir}"
    if File.exist?(working_dir)
        FileUtils.remove_dir(working_dir, true)
    end
    FileUtils.mkdir_p(working_dir)

    begin
        local_manifest_path = File.join(working_dir, "manifest.txt")

        puts "local manifest path #{local_manifest_path}"
        # Initialize the S3 client for the manifest
        s3_client = Aws::S3::Client.new(
          region: tency_aws_region,
          force_path_style: false,
         endpoint: 'https://s3.us-east-1.amazonaws.com'
        )

        puts s3_client.config.endpoint

        importer = JamRuby::JamTrackLambdaImporter.new

        #manifest = "/Users/seth/workspace/tency/scripts/manifests/ace-of-base_the-sign_10111/manifest.txt"
        #tmp =  "/Users/seth/workspace/jam-cloud/lambda/jamtrack-importer/tmp"

        if mode == "unzip"
            #Aws.config[:region] = tency_aws_region
            puts "HEYYYY zips bucket: #{bucket_name}, exploded-files bucket: #{tency_uploads_bucket}"
            tency_zips_manager = JamRuby::S3Manager.new(bucket_name, s3_host)
            tence_uploads_manager = JamRuby::S3Manager.new(tency_uploads_bucket, s3_host)

            puts importer.tency_unzipper(tency_zips_manager, tence_uploads_manager, working_dir, manifest_name, local_manifest_path)
        elsif mode == "create-jamtrack"

            tency_zips_manager = JamRuby::S3Manager.new(tency_zips_bucket, s3_host)
            tence_uploads_manager = JamRuby::S3Manager.new(tency_uploads_bucket, s3_host)


            begin
                # Download the manifest from S3
                puts "Downloading manifest from #{bucket_name} the #{zip_key}"
                File.open(local_manifest_path, 'wb') do |file|
                    s3_client.get_object(bucket: bucket_name, key: zip_key) do |chunk|
                        file.write(chunk)
                    end
                end

                puts "File downloaded successfully to: #{local_manifest_path}"
            rescue Aws::S3::Errors::ServiceError => e
                puts "Failed to download manifest: #{e.message}"
                result_code = "PermanentFailure"
                result_string = "Failed to download manifest: #{e.message}"
            end

            if ActiveRecord::Base.connected?
                puts "Already connected to db"
            else
                establish_connection
            end

            result = ActiveRecord::Base.connection.execute('SELECT NOW()')
            puts "Database query result: #{result.first}"

            JamRuby::JamTrackLambdaImporter.storage_format = "Tency"
            JamRuby::JamTrackLambdaImporter::import(tence_uploads_manager, working_dir, manifest_name, local_manifest_path, obj_key)
        else
            puts "Unknown mode #{mode}"
            exit 1
        end
    rescue => e
      puts "Error: #{e.message}"
      puts e.backtrace.join("\n")
      raise 
    ensure
        puts "ensure block of run"
        if mode == "unzip" # because this happens generally in EFS/AWS
            FileUtils.rm_rf(working_dir)
        end
    end

    puts "success"
    true
end

# take the 1st 3 args from the cli
if __FILE__ == $0
  if ARGV.length < 3
    puts "Usage: #{$0} <mode> <bucket_name> <obj_key>"
    exit 1
  end

  mode = ARGV[0]
  bucket_name = ARGV[1]
  obj_key = ARGV[2]

  run(mode,bucket_name, obj_key, "1")
end
