require 'repositories/event_types'

module VCAP::CloudController
  module Repositories
    class DropletEventRepository
      def self.record_create_by_staging(droplet, user_audit_info, v3_app_name, space_guid, org_guid)
        VCAP::AppLogEmitter.emit(droplet.app_guid, "Creating droplet for app with guid #{droplet.app_guid}")

        metadata = {
          droplet_guid: droplet.guid,
          package_guid: droplet.package_guid
        }

        Event.create(
          type: EventTypes::APP_DROPLET_CREATE,
          actor: user_audit_info.user_guid,
          actor_type: 'user',
          actor_name: user_audit_info.user_email,
          actor_username: user_audit_info.user_name,
          actee: droplet.app_guid,
          actee_type: 'app',
          actee_name: v3_app_name,
          timestamp: Sequel::CURRENT_TIMESTAMP,
          metadata: metadata,
          space_guid: space_guid,
          organization_guid: org_guid
        )
      end

      def self.record_create(droplet, user_audit_info, v3_app_name, space_guid, org_guid)
        VCAP::AppLogEmitter.emit(droplet.app_guid, "Creating droplet for app with guid #{droplet.app_guid}")

        metadata = {
          droplet_guid: droplet.guid
        }

        Event.create(
          type: EventTypes::APP_DROPLET_CREATE,
          actor: user_audit_info.user_guid,
          actor_type: 'user',
          actor_name: user_audit_info.user_email,
          actor_username: user_audit_info.user_name,
          actee: droplet.app_guid,
          actee_type: 'app',
          actee_name: v3_app_name,
          timestamp: Sequel::CURRENT_TIMESTAMP,
          metadata: metadata,
          space_guid: space_guid,
          organization_guid: org_guid
        )
      end

      def self.record_create_by_copying(new_droplet_guid, source_droplet_guid, user_audit_info, v3_app_guid, v3_app_name, space_guid, org_guid)
        VCAP::AppLogEmitter.emit(v3_app_guid, "Creating droplet for app with guid #{v3_app_guid}")

        metadata = {
          droplet_guid: new_droplet_guid,
          request: {
            source_droplet_guid:
          }
        }

        Event.create(
          type: EventTypes::APP_DROPLET_CREATE,
          actor: user_audit_info.user_guid,
          actor_type: 'user',
          actor_name: user_audit_info.user_email,
          actor_username: user_audit_info.user_name,
          actee: v3_app_guid,
          actee_type: 'app',
          actee_name: v3_app_name,
          timestamp: Sequel::CURRENT_TIMESTAMP,
          metadata: metadata,
          space_guid: space_guid,
          organization_guid: org_guid
        )
      end

      def self.record_delete(droplet, user_audit_info, v3_app_name, space_guid, org_guid)
        VCAP::AppLogEmitter.emit(droplet.app_guid, "Deleting droplet for app with guid #{droplet.app_guid}")

        metadata = { droplet_guid: droplet.guid }

        Event.create(
          type: EventTypes::APP_DROPLET_DELETE,
          actor: user_audit_info.user_guid,
          actor_type: 'user',
          actor_name: user_audit_info.user_email,
          actor_username: user_audit_info.user_name,
          actee: droplet.app_guid,
          actee_type: 'app',
          actee_name: v3_app_name,
          timestamp: Sequel::CURRENT_TIMESTAMP,
          metadata: metadata,
          space_guid: space_guid,
          organization_guid: org_guid
        )
      end

      # Emit this event once we have droplet download capability
      def self.record_download(droplet, user_audit_info, v3_app_name, space_guid, org_guid)
        VCAP::AppLogEmitter.emit(droplet.app_guid, "Downloading droplet for app with guid #{droplet.app_guid}")

        metadata = { droplet_guid: droplet.guid }

        Event.create(
          type: EventTypes::APP_DROPLET_DOWNLOAD,
          actor: user_audit_info.user_guid,
          actor_type: 'user',
          actor_name: user_audit_info.user_email,
          actor_username: user_audit_info.user_name,
          actee: droplet.app_guid,
          actee_type: 'app',
          actee_name: v3_app_name,
          timestamp: Sequel::CURRENT_TIMESTAMP,
          metadata: metadata,
          space_guid: space_guid,
          organization_guid: org_guid
        )
      end

      def self.record_upload(droplet, user_audit_info, v3_app_name, space_guid, org_guid)
        VCAP::AppLogEmitter.emit(droplet.app_guid, "Uploading droplet for app with guid #{droplet.app_guid}")

        metadata = { droplet_guid: droplet.guid }

        Event.create(
          type: EventTypes::APP_DROPLET_UPLOAD,
          actor: user_audit_info.user_guid,
          actor_type: 'user',
          actor_name: user_audit_info.user_email,
          actor_username: user_audit_info.user_name,
          actee: droplet.app_guid,
          actee_type: 'app',
          actee_name: v3_app_name,
          timestamp: Sequel::CURRENT_TIMESTAMP,
          metadata: metadata,
          space_guid: space_guid,
          organization_guid: org_guid
        )
      end
    end
  end
end
