import { test } from 'node:test'
import assert from 'node:assert/strict'
import { unzipSync } from 'fflate'
import { createWebCadFacade } from '../src/facade/mockFacade'
import { decodeFcstdPropertyValue } from '../src/facade/fcstd'
import { convertQuantity, quantityFromUnit } from '../src/facade/units'
import type { DocumentSnapshot } from '../src/facade/types'

const fixture = (): DocumentSnapshot => ({
  id: 'property-acceleration-facade',
  label: 'PropertyAcceleration Facade',
  version: 1,
  dirty: false,
  readOnly: false,
  units: 'mm',
  tree: [
    { id: 'SourceTrajectory', label: 'Source trajectory', type: 'feature', state: 'valid' },
    { id: 'AccelerationProbe', label: 'Acceleration probe', type: 'feature', state: 'valid' },
  ],
  objects: [
    { id: 'SourceTrajectory', typeId: 'Robot::TrajectoryObject', properties: [] },
    {
      id: 'AccelerationProbe',
      typeId: 'Robot::TrajectoryDressUpObject',
      properties: [{ name: 'Acceleration', label: 'Acceleration', group: 'TrajectoryDressUp', scope: 'data', type: 'App::PropertyAcceleration', value: 1000, unit: 'mm/s^2', recompute: true }],
    },
  ],
  dependencies: [],
  recompute: { generation: 0, status: 'idle', objectStates: { SourceTrajectory: 'up-to-date', AccelerationProbe: 'up-to-date' }, dirtyObjects: [], order: [], errors: [] },
})

test('App::PropertyAcceleration uses the typed Facade and native FCStd Float codec', () => {
  const facade = createWebCadFacade({ initialDocument: fixture(), initialSelectedObjectIds: ['AccelerationProbe'], runtimeMode: 'mock' })
  const before = facade.app.document.getActive()
  facade.app.document.setProperty({ objectId: 'AccelerationProbe', propertyName: 'Acceleration', value: 500 })
  const edited = facade.app.document.getActive()
  const property = edited.objects.find((object) => object.id === 'AccelerationProbe')?.properties.find((candidate) => candidate.name === 'Acceleration')
  assert.equal(property?.type, 'App::PropertyAcceleration')
  assert.equal(property?.value, 500)
  assert.equal(property?.unit, 'mm/s^2')
  assert.equal(edited.version, before.version + 1)
  assert.equal(edited.dirty, true)
  assert.equal(edited.recompute?.objectStates.AccelerationProbe, 'touched')
  assert.throws(() => facade.app.document.setProperty({ objectId: 'AccelerationProbe', propertyName: 'Acceleration', value: '500 mm/s^2' }), /finite numeric value/)
  assert.throws(() => facade.app.document.setProperty({ objectId: 'AccelerationProbe', propertyName: 'Acceleration', value: Number.POSITIVE_INFINITY }), /finite numeric value/)

  const archive = facade.project.fcstd.serializeMetadata(edited)
  const documentXml = new TextDecoder().decode(unzipSync(archive)['Document.xml'])
  assert.match(documentXml, /<Object name="AccelerationProbe"><Properties Count="1" TransientCount="0"><Property name="Acceleration" type="App::PropertyAcceleration"><Float value="500"\/><\/Property><\/Properties><\/Object>/)
  assert.doesNotMatch(documentXml, /<Property name="Acceleration"[^>]+group=/)
  const summary = facade.project.fcstd.inspect(archive).objects.find((object) => object.name === 'AccelerationProbe')?.properties.find((candidate) => candidate.name === 'Acceleration')
  assert.equal(summary?.typeId, 'App::PropertyAcceleration')
  assert.equal(summary?.element, 'Float')
  assert.deepEqual(decodeFcstdPropertyValue(summary!), { value: 500, decoded: true })
  assert.equal(convertQuantity(quantityFromUnit(9.81, 'm/s^2'), 'mm/s^2'), 9810)
})
