# frozen_string_literal: true

require_relative 'base'

module PluginConfig
  module Operations
    class Audit < Base
      def call
        checks = {}
        issues = []
        fixes = []

        checks[:marketplace] = check_marketplace
        checks[:installed] = check_installed
        checks[:settings] = check_settings

        checks.each_value do |check|
          next if check[:status] == 'ok'

          issues << check[:issue]
          fixes << check[:fix] if check[:fix]
        end

        {
          status: issues.empty? ? 'ok' : 'issues_found',
          checks: checks,
          issues: issues,
          fixes: fixes
        }
      end

      private

      def check_marketplace
        path = marketplace_file
        return missing_check('marketplace', path, register_fix) unless path && File.exist?(path)

        data = read_json(path)
        return parse_error_check('marketplace', path) unless data

        registered = data['plugins']&.any? { |p| p['name'] == plugin }
        if registered
          { status: 'ok', path: path }
        else
          {
            status: 'issue',
            path: path,
            issue: "Plugin '#{plugin}' not registered in #{path}",
            fix: register_fix
          }
        end
      end

      def check_installed
        path = installed_plugins_path
        return missing_check('installed_plugins', path, install_fix) unless File.exist?(path)

        data = read_json(path)
        return parse_error_check('installed_plugins', path) unless data

        installed = data['plugins']&.key?(plugin_key)
        if installed
          { status: 'ok', path: path }
        else
          {
            status: 'issue',
            path: path,
            issue: "Plugin '#{plugin_key}' not found in installed plugins",
            fix: install_fix
          }
        end
      end

      def check_settings
        path = settings_path
        return missing_check('settings', path, enable_fix) unless project && File.exist?(path)

        data = read_json(path)
        return parse_error_check('settings', path) unless data

        enabled = data['enabledPlugins']&.key?(plugin_key)
        if enabled
          { status: 'ok', path: path }
        else
          {
            status: 'issue',
            path: path,
            issue: "Plugin '#{plugin_key}' not enabled in #{path}",
            fix: enable_fix
          }
        end
      end

      def missing_check(name, path, fix)
        {
          status: 'issue',
          path: path,
          issue: "#{name} file not found at #{path}",
          fix: fix
        }
      end

      def parse_error_check(name, path)
        {
          status: 'issue',
          path: path,
          issue: "#{name} file at #{path} contains invalid JSON"
        }
      end

      def register_fix
        "Register plugin: ruby plugin_config.rb register --plugin=#{plugin} --marketplace_file=<path>"
      end

      def install_fix
        "Install plugin: /install #{plugin_key}"
      end

      def enable_fix
        "Enable plugin: ruby plugin_config.rb enable --plugin=#{plugin} --marketplace=#{marketplace} --project=#{project}"
      end
    end
  end
end
