# frozen_string_literal: true

require_relative "../github/base"

module Neetob
  class CLI
    module Code
      class Audit < Github::Base
        attr_accessor :sandbox, :apps

        def initialize(apps, sandbox = false)
          super()
          @apps = apps
          @sandbox = sandbox
        end

        def run
          matching_apps = find_all_matching_apps_or_repos(apps, :github, sandbox)
          ui.info("\nListing apps and their tables that doesn't have uuid as primary key type:-")
          has_found_tables_without_uuid = false
          matching_apps.each do |app|
            begin
              db_schema = Base64.decode64(client.contents(app, path: "./db/schema.rb").content)
              tables_without_uuid = find_tables_without_uuid_as_primary_key(db_schema).compact
              if !tables_without_uuid.nil?
                has_found_tables_without_uuid = true
                print_app_and_tables(app, tables_without_uuid)
              end
              verify_unique_email_index(db_schema)
            rescue Octokit::NotFound
              ui.error("There is no \"db/schema.rb\" file in the \"#{app}\" app.")
            rescue StandardError => e
              ExceptionHandler.new(e).process
            end
          end
          ui.info("No apps found to have tables without uuid as primary key type") if !has_found_tables_without_uuid
        end

        private

          def find_tables_without_uuid_as_primary_key(db_schema)
            create_table_regex = /create_table.*?,\s*force:\s*:cascade\s*do\s*\|t\|/
            db_schema.scan(create_table_regex).map do |create_table_line|
              !create_table_line.include?("id: :uuid") ? create_table_line.scan(/"([^"]+)"/).flatten.first : nil
            end
          end

          def verify_unique_email_index(db_schema)
            index_pattern = /
              create_table\s+"users".*?
              (
                t\.index\s+\[
                  \s*"email"\s*
                \],\s+
                name:\s*"index_users_on_email"
                |
                t\.index\s+\[
                  \s*"email"\s*,\s*"organization_id"\s*
                \],\s+
                name:\s*"index_users_on_email_and_organization_id"\s*,\s+
                unique:\s*true
              )
            /mx

            if db_schema.match(index_pattern)
              ui.success("\nUsers are indexed properly")
            else
              ui.error("\nUsers are not indexed")
            end
          end

          def print_app_and_tables(app, tables)
            ui.info("\n#{app}:-")
            tables.each do |table|
              ui.say(" ↳#{table}")
            end
          end
      end
    end
  end
end