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

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

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

  it('herstelt hints uit localStorage', () => {
    localStorage.setItem(
      'sprint-workspace-hints',
      JSON.stringify({
        lastActiveProductId: 'p1',
        perProduct: { p1: { lastActiveSprintId: 'sp-1' } },
        perSprint: { 'sp-1': { lastActiveStoryId: 's-1' } },
      }),
    )
    const hints = readHints()
    expect(hints.lastActiveProductId).toBe('p1')
    expect(hints.perProduct.p1.lastActiveSprintId).toBe('sp-1')
    expect(hints.perSprint['sp-1'].lastActiveStoryId).toBe('s-1')
  })

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

  it('valt terug op defaults bij verkeerde shape', () => {
    localStorage.setItem('sprint-workspace-hints', '"just a string"')
    const hints = readHints()
    expect(hints.perProduct).toEqual({})
    expect(hints.perSprint).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('writeSprintHint', () => {
  it('schrijft lastActiveSprintId per productId', () => {
    writeSprintHint('prod-1', 'sp-a')
    writeSprintHint('prod-2', 'sp-b')
    const hints = readHints()
    expect(hints.perProduct['prod-1'].lastActiveSprintId).toBe('sp-a')
    expect(hints.perProduct['prod-2'].lastActiveSprintId).toBe('sp-b')
  })

  it('accepteert null om sprint-hint te wissen', () => {
    writeSprintHint('prod-1', 'sp-a')
    writeSprintHint('prod-1', null)
    expect(readHints().perProduct['prod-1'].lastActiveSprintId).toBeNull()
  })
})

describe('writeStoryHint', () => {
  it('schrijft lastActiveStoryId per sprintId', () => {
    writeStoryHint('sp-1', 's-1')
    expect(readHints().perSprint['sp-1'].lastActiveStoryId).toBe('s-1')
  })

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

describe('writeTaskHint', () => {
  it('schrijft lastActiveTaskId per sprintId', () => {
    writeTaskHint('sp-1', 't-1')
    expect(readHints().perSprint['sp-1'].lastActiveTaskId).toBe('t-1')
  })
})

describe('clearHints', () => {
  it('verwijdert alle hints', () => {
    writeProductHint('p1')
    writeSprintHint('p1', 'sp-1')
    writeStoryHint('sp-1', 's-1')
    clearHints()
    const hints = readHints()
    expect(hints.lastActiveProductId).toBeNull()
    expect(hints.perProduct).toEqual({})
    expect(hints.perSprint).toEqual({})
  })
})
