import {describe, expect, it} from 'vitest'

import {type ReleaseDocument} from '../../../store/types'
import {RELEASE_STATE_CHANGE} from './__fixtures__/mock-translog-events'
import {buildReleaseEditEvents} from './buildReleaseEditEvents'

describe('buildReleaseEditEvents()', () => {
  it('should identify a metadata.releaseType change', () => {
    const release = {
      userId: '',
      _createdAt: '2024-12-05T16:34:59Z',
      _rev: '27IdYXOVe1PEc0ZOADFAhQ',
      name: 'rWBfpXZVj',
      state: 'active',
      _updatedAt: '2024-12-05T17:09:28Z',
      metadata: {
        releaseType: 'undecided',
        description: '',
        title: 'winter drop',
      },
      publishAt: null,
      _id: '_.releases.rWBfpXZVj',
      _type: 'system.release',
      finalDocumentStates: null,
    } as unknown as ReleaseDocument
    const changes = buildReleaseEditEvents(
      [
        {
          id: '27IdYXOVe1PEc0ZOADFAhQ',
          timestamp: '2024-12-05T17:09:28.325641Z',
          author: 'p8xDvUMxC',
          documentIDs: ['_.releases.rWBfpXZVj'],
          effects: {
            '_.releases.rWBfpXZVj': {
              apply: [
                11,
                3,
                23,
                0,
                12,
                22,
                '7:09:28',
                23,
                19,
                20,
                15,
                10,
                5,
                19,
                1,
                17,
                'undecided',
                'releaseType',
                15,
              ],
              revert: [
                11,
                3,
                23,
                0,
                12,
                22,
                '6:35:11',
                23,
                19,
                20,
                15,
                10,
                5,
                17,
                '2024-12-20T16:35:00.000Z',
                'intendedPublishAt',
                17,
                'scheduled',
                'releaseType',
                15,
              ],
            },
          },
        },
      ],
      release,
    )
    expect(changes).toEqual([
      {
        type: 'editRelease',
        author: 'p8xDvUMxC',
        origin: 'translog',
        change: {releaseType: 'undecided', intendedPublishDate: undefined},
        id: '27IdYXOVe1PEc0ZOADFAhQ',
        timestamp: '2024-12-05T17:09:28.325641Z',
        releaseName: 'rWBfpXZVj',
      },
    ])
  })
  it('should identify a intededPublishDate change', () => {
    const release = {
      publishAt: null,
      _rev: 'zGoOhrVQZLzwh7QVfgQryJ',
      _id: '_.releases.rWBfpXZVj',
      _createdAt: '2024-12-05T16:34:59Z',
      userId: '',
      metadata: {
        releaseType: 'scheduled',
        description: '',
        title: 'winter drop',
        intendedPublishAt: '2024-12-13T17:12:00.000Z',
      },
      name: 'rWBfpXZVj',
      state: 'active',
      _updatedAt: '2024-12-05T17:12:56Z',
      _type: 'system.release',
      finalDocumentStates: null,
    } as unknown as ReleaseDocument
    const changes = buildReleaseEditEvents(
      [
        {
          id: 'zGoOhrVQZLzwh7QVfgQryJ',
          timestamp: '2024-12-05T17:12:56.253502Z',
          author: 'p8xDvUMxC',
          documentIDs: ['_.releases.rWBfpXZVj'],
          effects: {
            '_.releases.rWBfpXZVj': {
              apply: [
                11,
                3,
                23,
                0,
                17,
                22,
                '56',
                23,
                19,
                20,
                15,
                10,
                5,
                11,
                1,
                23,
                0,
                9,
                22,
                '3',
                23,
                10,
                24,
                15,
                15,
              ],
              revert: [
                11,
                3,
                23,
                0,
                17,
                22,
                '48',
                23,
                19,
                20,
                15,
                10,
                5,
                11,
                1,
                23,
                0,
                9,
                22,
                '2',
                23,
                10,
                24,
                15,
                15,
              ],
            },
          },
        },
      ],
      release,
    )
    expect(changes).toEqual([
      {
        type: 'editRelease',
        author: 'p8xDvUMxC',
        origin: 'translog',
        change: {intendedPublishDate: '2024-12-13T17:12:00.000Z'},
        id: 'zGoOhrVQZLzwh7QVfgQryJ',
        timestamp: '2024-12-05T17:12:56.253502Z',
        releaseName: 'rWBfpXZVj',
      },
    ])
  })
  it('should identify a metadata.releaseType and intendedPublishDate change', () => {
    const releaseDocument = {
      publishAt: null,
      finalDocumentStates: null,
      _id: '_.releases.rWBfpXZVj',
      state: 'active',
      _updatedAt: '2024-12-05T16:35:11Z',
      metadata: {
        releaseType: 'scheduled',
        description: '',
        title: 'winter drop',
        intendedPublishAt: '2024-12-20T16:35:00.000Z',
      },
      _rev: 'zGoOhrVQZLzwh7QVfgIGWK',
      _type: 'system.release',
      name: 'rWBfpXZVj',
      userId: '',
      _createdAt: '2024-12-05T16:34:59Z',
    } as unknown as ReleaseDocument

    const releaseEditEvents = buildReleaseEditEvents(
      [
        {
          id: 'zGoOhrVQZLzwh7QVfgIGWK',
          timestamp: '2024-12-05T16:35:11.995089Z',
          author: 'p8xDvUMxC',
          documentIDs: ['_.releases.rWBfpXZVj'],
          effects: {
            '_.releases.rWBfpXZVj': {
              apply: [
                11,
                3,
                23,
                0,
                15,
                22,
                '5:11',
                23,
                19,
                20,
                15,
                10,
                5,
                17,
                '2024-12-20T16:35:00.000Z',
                'intendedPublishAt',
                17,
                'scheduled',
                'releaseType',
                15,
              ],
              revert: [10, 0, 14, '_updatedAt', 10, 5, 19, 1, 17, 'asap', 'releaseType', 15],
            },
          },
        },
      ],
      releaseDocument,
    )
    expect(releaseEditEvents).toEqual([
      {
        type: 'editRelease',
        author: 'p8xDvUMxC',
        origin: 'translog',
        change: {
          releaseType: 'scheduled',
          intendedPublishDate: '2024-12-20T16:35:00.000Z',
        },
        id: 'zGoOhrVQZLzwh7QVfgIGWK',
        timestamp: '2024-12-05T16:35:11.995089Z',
        releaseName: 'rWBfpXZVj',
      },
    ])
  })
  it('should handle multiple changes correctly', () => {
    const release = {
      publishAt: null,
      _rev: 'zGoOhrVQZLzwh7QVfgQryJ',
      _id: '_.releases.rWBfpXZVj',
      _createdAt: '2024-12-05T16:34:59Z',
      userId: '',
      metadata: {
        releaseType: 'scheduled',
        description: '',
        title: 'winter drop',
        intendedPublishAt: '2024-12-13T17:12:00.000Z',
      },
      name: 'rWBfpXZVj',
      state: 'active',
      _updatedAt: '2024-12-05T17:12:56Z',
      _type: 'system.release',
      finalDocumentStates: null,
    } as unknown as ReleaseDocument
    const changes = buildReleaseEditEvents(
      [
        {
          id: 'zGoOhrVQZLzwh7QVfgQryJ',
          timestamp: '2024-12-05T17:12:56.253502Z',
          author: 'p8xDvUMxC',
          documentIDs: ['_.releases.rWBfpXZVj'],
          effects: {
            '_.releases.rWBfpXZVj': {
              apply: [
                11,
                3,
                23,
                0,
                17,
                22,
                '56',
                23,
                19,
                20,
                15,
                10,
                5,
                11,
                1,
                23,
                0,
                9,
                22,
                '3',
                23,
                10,
                24,
                15,
                15,
              ],
              revert: [
                11,
                3,
                23,
                0,
                17,
                22,
                '48',
                23,
                19,
                20,
                15,
                10,
                5,
                11,
                1,
                23,
                0,
                9,
                22,
                '2',
                23,
                10,
                24,
                15,
                15,
              ],
            },
          },
        },
        {
          id: '27IdYXOVe1PEc0ZOADFjtK',
          timestamp: '2024-12-05T17:12:48.742846Z',
          author: 'p8xDvUMxC',

          documentIDs: ['_.releases.rWBfpXZVj'],
          effects: {
            '_.releases.rWBfpXZVj': {
              apply: [
                11,
                3,
                23,
                0,
                14,
                22,
                '12:4',
                23,
                18,
                20,
                15,
                10,
                5,
                17,
                '2024-12-12T17:12:00.000Z',
                'intendedPublishAt',
                17,
                'scheduled',
                'releaseType',
                15,
              ],
              revert: [
                11,
                3,
                23,
                0,
                14,
                22,
                '09:2',
                23,
                18,
                20,
                15,
                10,
                5,
                19,
                1,
                17,
                'undecided',
                'releaseType',
                15,
              ],
            },
          },
        },
        {
          id: '27IdYXOVe1PEc0ZOADFAhQ',
          timestamp: '2024-12-05T17:09:28.325641Z',
          author: 'p8xDvUMxC',

          documentIDs: ['_.releases.rWBfpXZVj'],
          effects: {
            '_.releases.rWBfpXZVj': {
              apply: [
                11,
                3,
                23,
                0,
                12,
                22,
                '7:09:28',
                23,
                19,
                20,
                15,
                10,
                5,
                19,
                1,
                17,
                'undecided',
                'releaseType',
                15,
              ],
              revert: [
                11,
                3,
                23,
                0,
                12,
                22,
                '6:35:11',
                23,
                19,
                20,
                15,
                10,
                5,
                17,
                '2024-12-20T16:35:00.000Z',
                'intendedPublishAt',
                17,
                'scheduled',
                'releaseType',
                15,
              ],
            },
          },
        },
        {
          id: 'zGoOhrVQZLzwh7QVfgIGWK',
          timestamp: '2024-12-05T16:35:11.995089Z',
          author: 'p8xDvUMxC',

          documentIDs: ['_.releases.rWBfpXZVj'],
          effects: {
            '_.releases.rWBfpXZVj': {
              apply: [
                11,
                3,
                23,
                0,
                15,
                22,
                '5:11',
                23,
                19,
                20,
                15,
                10,
                5,
                17,
                '2024-12-20T16:35:00.000Z',
                'intendedPublishAt',
                17,
                'scheduled',
                'releaseType',
                15,
              ],
              revert: [10, 0, 14, '_updatedAt', 10, 5, 19, 1, 17, 'asap', 'releaseType', 15],
            },
          },
        },
        {
          id: '27IdYXOVe1PEc0ZOAD1jvY',
          timestamp: '2024-12-05T16:34:59.512774Z',
          author: 'p8xDvUMxC',
          documentIDs: ['_.releases.rWBfpXZVj'],
          effects: {
            '_.releases.rWBfpXZVj': {
              apply: [
                0,
                {
                  _createdAt: '2024-12-05T16:34:59Z',
                  _id: '_.releases.rWBfpXZVj',
                  _type: 'system.release',
                  _updatedAt: '2024-12-05T16:34:59Z',
                  finalDocumentStates: null,
                  metadata: {
                    description: '',
                    releaseType: 'asap',
                    title: 'winter drop',
                  },
                  name: 'rWBfpXZVj',
                  publishAt: null,
                  state: 'active',
                  userId: '',
                },
              ],
              revert: [0, null],
            },
          },
        },
      ],
      release,
    )

    expect(changes).toEqual([
      {
        type: 'editRelease',
        author: 'p8xDvUMxC',
        origin: 'translog',
        change: {intendedPublishDate: '2024-12-13T17:12:00.000Z'},
        id: 'zGoOhrVQZLzwh7QVfgQryJ',
        timestamp: '2024-12-05T17:12:56.253502Z',
        releaseName: 'rWBfpXZVj',
      },
      {
        type: 'editRelease',
        author: 'p8xDvUMxC',
        origin: 'translog',
        change: {releaseType: 'scheduled', intendedPublishDate: '2024-12-12T17:12:00.000Z'},
        id: '27IdYXOVe1PEc0ZOADFjtK',
        timestamp: '2024-12-05T17:12:48.742846Z',
        releaseName: 'rWBfpXZVj',
      },
      {
        type: 'editRelease',
        author: 'p8xDvUMxC',
        origin: 'translog',
        change: {releaseType: 'undecided', intendedPublishDate: undefined},
        id: '27IdYXOVe1PEc0ZOADFAhQ',
        timestamp: '2024-12-05T17:09:28.325641Z',
        releaseName: 'rWBfpXZVj',
      },
      {
        type: 'editRelease',
        author: 'p8xDvUMxC',
        origin: 'translog',
        change: {releaseType: 'scheduled', intendedPublishDate: '2024-12-20T16:35:00.000Z'},
        id: 'zGoOhrVQZLzwh7QVfgIGWK',
        timestamp: '2024-12-05T16:35:11.995089Z',
        releaseName: 'rWBfpXZVj',
      },
      {
        type: 'createRelease',
        author: 'p8xDvUMxC',
        origin: 'translog',
        change: {releaseType: 'asap'},
        id: '27IdYXOVe1PEc0ZOAD1jvY',
        timestamp: '2024-12-05T16:34:59.512774Z',
        releaseName: 'rWBfpXZVj',
      },
    ])
  })
  it('should identify state changes', () => {
    const changes = buildReleaseEditEvents(
      RELEASE_STATE_CHANGE.transactions,
      RELEASE_STATE_CHANGE.document,
    )

    expect(changes).toEqual([
      {
        type: 'publishRelease',
        timestamp: '2025-02-17T15:50:22.301295Z',
        author: 'p8xDvUMxC',
        releaseName: 'rY3Ko1I4q',
        id: 'ad56vrBQ94RnZtJ3uXY2W4',
        origin: 'translog',
      },
      {
        type: 'unarchiveRelease',
        timestamp: '2025-02-17T15:48:37.430863Z',
        author: 'p8xDvUMxC',
        releaseName: 'rY3Ko1I4q',
        id: 'ad56vrBQ94RnZtJ3uXY2N4',
        origin: 'translog',
      },
      {
        type: 'archiveRelease',
        timestamp: '2025-02-17T15:37:41.591300Z',
        author: 'p8xDvUMxC',
        releaseName: 'rY3Ko1I4q',
        id: 'ad56vrBQ94RnZtJ3uXY2K4',
        origin: 'translog',
      },
      {
        type: 'createRelease',
        origin: 'translog',
        author: 'p8xDvUMxC',
        change: {releaseType: 'asap'},
        id: 'g9WA6RAm4T2Bl8zZYz08dP',
        timestamp: '2025-02-17T15:37:30.977183Z',
        releaseName: 'rY3Ko1I4q',
      },
    ])
  })
})
