# frozen_string_literal: true

shared_context 'jira projects request context' do
  let(:url) { 'https://jira.example.com' }
  let(:username) { 'jira-username' }
  let(:password) { 'jira-password' }
  let!(:jira_service) do
    create(:jira_service,
      project: project,
      url: url,
      username: username,
      password: password
    )
  end

  let_it_be(:jira_projects_json) do
    '{
          "self": "https://your-domain.atlassian.net/rest/api/2/project/search?startAt=0&maxResults=2",
          "nextPage": "https://your-domain.atlassian.net/rest/api/2/project/search?startAt=2&maxResults=2",
          "maxResults": 2,
          "startAt": 0,
          "total": 7,
          "isLast": false,
          "values": [
            {
              "self": "https://your-domain.atlassian.net/rest/api/2/project/EX",
              "id": "10000",
              "key": "EX",
              "name": "Example",
              "avatarUrls": {
                "48x48": "https://your-domain.atlassian.net/secure/projectavatar?size=large&pid=10000",
                "24x24": "https://your-domain.atlassian.net/secure/projectavatar?size=small&pid=10000",
                "16x16": "https://your-domain.atlassian.net/secure/projectavatar?size=xsmall&pid=10000",
                "32x32": "https://your-domain.atlassian.net/secure/projectavatar?size=medium&pid=10000"
              },
              "projectCategory": {
                "self": "https://your-domain.atlassian.net/rest/api/2/projectCategory/10000",
                "id": "10000",
                "name": "FIRST",
                "description": "First Project Category"
              },
              "simplified": false,
              "style": "classic",
              "insight": {
                "totalIssueCount": 100,
                "lastIssueUpdateTime": "2020-03-31T05:45:24.792+0000"
              }
            },
            {
              "self": "https://your-domain.atlassian.net/rest/api/2/project/ABC",
              "id": "10001",
              "key": "ABC",
              "name": "Alphabetical",
              "avatarUrls": {
                "48x48": "https://your-domain.atlassian.net/secure/projectavatar?size=large&pid=10001",
                "24x24": "https://your-domain.atlassian.net/secure/projectavatar?size=small&pid=10001",
                "16x16": "https://your-domain.atlassian.net/secure/projectavatar?size=xsmall&pid=10001",
                "32x32": "https://your-domain.atlassian.net/secure/projectavatar?size=medium&pid=10001"
              },
              "projectCategory": {
                "self": "https://your-domain.atlassian.net/rest/api/2/projectCategory/10000",
                "id": "10000",
                "name": "FIRST",
                "description": "First Project Category"
              },
              "simplified": false,
              "style": "classic",
              "insight": {
                "totalIssueCount": 100,
                "lastIssueUpdateTime": "2020-03-31T05:45:24.792+0000"
              }
            }
          ]
        }'
  end

  let_it_be(:empty_jira_projects_json) do
    '{
          "self": "https://your-domain.atlassian.net/rest/api/2/project/search?startAt=0&maxResults=2",
          "nextPage": "https://your-domain.atlassian.net/rest/api/2/project/search?startAt=2&maxResults=2",
          "maxResults": 2,
          "startAt": 0,
          "total": 7,
          "isLast": false,
          "values": []
    }'
  end

  let(:test_url) { "#{url}/rest/api/2/project/search?maxResults=50&query=&startAt=0" }
  let(:start_at_20_url) { "#{url}/rest/api/2/project/search?maxResults=50&query=&startAt=20" }
  let(:start_at_1_url) { "#{url}/rest/api/2/project/search?maxResults=50&query=&startAt=1" }
  let(:max_results_1_url) { "#{url}/rest/api/2/project/search?maxResults=1&query=&startAt=0" }

  before do
    WebMock.stub_request(:get, test_url).with(basic_auth: [username, password])
      .to_return(body: jira_projects_json, headers: { "Content-Type": "application/json" })
    WebMock.stub_request(:get, start_at_20_url).with(basic_auth: [username, password])
      .to_return(body: empty_jira_projects_json, headers: { "Content-Type": "application/json" })
    WebMock.stub_request(:get, start_at_1_url).with(basic_auth: [username, password])
      .to_return(body: jira_projects_json, headers: { "Content-Type": "application/json" })
    WebMock.stub_request(:get, max_results_1_url).with(basic_auth: [username, password])
      .to_return(body: jira_projects_json, headers: { "Content-Type": "application/json" })
  end
end
