<commit_before>FactoryGirl.define do
  factory :user do
    sequence(:uid) { |n| "uid-#{n}" }
    sequence(:name) { |n| "<NAME> #{n}" }
    sequence(:email) { |n| "<EMAIL>" }
    if defined?(GDS::SSO::Config)
      # Grant permission to signin to the app using the gem
      permissions { ["signin"] }
    end
  end

  factory :editor, parent: :user do
    permissions %w(signin editor)
  end

  factory :gds_editor, parent: :user do
    organisation_slug "government-digital-service"
    organisation_content_id "af07d5a5-df63-4ddc-9383-6a666845ebe9"
    permissions %w(signin gds_editor)
  end

  factory :cma_editor, parent: :editor do
    organisation_slug "competition-and-markets-authority"
    organisation_content_id "957eb4ec-089b-4f71-ba2a-dc69ac8919ea"
  end

  factory :writer, aliases: [:cma_writer], parent: :editor do
    organisation_slug "competition-and-markets-authority"
    organisation_content_id "957eb4ec-089b-4f71-ba2a-dc69ac8919ea"
    permissions %w(signin)
  end

  factory :aaib_editor, parent: :editor do
    organisation_slug "air-accidents-investigation-branch"
    organisation_content_id "38eb5d8f-2d89-480c-8655-e2e7ac23f8f4"
  end

  factory :dfid_editor, parent: :editor do
    organisation_slug "department-for-international-development"
    organisation_content_id "db994552-7644-404d-a770-a2fe659c661f"
  end

  sequence :content_id do |_|
    SecureRandom.uuid
  end

  factory :document, class: Hash do
    content_id
    base_path "/a/b"
    title "Example document"
    description "This is the summary of example document"
    schema_name "specialist_document"
    publishing_app "specialist-publisher"
    rendering_app "specialist-frontend"
    locale "en"
    phase "live"
    redirects []
    update_type "major"
    public_updated_at "2015-11-16T11:53:30+00:00"
    first_published_at "2015-11-15T00:00:00"
    updated_at "2015-11-15T11:53:30"
    publication_state "draft"
    routes {
      [
        {
          "path" => base_path,
          "type" => "exact",
        }
      ]
    }

    details { default_details }

    transient do
      default_details {
        {
          "body" => [
            {
              "content_type" => "text/govspeak",
              "content" => "## Header" + ("\r\n\r\nThis is the long body of an example document" * 10)
            },
            {
              "content_type" => "text/html",
              "content" => ("<h2 id=\"header\">Header</h2>\n" + "\n<p>This is the long body of an example document</p>\n" * 10)
            }
          ],
          "headers" => [{
            "text" => "Header",
            "level" => 2,
            "id" => "header",
          }],
          "metadata" => default_metadata,
          "max_cache_time" => 10,
          "change_history" => [],
        }
      }
      default_metadata { {} }
    end

    initialize_with {
      merged_details = default_details.deep_stringify_keys.deep_merge(details.deep_stringify_keys)
      attributes.merge(details: merged_details)
    }
    to_create(&:deep_stringify_keys!)
  end

  factory :aaib_report, parent: :document do
    base_path "/aaib-reports/example-aaib-report"
    document_type "aaib_report"

    transient do
      default_metadata {
        {
          "date_of_occurrence" => "2015-10-10",
          "document_type" => "aaib_report",
        }
      }
    end
  end

  factory :cma_case, parent: :document do
    base_path "/cma-cases/example-cma-case"
    document_type "cma_case"

    transient do
      default_metadata {
        {
          "document_type" => "cma_case",
          "opened_date" => "2014-01-01",
          "closed_date" => "2015-01-01",
          "case_type" => "ca98-and-civil-cartels",
          "case_state" => "closed",
          "market_sector" => ["energy"],
          "outcome_type" => "ca98-no-grounds-for-action-non-infringement",
        }
      }
    end
  end

  factory :countryside_stewardship_grant, parent: :document do
    base_path "/countryside-stewardship-grants/example-countryside-stewardship-grant"
    document_type "countryside_stewardship_grant"

    transient do<commit_msg>More complete test data for AAIB Reports<commit_after>FactoryGirl.define do
  factory :user do
    sequence(:uid) { |n| "uid-#{n}" }
    sequence(:name) { |n| "<NAME> #{n}" }
    sequence(:email) { |n| "<EMAIL>" }
    if defined?(GDS::SSO::Config)
      # Grant permission to signin to the app using the gem
      permissions { ["signin"] }
    end
  end

  factory :editor, parent: :user do
    permissions %w(signin editor)
  end

  factory :gds_editor, parent: :user do
    organisation_slug "government-digital-service"
    organisation_content_id "af07d5a5-df63-4ddc-9383-6a666845ebe9"
    permissions %w(signin gds_editor)
  end

  factory :cma_editor, parent: :editor do
    organisation_slug "competition-and-markets-authority"
    organisation_content_id "957eb4ec-089b-4f71-ba2a-dc69ac8919ea"
  end

  factory :writer, aliases: [:cma_writer], parent: :editor do
    organisation_slug "competition-and-markets-authority"
    organisation_content_id "957eb4ec-089b-4f71-ba2a-dc69ac8919ea"
    permissions %w(signin)
  end

  factory :aaib_editor, parent: :editor do
    organisation_slug "air-accidents-investigation-branch"
    organisation_content_id "38eb5d8f-2d89-480c-8655-e2e7ac23f8f4"
  end

  factory :dfid_editor, parent: :editor do
    organisation_slug "department-for-international-development"
    organisation_content_id "db994552-7644-404d-a770-a2fe659c661f"
  end

  sequence :content_id do |_|
    SecureRandom.uuid
  end

  factory :document, class: Hash do
    content_id
    base_path "/a/b"
    title "Example document"
    description "This is the summary of example document"
    schema_name "specialist_document"
    publishing_app "specialist-publisher"
    rendering_app "specialist-frontend"
    locale "en"
    phase "live"
    redirects []
    update_type "major"
    public_updated_at "2015-11-16T11:53:30+00:00"
    first_published_at "2015-11-15T00:00:00"
    updated_at "2015-11-15T11:53:30"
    publication_state "draft"
    routes {
      [
        {
          "path" => base_path,
          "type" => "exact",
        }
      ]
    }

    details { default_details }

    transient do
      default_details {
        {
          "body" => [
            {
              "content_type" => "text/govspeak",
              "content" => "## Header" + ("\r\n\r\nThis is the long body of an example document" * 10)
            },
            {
              "content_type" => "text/html",
              "content" => ("<h2 id=\"header\">Header</h2>\n" + "\n<p>This is the long body of an example document</p>\n" * 10)
            }
          ],
          "headers" => [{
            "text" => "Header",
            "level" => 2,
            "id" => "header",
          }],
          "metadata" => default_metadata,
          "max_cache_time" => 10,
          "change_history" => [],
        }
      }
      default_metadata { {} }
    end

    initialize_with {
      merged_details = default_details.deep_stringify_keys.deep_merge(details.deep_stringify_keys)
      attributes.merge(details: merged_details)
    }
    to_create(&:deep_stringify_keys!)
  end

  factory :aaib_report, parent: :document do
    base_path "/aaib-reports/example-aaib-report"
    document_type "aaib_report"

    transient do
      default_metadata {
        {
          "date_of_occurrence" => "2015-10-10",
          "aircraft_category" => ["commercial-fixed-wing"],
          "report_type" => "annual-safety-report",
          "location" => "Near Popham Airfield, Hampshire",
          "aircraft_type" => "Alpi (Cavaciuti) Pioneer 400",
          "registration" => "G-CGVO",
          "document_type" => "aaib_report",
        }
      }
    end
  end

  factory :cma_case, parent: :document do
    base_path "/cma-cases/example-cma-case"
    document_type "cma_case"

    transient do
      default_metadata {
        {
          "document_type" => "cma_case",
          "opened_date" => "2014-01-01",
          "closed_date" => "2015-01-01",
          "case_type" => "ca98-and-civil-cartels",
          "case_state" => "closed",
          "market_sector" => ["energy"],
          "outcome_type" => "ca98-no-grounds-for-action-non-infringement",
        }
      }
    end
  end

  factory :countryside_stewardship_grant, parent: :document do
    base_path "/countryside-stewardship-grants/example-countryside-stewardship-grant"
    document_type "countryside_stewardship_grant"

    transient do
{'fraction_non_alphanumeric': 0.12183395872420262, 'fraction_numerical': 0.04233114446529081, 'mean_word_length': 2.53167701863354, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7430543', 'n_tokens_mistral': 3102, 'n_tokens_neox': 2878, 'n_words': 541}