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-heatflux-facade',
  label: 'PropertyHeatFlux Facade',
  version: 1,
  dirty: false,
  readOnly: false,
  units: 'mm',
  tree: [{ id: 'HeatFluxProbe', label: 'Heat flux probe', type: 'feature', state: 'valid' }],
  objects: [{ id: 'HeatFluxProbe', typeId: 'Fem::ConstraintHeatflux', properties: [{ name: 'DFlux', label: 'DFlux', group: 'ConstraintHeatflux', scope: 'data', type: 'App::PropertyHeatFlux', value: 0, unit: 'kg/s^3', recompute: true }] }],
  dependencies: [],
  recompute: { generation: 0, status: 'idle', objectStates: { HeatFluxProbe: 'up-to-date' }, dirtyObjects: [], order: [], errors: [] },
})

test('App::PropertyHeatFlux uses the native internal unit and Float FCStd codec', () => {
  const facade = createWebCadFacade({ initialDocument: fixture(), initialSelectedObjectIds: ['HeatFluxProbe'], runtimeMode: 'mock' })
  facade.app.document.setProperty({ objectId: 'HeatFluxProbe', propertyName: 'DFlux', value: 500 })
  const edited = facade.app.document.getActive()
  const property = edited.objects[0].properties[0]
  assert.deepEqual({ type: property.type, value: property.value, unit: property.unit }, { type: 'App::PropertyHeatFlux', value: 500, unit: 'kg/s^3' })
  assert.equal(edited.version, 2)
  assert.equal(edited.dirty, true)
  assert.equal(edited.recompute?.objectStates.HeatFluxProbe, 'touched')
  assert.throws(() => facade.app.document.setProperty({ objectId: 'HeatFluxProbe', propertyName: 'DFlux', value: '500 W/m^2' as unknown as number }), /finite numeric value/)
  assert.throws(() => facade.app.document.setProperty({ objectId: 'HeatFluxProbe', propertyName: 'DFlux', 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, /<Property name="DFlux" type="App::PropertyHeatFlux" group="ConstraintHeatflux"[^>]*><Float value="500"\/><\/Property>/)
  const summary = facade.project.fcstd.inspect(archive).objects.find(({ name }) => name === 'HeatFluxProbe')?.properties.find(({ name }) => name === 'DFlux')
  assert.deepEqual({ typeId: summary?.typeId, element: summary?.element }, { typeId: 'App::PropertyHeatFlux', element: 'Float' })
  assert.deepEqual(decodeFcstdPropertyValue(summary!), { value: 500, decoded: true })

  const rewritten = facade.project.fcstd.rewriteNumeric(archive, { objectName: 'HeatFluxProbe', propertyName: 'DFlux', typeId: 'App::PropertyHeatFlux', value: -5, expectedValue: 500 })
  const rewrittenSummary = facade.project.fcstd.inspect(rewritten).objects.find(({ name }) => name === 'HeatFluxProbe')?.properties.find(({ name }) => name === 'DFlux')
  assert.deepEqual(decodeFcstdPropertyValue(rewrittenSummary!), { value: -5, decoded: true })
  assert.throws(() => facade.project.fcstd.rewriteNumeric(archive, { objectName: 'HeatFluxProbe', propertyName: 'DFlux', typeId: 'App::PropertyHeatFlux', value: 1000, expectedValue: 1 }), /expected 1, found 500/)
  assert.equal(convertQuantity(quantityFromUnit(2, 'kW/m^2'), 'W/m^2'), 2000)
  assert.equal(convertQuantity(quantityFromUnit(1, 'W/mm^2'), 'kg/s^3'), 1000000)
})
