# typed: false
FactoryBot.define do
  factory :http_log_entry do
    transient { uri_str { "https://example.com/path" } }
    uri_host { URI.parse(uri_str).host }
    uri_scheme { URI.parse(uri_str).scheme }
    uri_path { URI.parse(uri_str).path }
    uri_query { URI.parse(uri_str).query }
    verb { :get }
    status_code { 200 }
    content_type { "text/html" }
    response_time_ms { 100 }
    requested_at { Time.now }
    created_at { Time.now }
    performed_by { "direct" }

    # Create associated records
    association :response, factory: :blob_file
    association :request_headers, factory: :http_log_entry_header
    association :response_headers, factory: :http_log_entry_header

    # Set the response_sha256 to match the associated response
    after(:build) do |entry|
      entry.response_sha256 = entry.response.sha256 if entry.response
    end

    trait :post_request do
      verb { :post }
    end

    trait :with_query do
      uri_query { "foo=bar&baz=qux" }
    end

    trait :with_fragment do
      uri_hash { "section1" }
    end

    trait :with_error do
      status_code { 404 }
    end

    trait :with_caused_by do
      association :caused_by_entry, factory: :http_log_entry
    end

    trait :legacy do
      performed_by { "legacy" }
    end

    trait :proxy do
      performed_by { "proxy-1" }
    end

    trait :image_entry do
      content_type { "image/jpeg" }
      response { create(:blob_file, :image_blob) }
    end

    trait :text_entry do
      content_type { "text/plain" }
      response { create(:blob_file, :text_blob) }
    end

    trait :gif_entry do
      content_type { "image/gif" }
      response { create(:blob_file, :gif_blob) }
    end

    trait :webm_entry do
      content_type { "video/webm" }
      response { create(:blob_file, :webm_blob) }
    end
  end
end
