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

import {
  clearHints,
  readHints,
  writePbiHint,
  writeProductHint,
  writeStoryHint,
  writeTaskHint,
} from '@/stores/product-workspace/restore'

describe('readHints', () => {
  it('retourneert lege defaults wanneer localStorage leeg is', () => {
    const hints = readHints()
    expect(hints.lastActiveProductId).toBeNull()
    expect(hints.perProduct).toEqual({})
  })

  it('herstelt hints uit localStorage', () => {
    localStorage.setItem(
      'product-workspace-hints',
      JSON.stringify({
        lastActiveProductId: 'p1',
        perProduct: { p1: { lastActivePbiId: 'pbi-1' } },
      }),
    )
    const hints = readHints()
    expect(hints.lastActiveProductId).toBe('p1')
    expect(hints.perProduct.p1.lastActivePbiId).toBe('pbi-1')
  })

  it('valt terug op defaults bij ongeldige JSON', () => {
    localStorage.setItem('product-workspace-hints', '{not-json')
    const hints = readHints()
    expect(hints.lastActiveProductId).toBeNull()
    expect(hints.perProduct).toEqual({})
  })

  it('valt terug op defaults bij verkeerde shape', () => {
    localStorage.setItem('product-workspace-hints', '"just a string"')
    const hints = readHints()
    expect(hints.perProduct).toEqual({})
  })
})

describe('writeProductHint', () => {
  it('schrijft lastActiveProductId', () => {
    writeProductHint('p1')
    expect(readHints().lastActiveProductId).toBe('p1')
  })

  it('overschrijft bestaande waarde', () => {
    writeProductHint('p1')
    writeProductHint('p2')
    expect(readHints().lastActiveProductId).toBe('p2')
  })

  it('accepteert null om hint te wissen', () => {
    writeProductHint('p1')
    writeProductHint(null)
    expect(readHints().lastActiveProductId).toBeNull()
  })
})

describe('writePbiHint', () => {
  it('schrijft lastActivePbiId per productId', () => {
    writePbiHint('prod-1', 'pbi-a')
    writePbiHint('prod-2', 'pbi-b')
    const hints = readHints()
    expect(hints.perProduct['prod-1'].lastActivePbiId).toBe('pbi-a')
    expect(hints.perProduct['prod-2'].lastActivePbiId).toBe('pbi-b')
  })

  it('null wist child story- en task-hints', () => {
    writePbiHint('prod-1', 'pbi-1')
    writeStoryHint('prod-1', 's-1')
    writeTaskHint('prod-1', 't-1')
    writePbiHint('prod-1', null)
    const hints = readHints()
    expect(hints.perProduct['prod-1'].lastActivePbiId).toBeNull()
    expect(hints.perProduct['prod-1'].lastActiveStoryId).toBeNull()
    expect(hints.perProduct['prod-1'].lastActiveTaskId).toBeNull()
  })
})

describe('writeStoryHint', () => {
  it('schrijft lastActiveStoryId per productId', () => {
    writeStoryHint('prod-1', 's-1')
    expect(readHints().perProduct['prod-1'].lastActiveStoryId).toBe('s-1')
  })

  it('null wist child task-hint', () => {
    writeStoryHint('prod-1', 's-1')
    writeTaskHint('prod-1', 't-1')
    writeStoryHint('prod-1', null)
    expect(readHints().perProduct['prod-1'].lastActiveStoryId).toBeNull()
    expect(readHints().perProduct['prod-1'].lastActiveTaskId).toBeNull()
  })
})

describe('writeTaskHint', () => {
  it('schrijft lastActiveTaskId per productId', () => {
    writeTaskHint('prod-1', 't-1')
    expect(readHints().perProduct['prod-1'].lastActiveTaskId).toBe('t-1')
  })
})

describe('clearHints', () => {
  it('verwijdert alle hints', () => {
    writeProductHint('p1')
    writePbiHint('p1', 'pbi-1')
    clearHints()
    const hints = readHints()
    expect(hints.lastActiveProductId).toBeNull()
    expect(hints.perProduct).toEqual({})
  })
})
