import { expect, vi } from 'vitest'
import sinon from 'sinon'
import { RequestFailedError } from '@overleaf/fetch-utils'
import OError from '@overleaf/o-error'
const modulePath = '../../../../app/src/Features/Exports/ExportsHandler.mjs'

describe('ExportsHandler', function () {
  beforeEach(async function (ctx) {
    vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
      default: (ctx.ProjectGetter = { promises: {} }),
    }))

    vi.doMock(
      '../../../../app/src/Features/Project/ProjectHistoryHandler',
      () => ({
        default: (ctx.ProjectHistoryHandler = {
          promises: {},
        }),
      })
    )

    ctx.fetchUtils = {
      fetchJson: sinon.stub().resolves(),
      fetchString: sinon.stub().resolves(),
      RequestFailedError,
    }

    vi.doMock('@overleaf/fetch-utils', () => ctx.fetchUtils)

    vi.doMock(
      '../../../../app/src/Features/Project/ProjectRootDocManager',
      () => ({
        default: (ctx.ProjectRootDocManager = { promises: {} }),
      })
    )

    vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
      default: (ctx.UserGetter = { promises: {} }),
    }))

    vi.doMock('@overleaf/settings', () => ({
      default: (ctx.settings = {}),
    }))

    ctx.ExportsHandler = (await import(modulePath)).default
    ctx.project_id = 'project-id-123'
    ctx.project_history_id = 987
    ctx.user_id = 'user-id-456'
    ctx.brand_variation_id = 789
    ctx.title = 'title'
    ctx.description = 'description'
    ctx.author = 'author'
    ctx.license = 'other'
    ctx.show_source = true
    ctx.export_params = {
      project_id: ctx.project_id,
      brand_variation_id: ctx.brand_variation_id,
      user_id: ctx.user_id,
      title: ctx.title,
      description: ctx.description,
      author: ctx.author,
      license: ctx.license,
      show_source: ctx.show_source,
    }
  })

  describe('exportProject', function () {
    beforeEach(function (ctx) {
      ctx.export_data = { iAmAnExport: true }
      ctx.response_body = { iAmAResponseBody: true }
      ctx.ExportsHandler._buildExport = sinon.stub().resolves(ctx.export_data)
      ctx.ExportsHandler._requestExport = sinon
        .stub()
        .resolves(ctx.response_body)
    })

    describe('when all goes well', function () {
      let exportData
      beforeEach(async function (ctx) {
        exportData = await ctx.ExportsHandler.exportProject(ctx.export_params)
      })

      it('should build the export', function (ctx) {
        ctx.ExportsHandler._buildExport
          .calledWith(ctx.export_params)
          .should.equal(true)
      })

      it('should request the export', function (ctx) {
        ctx.ExportsHandler._requestExport
          .calledWith(ctx.export_data)
          .should.equal(true)
      })

      it('should return the export', function (ctx) {
        expect(exportData).to.equal(ctx.export_data)
      })
    })

    describe("when request can't be built", function () {
      beforeEach(async function (ctx) {
        ctx.ExportsHandler._buildExport = sinon
          .stub()
          .rejects(new Error('cannot export project without root doc'))
      })

      it('should reject with an error', async function (ctx) {
        await expect(
          ctx.ExportsHandler.exportProject(ctx.export_params)
        ).to.be.rejectedWith(Error)
      })
    })

    describe('when export request returns an error to forward to the user', function () {
      let exportData
      let exportError

      beforeEach(async function (ctx) {
        ctx.error_json = { status: 422, message: 'nope' }
        const error = new Error('error message')
        ctx.ExportsHandler._requestExport = sinon.stub().rejects(
          OError.tag(error, 'v1 error', {
            forwardResponse: ctx.error_json,
          })
        )
        try {
          exportData = await ctx.ExportsHandler.exportProject(ctx.export_params)
        } catch (error) {
          exportError = error
        }
      })

      it('should throw with an error containing a response to forward', function (ctx) {
        expect(exportData).to.be.undefined
        expect(OError.getFullInfo(exportError)).to.eql({
          forwardResponse: ctx.error_json,
        })
      })
    })
  })

  describe('_buildExport', function () {
    beforeEach(async function (ctx) {
      ctx.project = {
        id: ctx.project_id,
        rootDoc_id: 'doc1_id',
        compiler: 'pdflatex',
        imageName: 'mock-image-name',
        overleaf: {
          id: ctx.project_history_id, // for projects imported from v1
          history: {
            id: ctx.project_history_id,
          },
        },
      }
      ctx.user = {
        id: ctx.user_id,
        first_name: 'Arthur',
        last_name: 'Author',
        email: 'arthur.author@arthurauthoring.org',
        overleaf: {
          id: 876,
        },
      }
      ctx.rootDocPath = 'main.tex'
      ctx.historyVersion = 777
      ctx.ProjectGetter.promises.getProject = sinon.stub().resolves(ctx.project)
      ctx.ProjectHistoryHandler.promises.ensureHistoryExistsForProject = sinon
        .stub()
        .resolves()
      ctx.ProjectRootDocManager.promises.ensureRootDocumentIsValid = sinon
        .stub()
        .resolves({ rootResourcePath: 'main.tex' })
      ctx.UserGetter.promises.getUser = sinon.stub().resolves(ctx.user)
      ctx.ExportsHandler._requestVersion = sinon
        .stub()
        .resolves(ctx.historyVersion)
    })

    describe('when all goes well', function () {
      let exportData
      beforeEach(async function (ctx) {
        exportData = await ctx.ExportsHandler._buildExport(ctx.export_params)
      })

      it('should ensure the project has history', function (ctx) {
        ctx.ProjectHistoryHandler.promises.ensureHistoryExistsForProject.called.should.equal(
          true
        )
      })

      it('should request the project history version', function (ctx) {
        ctx.ExportsHandler._requestVersion.called.should.equal(true)
      })

      it('should return export data', function (ctx) {
        const expectedExportData = {
          project: {
            id: ctx.project_id,
            rootDocPath: ctx.rootDocPath,
            historyId: ctx.project_history_id,
            historyVersion: ctx.historyVersion,
            v1ProjectId: ctx.project_history_id,
            metadata: {
              compiler: 'pdflatex',
              imageName: 'mock-image-name',
              title: ctx.title,
              description: ctx.description,
              author: ctx.author,
              license: ctx.license,
              showSource: ctx.show_source,
            },
          },
          user: {
            id: ctx.user_id,
            firstName: ctx.user.first_name,
            lastName: ctx.user.last_name,
            email: ctx.user.email,
            orcidId: null,
            v1UserId: 876,
          },
          destination: {
            brandVariationId: ctx.brand_variation_id,
          },
          options: {
            callbackUrl: null,
          },
        }
        expect(exportData).to.eql(expectedExportData)
      })
    })

    describe('when we send replacement user first and last name', function () {
      let exportData
      beforeEach(async function (ctx) {
        ctx.custom_first_name = 'FIRST'
        ctx.custom_last_name = 'LAST'
        ctx.export_params.first_name = ctx.custom_first_name
        ctx.export_params.last_name = ctx.custom_last_name
        exportData = await ctx.ExportsHandler._buildExport(ctx.export_params)
      })

      it('should send the data from the user input', function (ctx) {
        const expectedExportData = {
          project: {
            id: ctx.project_id,
            rootDocPath: ctx.rootDocPath,
            historyId: ctx.project_history_id,
            historyVersion: ctx.historyVersion,
            v1ProjectId: ctx.project_history_id,
            metadata: {
              compiler: 'pdflatex',
              imageName: 'mock-image-name',
              title: ctx.title,
              description: ctx.description,
              author: ctx.author,
              license: ctx.license,
              showSource: ctx.show_source,
            },
          },
          user: {
            id: ctx.user_id,
            firstName: ctx.custom_first_name,
            lastName: ctx.custom_last_name,
            email: ctx.user.email,
            orcidId: null,
            v1UserId: 876,
          },
          destination: {
            brandVariationId: ctx.brand_variation_id,
          },
          options: {
            callbackUrl: null,
          },
        }
        expect(exportData).to.eql(expectedExportData)
      })
    })

    describe('when project is not found', function () {
      beforeEach(async function (ctx) {
        ctx.ProjectGetter.promises.getProject = sinon
          .stub()
          .rejects(new Error('project not found'))
      })

      it('should return an error', async function (ctx) {
        await expect(
          ctx.ExportsHandler._buildExport(ctx.export_params)
        ).to.be.rejectedWith(Error)
      })
    })

    describe('when project has no root doc', function () {
      describe('when a root doc can be set automatically', function () {
        let exportData
        beforeEach(async function (ctx) {
          ctx.project.rootDoc_id = null
          ctx.ProjectRootDocManager.promises.ensureRootDocumentIsValid = sinon
            .stub()
            .resolves({ rootResourcePath: 'other.tex' })
          exportData = await ctx.ExportsHandler._buildExport(ctx.export_params)
        })

        it('should set a root doc', function (ctx) {
          ctx.ProjectRootDocManager.promises.ensureRootDocumentIsValid.called.should.equal(
            true
          )
        })

        it('should return export data', function (ctx) {
          const expectedExportData = {
            project: {
              id: ctx.project_id,
              rootDocPath: 'other.tex',
              historyId: ctx.project_history_id,
              historyVersion: ctx.historyVersion,
              v1ProjectId: ctx.project_history_id,
              metadata: {
                compiler: 'pdflatex',
                imageName: 'mock-image-name',
                title: ctx.title,
                description: ctx.description,
                author: ctx.author,
                license: ctx.license,
                showSource: ctx.show_source,
              },
            },
            user: {
              id: ctx.user_id,
              firstName: ctx.user.first_name,
              lastName: ctx.user.last_name,
              email: ctx.user.email,
              orcidId: null,
              v1UserId: 876,
            },
            destination: {
              brandVariationId: ctx.brand_variation_id,
            },
            options: {
              callbackUrl: null,
            },
          }
          expect(exportData).to.eql(expectedExportData)
        })
      })
    })

    describe('when project has an invalid root doc', function () {
      describe('when a new root doc can be set automatically', function () {
        let exportData
        beforeEach(async function (ctx) {
          ctx.fakeDoc_id = '1a2b3c4d5e6f'
          ctx.project.rootDoc_id = ctx.fakeDoc_id
          ctx.ProjectRootDocManager.promises.ensureRootDocumentIsValid = sinon
            .stub()
            .resolves({ rootResourcePath: 'other.tex' })
          exportData = await ctx.ExportsHandler._buildExport(ctx.export_params)
        })

        it('should set a valid root doc', function (ctx) {
          ctx.ProjectRootDocManager.promises.ensureRootDocumentIsValid.called.should.equal(
            true
          )
        })

        it('should return export data', function (ctx) {
          const expectedExportData = {
            project: {
              id: ctx.project_id,
              rootDocPath: 'other.tex',
              historyId: ctx.project_history_id,
              historyVersion: ctx.historyVersion,
              v1ProjectId: ctx.project_history_id,
              metadata: {
                compiler: 'pdflatex',
                imageName: 'mock-image-name',
                title: ctx.title,
                description: ctx.description,
                author: ctx.author,
                license: ctx.license,
                showSource: ctx.show_source,
              },
            },
            user: {
              id: ctx.user_id,
              firstName: ctx.user.first_name,
              lastName: ctx.user.last_name,
              email: ctx.user.email,
              orcidId: null,
              v1UserId: 876,
            },
            destination: {
              brandVariationId: ctx.brand_variation_id,
            },
            options: {
              callbackUrl: null,
            },
          }
          expect(exportData).to.eql(expectedExportData)
        })
      })

      describe('when no root doc can be identified', function () {
        beforeEach(function (ctx) {
          ctx.ProjectRootDocManager.promises.ensureRootDocumentIsValid = sinon
            .stub()
            .resolves()
        })

        it('should return an error', async function (ctx) {
          await expect(
            ctx.ExportsHandler._buildExport(ctx.export_params)
          ).to.be.rejectedWith(Error)
        })
      })
    })

    describe('when user is not found', function () {
      beforeEach(async function (ctx) {
        ctx.UserGetter.promises.getUser = sinon
          .stub()
          .rejects(new Error('user not found'))
      })

      it('should return an error', async function (ctx) {
        await expect(
          ctx.ExportsHandler._buildExport(ctx.export_params)
        ).to.be.rejectedWith(Error)
      })
    })

    describe('when project history request fails', function () {
      beforeEach(function (ctx) {
        ctx.ExportsHandler._requestVersion = sinon
          .stub()
          .rejects(new Error('project history call failed'))
      })

      it('should return an error', async function (ctx) {
        await expect(
          ctx.ExportsHandler._buildExport(ctx.export_params)
        ).to.be.rejectedWith(Error)
      })
    })
  })

  describe('_requestExport', function () {
    beforeEach(function (ctx) {
      ctx.settings.apis = {
        v1: {
          url: 'http://127.0.0.1:5000',
          user: 'overleaf',
          pass: 'pass',
          timeout: 15000,
        },
      }
      ctx.export_data = { iAmAnExport: true }
      ctx.export_id = 4096
      ctx.export_token = 'mock-export-token'
      ctx.fetchUtils.fetchJson.resolves({
        exportId: ctx.export_id,
        token: ctx.export_token,
      })
    })

    describe('when all goes well', function () {
      let response
      beforeEach(async function (ctx) {
        response = await ctx.ExportsHandler._requestExport(ctx.export_data)
      })

      it('should issue the request', function (ctx) {
        expect(ctx.fetchUtils.fetchJson).to.have.been.calledWith(
          new URL('/api/v1/overleaf/exports', ctx.settings.apis.v1.url),
          {
            basicAuth: {
              user: ctx.settings.apis.v1.user,
              password: ctx.settings.apis.v1.pass,
            },
            json: ctx.export_data,
            method: 'POST',
            signal: sinon.match.instanceOf(AbortSignal),
          }
        )
      })

      it('should return the body with v1 export id and token', function (ctx) {
        expect(response).to.eql({
          exportId: ctx.export_id,
          token: ctx.export_token,
        })
      })
    })

    describe('when the request fails', function () {
      beforeEach(function (ctx) {
        ctx.fetchUtils.fetchJson = sinon
          .stub()
          .rejects(new Error('export request failed'))
      })

      it('should return an error', async function (ctx) {
        await expect(
          ctx.ExportsHandler._requestExport(ctx.export_data)
        ).to.be.rejectedWith(Error)
      })
    })

    describe('when the request returns an error response to forward', function () {
      let response
      let error
      beforeEach(async function (ctx) {
        ctx.error_code = 422
        ctx.error_json = { status: ctx.error_code, message: 'nope' }
        ctx.fetchUtils.fetchJson = sinon
          .stub()
          .rejects(
            new RequestFailedError(
              new URL('/api/v1/overleaf/exports', ctx.settings.apis.v1.url),
              {},
              { status: ctx.error_code },
              ctx.error_json
            )
          )

        try {
          response = await ctx.ExportsHandler._requestExport(ctx.export_data)
        } catch (err) {
          error = err
        }
      })

      it('should throw with an error containing the response to forward', function (ctx) {
        expect(response).to.be.undefined
        expect(OError.getFullInfo(error)).to.have.property(
          'forwardResponse',
          ctx.error_json
        )
      })
    })
  })

  describe('fetchExport', function () {
    beforeEach(function (ctx) {
      ctx.settings.apis = {
        v1: {
          url: 'http://127.0.0.1:5000',
          user: 'overleaf',
          pass: 'pass',
          timeout: 15000,
        },
      }
      ctx.export_id = 897
      ctx.export_token = 'mock-export-token'
      ctx.body = '{"id":897, "status_summary":"completed"}'
      ctx.fetchUtils.fetchString = sinon.stub().resolves(ctx.body)
    })

    describe('when all goes well with token', function () {
      let exportResponse
      beforeEach(async function (ctx) {
        exportResponse = await ctx.ExportsHandler.fetchExport(
          ctx.export_id,
          ctx.export_token
        )
      })

      it('should issue the request with token', function (ctx) {
        const expectedUrl = new URL(
          '/api/v1/overleaf/exports/' + ctx.export_id,
          ctx.settings.apis.v1.url
        )
        expectedUrl.searchParams.append('token', ctx.export_token)
        expect(ctx.fetchUtils.fetchString).to.have.been.calledWith(
          expectedUrl,
          {
            basicAuth: {
              user: ctx.settings.apis.v1.user,
              password: ctx.settings.apis.v1.pass,
            },
            signal: sinon.match.instanceOf(AbortSignal),
          }
        )
      })

      it('should return the v1 export id', function (ctx) {
        expect(exportResponse).to.eql(ctx.body)
      })
    })

    describe('when called without token', function () {
      let exportResponse
      beforeEach(async function (ctx) {
        exportResponse = await ctx.ExportsHandler.fetchExport(ctx.export_id)
      })

      it('should issue the request without token', function (ctx) {
        expect(ctx.fetchUtils.fetchString).to.have.been.calledWith(
          new URL(
            '/api/v1/overleaf/exports/' + ctx.export_id,
            ctx.settings.apis.v1.url
          ),
          {
            basicAuth: {
              user: ctx.settings.apis.v1.user,
              password: ctx.settings.apis.v1.pass,
            },
            signal: sinon.match.instanceOf(AbortSignal),
          }
        )
      })

      it('should return the v1 export id', function (ctx) {
        expect(exportResponse).to.eql(ctx.body)
      })
    })
  })

  describe('fetchDownload', function () {
    beforeEach(async function (ctx) {
      ctx.settings.apis = {
        v1: {
          url: 'http://127.0.0.1:5000',
          user: 'overleaf',
          pass: 'pass',
          timeout: 15000,
        },
      }
      ctx.export_id = 897
      ctx.export_token = 'mock-export-token'
      ctx.body =
        'https://writelatex-conversions-dev.s3.amazonaws.com/exports/ieee_latexqc/tnb/2912/xggmprcrpfwbsnqzqqmvktddnrbqkqkr.zip?X-Amz-Expires=14400&X-Amz-Date=20180730T181003Z&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJDGDIJFGLNVGZH6A/20180730/us-east-1/s3/aws4_request&X-Amz-SignedHeaders=host&X-Amz-Signature=dec990336913cef9933f0e269afe99722d7ab2830ebf2c618a75673ee7159fee'
      ctx.fetchUtils.fetchString = sinon.stub().resolves(ctx.body)
    })

    describe('when all goes well with token', function () {
      let downloadResponse
      beforeEach(async function (ctx) {
        downloadResponse = await ctx.ExportsHandler.fetchDownload(
          ctx.export_id,
          'zip',
          ctx.export_token
        )
      })

      it('should issue the request with token', function (ctx) {
        const expectedUrl = new URL(
          ctx.settings.apis.v1.url +
            '/api/v1/overleaf/exports/' +
            ctx.export_id +
            '/zip_url'
        )
        expectedUrl.searchParams.append('token', ctx.export_token)
        expect(ctx.fetchUtils.fetchString).to.have.been.calledWith(
          expectedUrl,
          {
            basicAuth: {
              user: ctx.settings.apis.v1.user,
              password: ctx.settings.apis.v1.pass,
            },
            signal: sinon.match.instanceOf(AbortSignal),
          }
        )
      })

      it('should return the download URL', function (ctx) {
        expect(downloadResponse).to.eql(ctx.body)
      })
    })

    describe('when called without token', function () {
      let downloadResponse
      beforeEach(async function (ctx) {
        downloadResponse = await ctx.ExportsHandler.fetchDownload(
          ctx.export_id,
          'zip'
        )
      })

      it('should issue the request without token', function (ctx) {
        expect(ctx.fetchUtils.fetchString).to.have.been.calledWith(
          new URL(
            ctx.settings.apis.v1.url +
              '/api/v1/overleaf/exports/' +
              ctx.export_id +
              '/zip_url'
          ),
          {
            basicAuth: {
              user: ctx.settings.apis.v1.user,
              password: ctx.settings.apis.v1.pass,
            },
            signal: sinon.match.instanceOf(AbortSignal),
          }
        )
      })

      it('should return the download URL', function (ctx) {
        expect(downloadResponse).to.eql(ctx.body)
      })
    })
  })
})
